2021-03-04 17:02:07 +08:00
|
|
|
import { ReposService } from './../repos/repos.service';
|
2021-03-02 16:28:37 +08:00
|
|
|
import { OnQueueCompleted, Process, Processor } from '@nestjs/bull';
|
|
|
|
import { Job } from 'bull';
|
2021-03-04 17:02:07 +08:00
|
|
|
import { spawn } from 'child_process';
|
2021-03-02 16:28:37 +08:00
|
|
|
import { PipelineTask } from './pipeline-task.entity';
|
|
|
|
import { PIPELINE_TASK_QUEUE } from './pipeline-tasks.constants';
|
|
|
|
import { PipelineTasksService } from './pipeline-tasks.service';
|
2021-03-04 17:02:07 +08:00
|
|
|
import { ApplicationException } from '../commons/exceptions/application.exception';
|
|
|
|
import { PipelineUnits } from './enums/pipeline-units.enum';
|
2021-03-02 16:28:37 +08:00
|
|
|
@Processor(PIPELINE_TASK_QUEUE)
|
|
|
|
export class PipelineTaskConsumer {
|
2021-03-04 17:02:07 +08:00
|
|
|
constructor(
|
|
|
|
private readonly service: PipelineTasksService,
|
|
|
|
private readonly reposService: ReposService,
|
|
|
|
) {}
|
2021-03-02 16:28:37 +08:00
|
|
|
@Process()
|
2021-03-04 17:02:07 +08:00
|
|
|
async doTask({ data: task }: Job<PipelineTask>) {
|
|
|
|
const workspaceRoot = this.reposService.getWorkspaceRootByTask(task);
|
|
|
|
|
|
|
|
const units = task.units.map(
|
|
|
|
(type) =>
|
|
|
|
task.pipeline.workUnitMetadata.units.find(
|
|
|
|
(unit) => unit.type === type,
|
|
|
|
) ?? { 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async runScript(task: PipelineTask, script: string, workspaceRoot: string) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const errorMessages: string[] = [];
|
|
|
|
const sub = spawn(script, {
|
|
|
|
shell: true,
|
|
|
|
cwd: workspaceRoot,
|
|
|
|
});
|
|
|
|
sub.stderr.on('data', (data) => errorMessages.push(data));
|
|
|
|
sub.addListener('close', (code) => {
|
|
|
|
if (code === 0) {
|
|
|
|
return resolve(code);
|
|
|
|
}
|
|
|
|
return reject(new ApplicationException(errorMessages.join('\n')));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2021-03-02 16:28:37 +08:00
|
|
|
|
|
|
|
@OnQueueCompleted()
|
|
|
|
onCompleted(job: Job<PipelineTask>) {
|
|
|
|
this.service.doNextTask(job.data.pipeline);
|
|
|
|
}
|
|
|
|
}
|