Skip to content

Commit

Permalink
feat: add stop endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowgate15 committed Aug 20, 2021
1 parent 620b614 commit 993e00c
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 8 deletions.
12 changes: 10 additions & 2 deletions app/controllers/api/v1/control.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ async function checkJobName(ctx, next) {
const { jobName } = ctx.params;

if (jobName && !ctx.bree.config.jobs.some((j) => j.name === jobName))
return Boom.badData('Job name does not exist');
return ctx.throw(Boom.badData('Job name does not exist'));

return next();
}
Expand All @@ -17,4 +17,12 @@ async function start(ctx) {
ctx.body = {};
}

module.exports = { checkJobName, start };
async function stop(ctx) {
const { jobName } = ctx.params;

await ctx.bree.stop(jobName);

ctx.body = {};
}

module.exports = { checkJobName, start, stop };
3 changes: 3 additions & 0 deletions routes/api/v1/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@ router.use(api.v1.control.checkJobName);
router.post('/start', api.v1.control.start);
router.post('/start/:jobName', api.v1.control.start);

router.post('/stop', api.v1.control.stop);
router.post('/stop/:jobName', api.v1.control.stop);

module.exports = router;
14 changes: 8 additions & 6 deletions test/api/v1/stop.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ test.before(async (t) => {
test.serial('successfully stop named job', async (t) => {
const { api, bree } = t.context;

t.truthy(bree.workers.active);

const res = await api.post(`${rootUrl}/active`).send({});

t.is(res.status, 200);
Expand All @@ -51,15 +53,15 @@ test.serial('successfully stop named job', async (t) => {
test.serial('successfully stop all jobs', async (t) => {
const { bree, api } = t.context;

t.not(bree.workers.length, 0);
t.not(bree.timeouts.length, 0);
t.not(bree.intervals.length, 0);
t.not(Object.values(bree.workers).length, 0);
t.not(Object.values(bree.timeouts).length, 0);
t.not(Object.values(bree.intervals).length, 0);

const res = await api.post(rootUrl).send({});

t.is(res.status, 200);

t.is(bree.workers.length, 0);
t.is(bree.timeouts.length, 0);
t.is(bree.intervals.length, 0);
t.is(Object.values(bree.workers).length, 0);
t.is(Object.values(bree.timeouts).length, 0);
t.is(Object.values(bree.intervals).length, 0);
});
15 changes: 15 additions & 0 deletions test/jobs/basic.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
const { parentPort } = require('worker_threads');

setTimeout(() => {
console.log('hello');
}, 100);

if (parentPort) {
parentPort.on('message', (message) => {
if (message === 'error') throw new Error('oops');
if (message === 'cancel') {
parentPort.postMessage('cancelled');
return;
}

parentPort.postMessage(message);
process.exit(0);
});
}
15 changes: 15 additions & 0 deletions test/jobs/long.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
const { parentPort } = require('worker_threads');

setTimeout(() => {
console.log('hello');
}, 10000);

if (parentPort) {
parentPort.on('message', (message) => {
if (message === 'error') throw new Error('oops');
if (message === 'cancel') {
parentPort.postMessage('cancelled');
return;
}

parentPort.postMessage(message);
process.exit(0);
});
}

0 comments on commit 993e00c

Please sign in to comment.