fix(pipeline-tasks): 修复未保存任务状态到数据库的问题。
This commit is contained in:
@@ -1,18 +1,10 @@
|
||||
import { PipelineTaskLogs } from './models/pipeline-task-logs.model';
|
||||
import { ReposService } from './../repos/repos.service';
|
||||
import {
|
||||
InjectQueue,
|
||||
OnQueueCompleted,
|
||||
Process,
|
||||
Processor,
|
||||
} from '@nestjs/bull';
|
||||
import { Job, Queue } from 'bull';
|
||||
import { OnQueueCompleted, Process, Processor } from '@nestjs/bull';
|
||||
import { Job } from 'bull';
|
||||
import { spawn } from 'child_process';
|
||||
import { PipelineTask } from './pipeline-task.entity';
|
||||
import {
|
||||
PIPELINE_TASK_LOG_QUEUE,
|
||||
PIPELINE_TASK_QUEUE,
|
||||
} from './pipeline-tasks.constants';
|
||||
import { PIPELINE_TASK_QUEUE } from './pipeline-tasks.constants';
|
||||
import { PipelineTasksService } from './pipeline-tasks.service';
|
||||
import { ApplicationException } from '../commons/exceptions/application.exception';
|
||||
import { PipelineUnits } from './enums/pipeline-units.enum';
|
||||
@@ -27,13 +19,19 @@ export class PipelineTaskConsumer {
|
||||
private readonly logsService: PipelineTaskLogsService,
|
||||
) {}
|
||||
@Process()
|
||||
async doTask({ data: task, update }: Job<PipelineTask>) {
|
||||
async doTask(job: Job<PipelineTask>) {
|
||||
let task = job.data;
|
||||
if (task.pipeline.workUnitMetadata.version !== 1) {
|
||||
throw new ApplicationException(
|
||||
'work unit metadata version is not match.',
|
||||
);
|
||||
}
|
||||
|
||||
task.startedAt = new Date();
|
||||
task.status = TaskStatuses.working;
|
||||
task = await this.service.updateTask(task);
|
||||
await job.update(task);
|
||||
|
||||
const workspaceRoot = this.reposService.getWorkspaceRootByTask(task);
|
||||
|
||||
const units = task.units.map(
|
||||
@@ -49,21 +47,14 @@ export class PipelineTaskConsumer {
|
||||
unitLog.unit = unit.type;
|
||||
unitLog.startedAt = new Date();
|
||||
try {
|
||||
// 检出代码时,不执行其他脚本。
|
||||
// 检出代码前执行 git checkout
|
||||
if (unit.type === PipelineUnits.checkout) {
|
||||
await this.reposService.checkout(task, workspaceRoot);
|
||||
unitLog.status = TaskStatuses.success;
|
||||
continue;
|
||||
}
|
||||
for (const script of unit.scripts) {
|
||||
unitLog.logs += `[RUN SCRIPT] ${script}`;
|
||||
const messages = await this.runScript(
|
||||
script,
|
||||
workspaceRoot,
|
||||
task,
|
||||
unit.type,
|
||||
);
|
||||
unitLog.logs += messages.join('');
|
||||
await this.runScript(script, workspaceRoot, task, unit.type);
|
||||
}
|
||||
unitLog.status = TaskStatuses.success;
|
||||
} catch (err) {
|
||||
@@ -72,15 +63,25 @@ export class PipelineTaskConsumer {
|
||||
throw err;
|
||||
} finally {
|
||||
unitLog.endedAt = new Date();
|
||||
unitLog.logs = await this.logsService
|
||||
.readLogsAsPipelineTaskLogs(task)
|
||||
.then(
|
||||
(taskLogs) =>
|
||||
taskLogs.find((tl) => tl.unit === unit.type)?.logs ?? '',
|
||||
);
|
||||
task.logs.push(unitLog);
|
||||
// await update(task);
|
||||
await job.update(task);
|
||||
}
|
||||
}
|
||||
|
||||
task.status = TaskStatuses.success;
|
||||
} catch (err) {
|
||||
task.status = TaskStatuses.failed;
|
||||
console.log(err);
|
||||
} finally {
|
||||
task.endedAt = new Date();
|
||||
task = await this.service.updateTask(task);
|
||||
await update(task);
|
||||
await job.update(task);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,35 +90,29 @@ export class PipelineTaskConsumer {
|
||||
workspaceRoot: string,
|
||||
task?: PipelineTask,
|
||||
unit?: PipelineUnits,
|
||||
): Promise<string[]> {
|
||||
): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const errorMessages: string[] = [];
|
||||
const logs: string[] = [];
|
||||
const sub = spawn(script, {
|
||||
shell: true,
|
||||
cwd: workspaceRoot,
|
||||
});
|
||||
sub.stderr.on('data', (data: Buffer) => {
|
||||
const str = data.toString();
|
||||
errorMessages.push(str);
|
||||
logs.push(str);
|
||||
this.logsService.recordLog(
|
||||
PipelineTaskLogMessage.create(task, unit, str),
|
||||
PipelineTaskLogMessage.create(task, unit, str, true),
|
||||
);
|
||||
});
|
||||
sub.stdout.on('data', (data: Buffer) => {
|
||||
const str = data.toString();
|
||||
logs.push(str);
|
||||
this.logsService.recordLog(
|
||||
PipelineTaskLogMessage.create(task, unit, str),
|
||||
PipelineTaskLogMessage.create(task, unit, str, false),
|
||||
);
|
||||
});
|
||||
sub.addListener('close', (code) => {
|
||||
if (code === 0) {
|
||||
sub.stdout;
|
||||
return resolve(logs);
|
||||
return resolve();
|
||||
}
|
||||
return reject(new ApplicationException(errorMessages.join('')));
|
||||
return reject(new ApplicationException('exec script failed'));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
Reference in New Issue
Block a user