2021-03-05 17:12:06 +08:00
|
|
|
import { PipelineTaskLogs } from './models/pipeline-task-logs.model';
|
2021-03-04 17:02:07 +08:00
|
|
|
import { ReposService } from './../repos/repos.service';
|
2021-03-05 17:12:06 +08:00
|
|
|
import {
|
|
|
|
InjectQueue,
|
|
|
|
OnQueueCompleted,
|
|
|
|
Process,
|
|
|
|
Processor,
|
|
|
|
} from '@nestjs/bull';
|
|
|
|
import { Job, Queue } 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';
|
2021-03-05 17:12:06 +08:00
|
|
|
import {
|
|
|
|
PIPELINE_TASK_LOG_QUEUE,
|
|
|
|
PIPELINE_TASK_QUEUE,
|
|
|
|
} from './pipeline-tasks.constants';
|
2021-03-02 16:28:37 +08:00
|
|
|
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-05 17:12:06 +08:00
|
|
|
import { PipelineTaskLogMessage } from './models/pipeline-task-log-message.module';
|
|
|
|
import { TaskStatuses } from './enums/task-statuses.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-05 17:12:06 +08:00
|
|
|
@InjectQueue(PIPELINE_TASK_LOG_QUEUE)
|
|
|
|
private readonly logQueue: Queue<PipelineTaskLogMessage>,
|
2021-03-04 17:02:07 +08:00
|
|
|
) {}
|
2021-03-02 16:28:37 +08:00
|
|
|
@Process()
|
2021-03-05 17:12:06 +08:00
|
|
|
async doTask({ data: task, update }: Job<PipelineTask>) {
|
|
|
|
if (task.pipeline.workUnitMetadata.version !== 1) {
|
|
|
|
throw new ApplicationException(
|
|
|
|
'work unit metadata version is not match.',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-03-04 17:02:07 +08:00
|
|
|
const workspaceRoot = this.reposService.getWorkspaceRootByTask(task);
|
|
|
|
|
|
|
|
const units = task.units.map(
|
|
|
|
(type) =>
|
|
|
|
task.pipeline.workUnitMetadata.units.find(
|
|
|
|
(unit) => unit.type === type,
|
|
|
|
) ?? { type: type, scripts: [] },
|
|
|
|
);
|
|
|
|
|
2021-03-05 17:12:06 +08:00
|
|
|
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);
|
2021-03-12 23:00:12 +08:00
|
|
|
update(task);
|
2021-03-05 17:12:06 +08:00
|
|
|
}
|
2021-03-04 17:02:07 +08:00
|
|
|
}
|
2021-03-05 17:12:06 +08:00
|
|
|
} catch (err) {
|
|
|
|
console.log(err);
|
|
|
|
} finally {
|
|
|
|
task = await this.service.updateTask(task);
|
|
|
|
update(task);
|
2021-03-04 17:02:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-05 17:12:06 +08:00
|
|
|
async runScript(
|
|
|
|
script: string,
|
|
|
|
workspaceRoot: string,
|
|
|
|
task?: PipelineTask,
|
|
|
|
): Promise<string[]> {
|
2021-03-04 17:02:07 +08:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const errorMessages: string[] = [];
|
2021-03-05 17:12:06 +08:00
|
|
|
const logs: string[] = [];
|
2021-03-04 17:02:07 +08:00
|
|
|
const sub = spawn(script, {
|
|
|
|
shell: true,
|
|
|
|
cwd: workspaceRoot,
|
|
|
|
});
|
2021-03-05 17:12:06 +08:00
|
|
|
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));
|
|
|
|
});
|
2021-03-04 17:02:07 +08:00
|
|
|
sub.addListener('close', (code) => {
|
|
|
|
if (code === 0) {
|
2021-03-05 17:12:06 +08:00
|
|
|
sub.stdout;
|
|
|
|
return resolve(logs);
|
2021-03-04 17:02:07 +08:00
|
|
|
}
|
2021-03-05 17:12:06 +08:00
|
|
|
return reject(new ApplicationException(errorMessages.join('')));
|
2021-03-04 17:02:07 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2021-03-02 16:28:37 +08:00
|
|
|
|
|
|
|
@OnQueueCompleted()
|
|
|
|
onCompleted(job: Job<PipelineTask>) {
|
|
|
|
this.service.doNextTask(job.data.pipeline);
|
|
|
|
}
|
|
|
|
}
|