Skip to content

Commit 5df2b17

Browse files
committed
perf(job-queue): run jobs in sequence for non-blocking Docker performance
1 parent 1d1c576 commit 5df2b17

File tree

2 files changed

+31
-18
lines changed

2 files changed

+31
-18
lines changed

src/api/process-manager.ts

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -473,21 +473,26 @@ export function startBuild(data: any, buildConfig?: any): Promise<any> {
473473
.then(bdata => buildData = bdata)
474474
.then(() => sendPendingStatus(buildData, buildData.id))
475475
.then(() => {
476-
return Promise.all(config.map(cfg => {
477-
let dataJob = null;
478-
return dbJob.insertJob({ data: JSON.stringify(cfg), builds_id: data.build_id })
479-
.then(job => dataJob = job)
480-
.then(() => getLastRunId(data.build_id))
481-
.then(lastRunId => {
482-
let jobRun = {
483-
start_time: new Date,
484-
status: 'queued',
485-
build_run_id: lastRunId,
486-
job_id: dataJob.id
487-
};
488-
return dbJobRuns.insertJobRun(jobRun);
489-
}).then(() => queueJob(dataJob.id));
490-
}));
476+
return config.reduce((prev, cfg, i) => {
477+
return prev.then(() => {
478+
let dataJob = null;
479+
480+
return dbJob.insertJob({ data: JSON.stringify(cfg), builds_id: data.build_id })
481+
.then(job => dataJob = job)
482+
.then(() => getLastRunId(data.build_id))
483+
.then(lastRunId => {
484+
const jobRun = {
485+
start_time: new Date,
486+
status: 'queued',
487+
build_run_id: lastRunId,
488+
job_id: dataJob.id
489+
};
490+
491+
return dbJobRuns.insertJobRun(jobRun);
492+
})
493+
.then(() => queueJob(dataJob.id));
494+
});
495+
}, Promise.resolve());
491496
})
492497
.then(lastBuild => {
493498
jobEvents.next({
@@ -539,7 +544,11 @@ export function restartBuild(buildId: number): Promise<any> {
539544
}));
540545
})
541546
.then(() => {
542-
return Promise.all(jobs.map(job => stopJob(job.id).then(() => queueJob(job.id))));
547+
return jobs.reduce((prev, curr) => {
548+
return prev.then(() => {
549+
return stopJob(curr.id).then(() => queueJob(curr.id));
550+
});
551+
}, Promise.resolve());
543552
})
544553
.then(() => getBuild(buildId))
545554
.then(build => sendPendingStatus(build, build.id))
@@ -563,7 +572,11 @@ export function restartBuild(buildId: number): Promise<any> {
563572

564573
export function stopBuild(buildId: number): Promise<any> {
565574
return getBuild(buildId)
566-
.then(build => Promise.all(build.jobs.map(job => stopJob(job.id))))
575+
.then(build => {
576+
return build.jobs.reduce((prev, current) => {
577+
return prev.then(() => stopJob(current.id));
578+
}, Promise.resolve());
579+
})
567580
.catch(err => {
568581
let msg: LogMessageType = { message: `[error]: ${err}`, type: 'error', notify: false };
569582
logger.next(msg);

tests/dev-scripts/push.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const crypto = require('crypto');
33
const data = require('./data/push-request.json');
44
const headers = require('./data/push-request-headers.json');
55

6-
const secret = 'thisIsSecret';
6+
const secret = 'defaultPassword';
77
const sig = crypto.createHmac('sha1', secret).update(JSON.stringify(data)).digest('hex');
88
headers['X-Hub-Signature'] = `sha1=${sig}`;
99

0 commit comments

Comments
 (0)