Skip to content

Commit c107585

Browse files
committed
perf(jobs): start & stop build jobs in parallel rather than in sequence
1 parent c04f8b4 commit c107585

File tree

2 files changed

+36
-52
lines changed

2 files changed

+36
-52
lines changed

src/api/docker.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,7 @@ export function killContainer(id: string): Promise<void> {
196196
if (containerInfo.State.Status === 'exited') {
197197
return container.remove();
198198
} else if (containerInfo.State.Status === 'running') {
199-
return container.stop()
200-
.then(container => container.remove());
199+
return container.kill();
201200
} else {
202201
return Promise.resolve();
203202
}

src/api/process-manager.ts

Lines changed: 35 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,7 @@ export function startBuild(data: any, buildConfig?: any): Promise<any> {
408408
let pr = null;
409409
let sha = null;
410410
let branch = null;
411+
let buildData = null;
411412

412413
return getRepositoryOnly(data.repositories_id)
413414
.then(repository => {
@@ -476,43 +477,37 @@ export function startBuild(data: any, buildConfig?: any): Promise<any> {
476477
return insertBuildRun(data);
477478
})
478479
.then(() => getBuild(data.build_id))
479-
.then(buildData => {
480-
sendPendingStatus(buildData, buildData.id)
481-
.then(() => {
482-
return config.reduce((prev, cfg, i) => {
483-
return prev.then(() => {
484-
let dataJob = null;
485-
486-
return dbJob.insertJob({ data: JSON.stringify(cfg), builds_id: data.build_id })
487-
.then(job => dataJob = job)
488-
.then(() => getLastRunId(data.build_id))
489-
.then(lastRunId => {
490-
const jobRun = {
491-
start_time: new Date,
492-
status: 'queued',
493-
build_run_id: lastRunId,
494-
job_id: dataJob.id
495-
};
496-
497-
return dbJobRuns.insertJobRun(jobRun);
498-
})
499-
.then(() => queueJob(dataJob.id));
500-
});
501-
}, Promise.resolve());
502-
})
503-
.then(() => getLastBuild(userId || null))
504-
.then(lastBuild => {
505-
jobEvents.next({
506-
type: 'process',
507-
build_id: data.build_id,
508-
repository_id: repoId,
509-
data: 'build added',
510-
additionalData: lastBuild
511-
});
512-
})
513-
.then(() => getDepracatedBuilds(buildData))
514-
.then(builds => Promise.all(builds.map(build => stopBuild(build))));
480+
.then(bdata => buildData = bdata)
481+
.then(() => sendPendingStatus(buildData, buildData.id))
482+
.then(() => {
483+
return Promise.all(config.map(cfg => {
484+
let dataJob = null;
485+
return dbJob.insertJob({ data: JSON.stringify(cfg), builds_id: data.build_id })
486+
.then(job => dataJob = job)
487+
.then(() => getLastRunId(data.build_id))
488+
.then(lastRunId => {
489+
const jobRun = {
490+
start_time: new Date,
491+
status: 'queued',
492+
build_run_id: lastRunId,
493+
job_id: dataJob.id
494+
};
495+
return dbJobRuns.insertJobRun(jobRun);
496+
}).then(() => queueJob(dataJob.id));
497+
}));
498+
})
499+
.then(() => getLastBuild(userId || null))
500+
.then(lastBuild => {
501+
jobEvents.next({
502+
type: 'process',
503+
build_id: data.build_id,
504+
repository_id: repoId,
505+
data: 'build added',
506+
additionalData: lastBuild
507+
});
515508
})
509+
.then(() => getDepracatedBuilds(buildData))
510+
.then(builds => Promise.all(builds.map(build => stopBuild(build))))
516511
.catch(err => {
517512
const msg: LogMessageType = { message: `[error]: ${err}`, type: 'error', notify: false };
518513
logger.next(msg);
@@ -551,11 +546,7 @@ export function restartBuild(buildId: number): Promise<any> {
551546
}));
552547
})
553548
.then(() => {
554-
return jobs.reduce((prev, curr) => {
555-
return prev.then(() => {
556-
return stopJob(curr.id).then(() => queueJob(curr.id));
557-
});
558-
}, Promise.resolve());
549+
return Promise.all(jobs.map(job => stopJob(job.id).then(() => queueJob(job.id))));
559550
})
560551
.then(() => getBuild(buildId))
561552
.then(build => sendPendingStatus(build, build.id))
@@ -579,12 +570,7 @@ export function restartBuild(buildId: number): Promise<any> {
579570

580571
export function stopBuild(buildId: number): Promise<any> {
581572
return getBuild(buildId)
582-
.then(build => {
583-
const jobs = build.jobs;
584-
return jobs.reduce((prev, current) => {
585-
return prev.then(() => stopJob(current.id));
586-
}, Promise.resolve());
587-
})
573+
.then(build => Promise.all(build.jobs.map(job => stopJob(job.id))))
588574
.catch(err => {
589575
const msg: LogMessageType = { message: `[error]: ${err}`, type: 'error', notify: false };
590576
logger.next(msg);
@@ -698,8 +684,8 @@ function jobSucceded(proc: JobProcess): Promise<any> {
698684
additionalData: time.getTime()
699685
});
700686

701-
getLastRunId(proc.build_id).then(id => updateBuildRun({ id: id, end_time: time} ));
702-
Promise.resolve();
687+
return getLastRunId(proc.build_id)
688+
.then(id => updateBuildRun({ id: id, end_time: time} ));
703689
});
704690
});
705691
}
@@ -756,7 +742,6 @@ function jobFailed(proc: JobProcess, msg?: LogMessageType): Promise<any> {
756742
message: `[error]: ${err}`, type: 'error', notify: false
757743
};
758744
logger.next(msg);
759-
Promise.resolve();
760745
});
761746
});
762747
}

0 commit comments

Comments
 (0)