Skip to content

Commit

Permalink
feat: return job after start/stop
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowgate15 committed Jun 25, 2022
1 parent 924237a commit 8b6dfe7
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 26 deletions.
26 changes: 21 additions & 5 deletions app/controllers/api/v1/control.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,53 @@ async function checkJobName(ctx, next) {
return next();
}

async function start(ctx) {
async function addJobNameToQuery(ctx, next) {
const { jobName } = ctx.params;

ctx.query = { name: jobName };

return next();
}

async function start(ctx, next) {
const { jobName } = ctx.params;

await ctx.bree.start(jobName);

ctx.body = {};

return next();
}

async function stop(ctx) {
async function stop(ctx, next) {
const { jobName } = ctx.params;

await ctx.bree.stop(jobName);

ctx.body = {};

return next();
}

async function run(ctx) {
async function run(ctx, next) {
const { jobName } = ctx.params;

await ctx.bree.run(jobName);

ctx.body = {};

return next();
}

async function restart(ctx) {
async function restart(ctx, next) {
const { jobName } = ctx.params;

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

ctx.body = {};

return next();
}

module.exports = { checkJobName, start, stop, run, restart };
module.exports = { checkJobName, addJobNameToQuery, start, stop, run, restart };
2 changes: 1 addition & 1 deletion app/controllers/api/v1/sse.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ async function connect(ctx) {
// send bree events over sse
for (const event of ['worker created', 'worker deleted']) {
ctx.bree.on(event, (name) => {
ctx.sse.send({ event, data: { name } });
ctx.sse.send({ event, data: name });
});
}

Expand Down
40 changes: 33 additions & 7 deletions routes/api/v1/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,42 @@ router.post('/jobs', api.v1.jobs.add);
router.use(api.v1.control.checkJobName);

router.post('/start', api.v1.control.start);
router.post('/start/:jobName', api.v1.control.start);
router.post(
'/start/:jobName',
api.v1.control.start,
api.v1.control.addJobNameToQuery,
api.v1.jobs.get
);

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

router.post('/run/:jobName', api.v1.control.run);
router.post(
'/stop/:jobName',
api.v1.control.stop,
api.v1.control.addJobNameToQuery,
api.v1.jobs.get
);

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

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

router.get('/sse/:token', api.v1.sse.connect);
router.post(
'/restart/:jobName',
api.v1.control.restart,
api.v1.control.addJobNameToQuery,
api.v1.jobs.get
);

router.get('/sse', api.v1.sse.connect);
router.get(
'/sse/:token',
api.v1.sse.connect,
api.v1.control.addJobNameToQuery,
api.v1.jobs.get
);

module.exports = router;
2 changes: 2 additions & 0 deletions test/api/v1/restart.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ test.serial('successfully restart active job', async (t) => {
const res = await api.post(`${rootUrl}/active`).send({});

t.is(res.status, 200);
t.is(res.body.length, 1);
t.like(res.body[0], { name: 'active' });

await delay(200);

Expand Down
17 changes: 4 additions & 13 deletions test/api/v1/sse.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,10 @@ const rootUrl = '/v1/sse';
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: 100
},
{
name: 'waiting',
path: path.join(utils.root, 'basic.js'),
interval: 100
},
{
name: 'active',
path: path.join(utils.root, 'long.js')
interval: 1000
}
]
});
Expand All @@ -48,9 +38,10 @@ const eventsMacro = test.macro({
async exec(t, event) {
const es = utils.setupEventSource(t, rootUrl);

await once(es, event);
const [res] = await once(es, event);

t.pass();
t.is(res.type, event);
t.is(res.data, 'delayed');
},
title(_, event) {
return `successfully listen to "${event}" messages`;
Expand Down
2 changes: 2 additions & 0 deletions test/api/v1/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ test.serial('successfully start named job', async (t) => {
const res = await api.post(`${rootUrl}/timeout`).send({});

t.is(res.status, 200);
t.is(res.body.length, 1);
t.like(res.body[0], { name: 'timeout' });

await delay(200);

Expand Down
2 changes: 2 additions & 0 deletions test/api/v1/stop.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ test.serial('successfully stop named job', async (t) => {
const res = await api.post(`${rootUrl}/active`).send({});

t.is(res.status, 200);
t.is(res.body.length, 1);
t.like(res.body[0], { name: 'active' });

t.falsy(bree.workers.has('active'));
});
Expand Down

0 comments on commit 8b6dfe7

Please sign in to comment.