From eb788002ebe9458caf755b79d4f668da4be69d2c Mon Sep 17 00:00:00 2001 From: Taylor Schley Date: Sun, 5 Jun 2022 10:11:53 -0500 Subject: [PATCH] feat: add restart endpoint --- app/controllers/api/v1/control.js | 11 +++++- routes/api/v1/index.js | 3 ++ test/api/v1/restart.js | 61 +++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 test/api/v1/restart.js diff --git a/app/controllers/api/v1/control.js b/app/controllers/api/v1/control.js index 7a50286..1aa04e2 100644 --- a/app/controllers/api/v1/control.js +++ b/app/controllers/api/v1/control.js @@ -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 }; diff --git a/routes/api/v1/index.js b/routes/api/v1/index.js index 54b5dbf..df98ba5 100644 --- a/routes/api/v1/index.js +++ b/routes/api/v1/index.js @@ -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; diff --git a/test/api/v1/restart.js b/test/api/v1/restart.js new file mode 100644 index 0000000..b45bc56 --- /dev/null +++ b/test/api/v1/restart.js @@ -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')); +});