feat(pipeline-tasks): 完善 doTask
This commit is contained in:
@@ -1,20 +1,39 @@
|
||||
import { PipelineTaskLogs } from './models/pipeline-task-logs.model';
|
||||
import { ReposService } from './../repos/repos.service';
|
||||
import { OnQueueCompleted, Process, Processor } from '@nestjs/bull';
|
||||
import { Job } from 'bull';
|
||||
import {
|
||||
InjectQueue,
|
||||
OnQueueCompleted,
|
||||
Process,
|
||||
Processor,
|
||||
} from '@nestjs/bull';
|
||||
import { Job, Queue } from 'bull';
|
||||
import { spawn } from 'child_process';
|
||||
import { PipelineTask } from './pipeline-task.entity';
|
||||
import { PIPELINE_TASK_QUEUE } from './pipeline-tasks.constants';
|
||||
import {
|
||||
PIPELINE_TASK_LOG_QUEUE,
|
||||
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';
|
||||
import { PipelineTaskLogMessage } from './models/pipeline-task-log-message.module';
|
||||
import { TaskStatuses } from './enums/task-statuses.enum';
|
||||
@Processor(PIPELINE_TASK_QUEUE)
|
||||
export class PipelineTaskConsumer {
|
||||
constructor(
|
||||
private readonly service: PipelineTasksService,
|
||||
private readonly reposService: ReposService,
|
||||
@InjectQueue(PIPELINE_TASK_LOG_QUEUE)
|
||||
private readonly logQueue: Queue<PipelineTaskLogMessage>,
|
||||
) {}
|
||||
@Process()
|
||||
async doTask({ data: task }: Job<PipelineTask>) {
|
||||
async doTask({ data: task, update }: Job<PipelineTask>) {
|
||||
if (task.pipeline.workUnitMetadata.version !== 1) {
|
||||
throw new ApplicationException(
|
||||
'work unit metadata version is not match.',
|
||||
);
|
||||
}
|
||||
|
||||
const workspaceRoot = this.reposService.getWorkspaceRootByTask(task);
|
||||
|
||||
const units = task.units.map(
|
||||
@@ -24,31 +43,70 @@ export class PipelineTaskConsumer {
|
||||
) ?? { type: type, scripts: [] },
|
||||
);
|
||||
|
||||
for (const unit of units) {
|
||||
// 检出代码时,不执行其他脚本。
|
||||
if (unit.type === PipelineUnits.checkout) {
|
||||
await this.reposService.checkout(task, workspaceRoot);
|
||||
continue;
|
||||
}
|
||||
for (const script of unit.scripts) {
|
||||
await this.runScript(task, script, workspaceRoot);
|
||||
try {
|
||||
for (const unit of units) {
|
||||
const unitLog = new PipelineTaskLogs();
|
||||
unitLog.unit = unit.type;
|
||||
unitLog.startedAt = new Date();
|
||||
try {
|
||||
// 检出代码时,不执行其他脚本。
|
||||
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);
|
||||
unitLog.logs += messages.join('');
|
||||
}
|
||||
unitLog.status = TaskStatuses.success;
|
||||
} catch (err) {
|
||||
unitLog.status = TaskStatuses.failed;
|
||||
unitLog.logs += err.message;
|
||||
throw err;
|
||||
} finally {
|
||||
unitLog.endedAt = new Date();
|
||||
task.logs.push(unitLog);
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
} finally {
|
||||
task = await this.service.updateTask(task);
|
||||
update(task);
|
||||
}
|
||||
}
|
||||
|
||||
async runScript(task: PipelineTask, script: string, workspaceRoot: string) {
|
||||
async runScript(
|
||||
script: string,
|
||||
workspaceRoot: string,
|
||||
task?: PipelineTask,
|
||||
): Promise<string[]> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const errorMessages: string[] = [];
|
||||
const logs: string[] = [];
|
||||
const sub = spawn(script, {
|
||||
shell: true,
|
||||
cwd: workspaceRoot,
|
||||
});
|
||||
sub.stderr.on('data', (data) => errorMessages.push(data));
|
||||
sub.stderr.on('data', (data: Buffer) => {
|
||||
const str = data.toString();
|
||||
errorMessages.push(str);
|
||||
logs.push(str);
|
||||
this.logQueue.add(PipelineTaskLogMessage.create(task, str));
|
||||
});
|
||||
sub.stdout.on('data', (data: Buffer) => {
|
||||
const str = data.toString();
|
||||
logs.push(str);
|
||||
this.logQueue.add(PipelineTaskLogMessage.create(task, str));
|
||||
});
|
||||
sub.addListener('close', (code) => {
|
||||
if (code === 0) {
|
||||
return resolve(code);
|
||||
sub.stdout;
|
||||
return resolve(logs);
|
||||
}
|
||||
return reject(new ApplicationException(errorMessages.join('\n')));
|
||||
return reject(new ApplicationException(errorMessages.join('')));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
Reference in New Issue
Block a user