Skip to content

Commit

Permalink
feat: add restart endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowgate15 committed Jun 5, 2022
1 parent 11bb7de commit eb78800
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
11 changes: 10 additions & 1 deletion app/controllers/api/v1/control.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,13 @@ async function run(ctx) {
ctx.body = {};
}

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

await ctx.bree.stop(jobName);
ctx.bree.start(jobName);

ctx.body = {};
}

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

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

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

module.exports = router;
61 changes: 61 additions & 0 deletions test/api/v1/restart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const path = require('path');
const test = require('ava');
const jwt = require('jsonwebtoken');
const delay = require('delay');

const config = require('../../../config');

const utils = require('../../utils');

const rootUrl = '/v1/restart';

test.before(async (t) => {
await utils.setupApiServer(t, {
jobs: [
{ name: 'done', path: path.join(utils.root, 'basic.js') },
{
name: 'delayed',
path: path.join(utils.root, 'basic.js'),
timeout: 1000
},
{
name: 'waiting',
path: path.join(utils.root, 'basic.js'),
interval: 1000
},
{
name: 'active',
path: path.join(utils.root, 'long.js')
}
]
});
t.context.token = jwt.sign({}, config.jwt.secret);

t.context.api = t.context.api.auth(t.context.token, { type: 'bearer' });
});

test.serial('successfully restart all jobs', async (t) => {
const { bree, api } = t.context;

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

t.is(res.status, 200);

await delay(200);

t.truthy(bree.workers.has('active'));
t.truthy(bree.timeouts.has('delayed'));
t.truthy(bree.intervals.has('waiting'));
});

test.serial('successfully restart active job', async (t) => {
const { bree, api } = t.context;

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

t.is(res.status, 200);

await delay(200);

t.truthy(bree.workers.has('active'));
});

0 comments on commit eb78800

Please sign in to comment.