fix(pipeline-tasks): 修复日志有概率丢失最后几条的问题

日志记录结束前,任务信息已入库存档。
This commit is contained in:
Ivan Li 2021-04-06 22:54:51 +08:00
parent 24a2f80e46
commit 246c0bd8f8
2 changed files with 21 additions and 12 deletions

View File

@ -223,9 +223,7 @@ describe('PipelineTaskConsumer', () => {
update: jest.fn().mockImplementation(() => undefined),
} as unknown) as Job;
const runScript = jest
.spyOn(consumer, 'runScript')
.mockImplementation(async () => {
jest.spyOn(consumer, 'runScript').mockImplementation(async () => {
throw new Error('bad message');
});
const updateTask = jest.spyOn(tasksService, 'updateTask');

View File

@ -113,19 +113,30 @@ export class PipelineTaskConsumer {
shell: true,
cwd: workspaceRoot,
});
let loggingCount = 0; // semaphore
sub.stderr.on('data', (data: Buffer) => {
const str = data.toString();
this.logsService.recordLog(
PipelineTaskLogMessage.create(task, unit, str, true),
);
loggingCount++;
this.logsService
.recordLog(PipelineTaskLogMessage.create(task, unit, str, true))
.finally(() => loggingCount--);
});
sub.stdout.on('data', (data: Buffer) => {
const str = data.toString();
this.logsService.recordLog(
PipelineTaskLogMessage.create(task, unit, str, false),
);
loggingCount++;
this.logsService
.recordLog(PipelineTaskLogMessage.create(task, unit, str, false))
.finally(() => loggingCount--);
});
sub.addListener('close', async (code) => {
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);
}
resolve();
});
sub.addListener('close', (code) => {
if (code === 0) {
return resolve();
}