feat(projects): normalization name.
This commit is contained in:
@ -12,6 +12,7 @@ import { Project } from '../projects/project.entity';
|
||||
import { TaskStatuses } from './enums/task-statuses.enum';
|
||||
import { PipelineTaskLogsService } from './pipeline-task-logs.service';
|
||||
import { ApplicationException } from '../commons/exceptions/application.exception';
|
||||
import { getLoggerToken, PinoLogger } from 'nestjs-pino';
|
||||
|
||||
describe('PipelineTaskConsumer', () => {
|
||||
let consumer: PipelineTaskConsumer;
|
||||
@ -49,6 +50,10 @@ describe('PipelineTaskConsumer', () => {
|
||||
readLogsAsPipelineTaskLogs: async () => [],
|
||||
},
|
||||
},
|
||||
{
|
||||
provide: getLoggerToken(PipelineTaskConsumer.name),
|
||||
useValue: new PinoLogger({}),
|
||||
},
|
||||
PipelineTaskConsumer,
|
||||
],
|
||||
}).compile();
|
||||
@ -92,12 +97,10 @@ describe('PipelineTaskConsumer', () => {
|
||||
'node one-second-work.js',
|
||||
join(__dirname, '../../test/data'),
|
||||
);
|
||||
expect(logText).toMatch(/10.+20.+30.+40.+50.+60.+70.+80.+90/s);
|
||||
expect(recordLog).toHaveBeenCalledTimes(10);
|
||||
expect(
|
||||
((recordLog.mock.calls[8][0] as unknown) as PipelineTaskLogMessage)
|
||||
.message,
|
||||
).toMatch(/^90/);
|
||||
expect(logText).toMatch(
|
||||
/node one-second-work\.js.+10.+20.+30.+40.+50.+60.+70.+80.+90/s,
|
||||
);
|
||||
expect(recordLog).toHaveBeenCalled();
|
||||
});
|
||||
it('should failed and log right message', async () => {
|
||||
await expect(
|
||||
|
@ -16,9 +16,7 @@ import { PipelineUnits } from './enums/pipeline-units.enum';
|
||||
import { PipelineTaskLogMessage } from './models/pipeline-task-log-message.module';
|
||||
import { TaskStatuses } from './enums/task-statuses.enum';
|
||||
import { PipelineTaskLogsService } from './pipeline-task-logs.service';
|
||||
import debug from 'debug';
|
||||
|
||||
const log = debug('fennec:pipeline-tasks:consumer');
|
||||
import { InjectPinoLogger, PinoLogger } from 'nestjs-pino';
|
||||
|
||||
@Processor(PIPELINE_TASK_QUEUE)
|
||||
export class PipelineTaskConsumer {
|
||||
@ -26,6 +24,8 @@ export class PipelineTaskConsumer {
|
||||
private readonly service: PipelineTasksService,
|
||||
private readonly reposService: ReposService,
|
||||
private readonly logsService: PipelineTaskLogsService,
|
||||
@InjectPinoLogger(PipelineTaskConsumer.name)
|
||||
private readonly logger: PinoLogger,
|
||||
) {}
|
||||
@Process()
|
||||
async doTask(job: Job<PipelineTask>) {
|
||||
@ -39,7 +39,7 @@ export class PipelineTaskConsumer {
|
||||
task.startedAt = new Date();
|
||||
task.status = TaskStatuses.working;
|
||||
task = await this.service.updateTask(task);
|
||||
log('start job');
|
||||
this.logger.info({ task }, 'running task [%s].', task.id);
|
||||
await job.update(task);
|
||||
|
||||
const workspaceRoot = this.reposService.getWorkspaceRootByTask(task);
|
||||
@ -51,26 +51,26 @@ export class PipelineTaskConsumer {
|
||||
) ?? { type: type, scripts: [] },
|
||||
);
|
||||
|
||||
log('task have [%o] units', units);
|
||||
this.logger.info({ units }, 'begin run units.');
|
||||
try {
|
||||
for (const unit of units) {
|
||||
const unitLog = new PipelineTaskLogs();
|
||||
unitLog.unit = unit.type;
|
||||
unitLog.startedAt = new Date();
|
||||
log('curr unit is %s', unit.type);
|
||||
this.logger.info('curr unit is %s', unit.type);
|
||||
try {
|
||||
// 检出代码前执行 git checkout
|
||||
if (unit.type === PipelineUnits.checkout) {
|
||||
log('begin checkout');
|
||||
this.logger.debug('begin checkout');
|
||||
await this.reposService.checkout(task, workspaceRoot);
|
||||
unitLog.status = TaskStatuses.success;
|
||||
log('end checkout');
|
||||
this.logger.debug('end checkout');
|
||||
}
|
||||
for (const script of unit.scripts) {
|
||||
unitLog.logs += `[RUN SCRIPT] ${script}`;
|
||||
log('begin runScript %s', script);
|
||||
this.logger.debug('begin runScript %s', script);
|
||||
await this.runScript(script, workspaceRoot, task, unit.type);
|
||||
log('end runScript %s', script);
|
||||
this.logger.debug('end runScript %s', script);
|
||||
}
|
||||
unitLog.status = TaskStatuses.success;
|
||||
} catch (err) {
|
||||
@ -92,9 +92,10 @@ export class PipelineTaskConsumer {
|
||||
}
|
||||
|
||||
task.status = TaskStatuses.success;
|
||||
this.logger.info({ task }, 'task [%s] completed.', task.id);
|
||||
} catch (err) {
|
||||
task.status = TaskStatuses.failed;
|
||||
log('task is failed', err);
|
||||
this.logger.error({ task, error: err }, 'task [%s] failed.', task.id);
|
||||
} finally {
|
||||
task.endedAt = new Date();
|
||||
task = await this.service.updateTask(task);
|
||||
@ -108,6 +109,9 @@ export class PipelineTaskConsumer {
|
||||
task?: PipelineTask,
|
||||
unit?: PipelineUnits,
|
||||
): Promise<void> {
|
||||
await this.logsService.recordLog(
|
||||
PipelineTaskLogMessage.create(task, unit, script + '\n', false),
|
||||
);
|
||||
return new Promise((resolve, reject) => {
|
||||
const sub = spawn(script, {
|
||||
shell: true,
|
||||
@ -133,7 +137,7 @@ export class PipelineTaskConsumer {
|
||||
await new Promise<void>(async (resolve) => {
|
||||
for (let i = 0; i < 10 && loggingCount > 0; i++) {
|
||||
await new Promise((resolve) => setTimeout(resolve, 500));
|
||||
log('waiting logging... (%dx500ms)', i);
|
||||
this.logger.debug('waiting logging... (%dx500ms)', i);
|
||||
}
|
||||
resolve();
|
||||
});
|
||||
@ -147,13 +151,11 @@ export class PipelineTaskConsumer {
|
||||
|
||||
@OnQueueCompleted()
|
||||
onCompleted(job: Job<PipelineTask>) {
|
||||
log('queue onCompleted');
|
||||
this.service.doNextTask(job.data.pipeline);
|
||||
}
|
||||
|
||||
@OnQueueFailed()
|
||||
onFailed(job: Job<PipelineTask>) {
|
||||
log('queue onFailed');
|
||||
this.service.doNextTask(job.data.pipeline);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user