Skip to content

Commit 8b6dfe7

Browse files
committed
feat: return job after start/stop
1 parent 924237a commit 8b6dfe7

File tree

7 files changed

+65
-26
lines changed

7 files changed

+65
-26
lines changed

app/controllers/api/v1/control.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,37 +9,53 @@ async function checkJobName(ctx, next) {
99
return next();
1010
}
1111

12-
async function start(ctx) {
12+
async function addJobNameToQuery(ctx, next) {
13+
const { jobName } = ctx.params;
14+
15+
ctx.query = { name: jobName };
16+
17+
return next();
18+
}
19+
20+
async function start(ctx, next) {
1321
const { jobName } = ctx.params;
1422

1523
await ctx.bree.start(jobName);
1624

1725
ctx.body = {};
26+
27+
return next();
1828
}
1929

20-
async function stop(ctx) {
30+
async function stop(ctx, next) {
2131
const { jobName } = ctx.params;
2232

2333
await ctx.bree.stop(jobName);
2434

2535
ctx.body = {};
36+
37+
return next();
2638
}
2739

28-
async function run(ctx) {
40+
async function run(ctx, next) {
2941
const { jobName } = ctx.params;
3042

3143
await ctx.bree.run(jobName);
3244

3345
ctx.body = {};
46+
47+
return next();
3448
}
3549

36-
async function restart(ctx) {
50+
async function restart(ctx, next) {
3751
const { jobName } = ctx.params;
3852

3953
await ctx.bree.stop(jobName);
4054
await ctx.bree.start(jobName);
4155

4256
ctx.body = {};
57+
58+
return next();
4359
}
4460

45-
module.exports = { checkJobName, start, stop, run, restart };
61+
module.exports = { checkJobName, addJobNameToQuery, start, stop, run, restart };

app/controllers/api/v1/sse.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ async function connect(ctx) {
33
// send bree events over sse
44
for (const event of ['worker created', 'worker deleted']) {
55
ctx.bree.on(event, (name) => {
6-
ctx.sse.send({ event, data: { name } });
6+
ctx.sse.send({ event, data: name });
77
});
88
}
99

routes/api/v1/index.js

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,42 @@ router.post('/jobs', api.v1.jobs.add);
1919
router.use(api.v1.control.checkJobName);
2020

2121
router.post('/start', api.v1.control.start);
22-
router.post('/start/:jobName', api.v1.control.start);
22+
router.post(
23+
'/start/:jobName',
24+
api.v1.control.start,
25+
api.v1.control.addJobNameToQuery,
26+
api.v1.jobs.get
27+
);
2328

2429
router.post('/stop', api.v1.control.stop);
25-
router.post('/stop/:jobName', api.v1.control.stop);
26-
27-
router.post('/run/:jobName', api.v1.control.run);
30+
router.post(
31+
'/stop/:jobName',
32+
api.v1.control.stop,
33+
api.v1.control.addJobNameToQuery,
34+
api.v1.jobs.get
35+
);
36+
37+
router.post(
38+
'/run/:jobName',
39+
api.v1.control.run,
40+
api.v1.control.addJobNameToQuery,
41+
api.v1.jobs.get
42+
);
2843

2944
router.post('/restart', api.v1.control.restart);
30-
router.post('/restart/:jobName', api.v1.control.restart);
31-
32-
router.get('/sse/:token', api.v1.sse.connect);
45+
router.post(
46+
'/restart/:jobName',
47+
api.v1.control.restart,
48+
api.v1.control.addJobNameToQuery,
49+
api.v1.jobs.get
50+
);
51+
52+
router.get('/sse', api.v1.sse.connect);
53+
router.get(
54+
'/sse/:token',
55+
api.v1.sse.connect,
56+
api.v1.control.addJobNameToQuery,
57+
api.v1.jobs.get
58+
);
3359

3460
module.exports = router;

test/api/v1/restart.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ test.serial('successfully restart active job', async (t) => {
5454
const res = await api.post(`${rootUrl}/active`).send({});
5555

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

5860
await delay(200);
5961

test/api/v1/sse.js

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,10 @@ const rootUrl = '/v1/sse';
1212
test.before(async (t) => {
1313
await utils.setupApiServer(t, {
1414
jobs: [
15-
{ name: 'done', path: path.join(utils.root, 'basic.js') },
1615
{
1716
name: 'delayed',
1817
path: path.join(utils.root, 'basic.js'),
19-
timeout: 100
20-
},
21-
{
22-
name: 'waiting',
23-
path: path.join(utils.root, 'basic.js'),
24-
interval: 100
25-
},
26-
{
27-
name: 'active',
28-
path: path.join(utils.root, 'long.js')
18+
interval: 1000
2919
}
3020
]
3121
});
@@ -48,9 +38,10 @@ const eventsMacro = test.macro({
4838
async exec(t, event) {
4939
const es = utils.setupEventSource(t, rootUrl);
5040

51-
await once(es, event);
41+
const [res] = await once(es, event);
5242

53-
t.pass();
43+
t.is(res.type, event);
44+
t.is(res.data, 'delayed');
5445
},
5546
title(_, event) {
5647
return `successfully listen to "${event}" messages`;

test/api/v1/start.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ test.serial('successfully start named job', async (t) => {
7070
const res = await api.post(`${rootUrl}/timeout`).send({});
7171

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

7476
await delay(200);
7577

test/api/v1/stop.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ test.serial('successfully stop named job', async (t) => {
4646
const res = await api.post(`${rootUrl}/active`).send({});
4747

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

5052
t.falsy(bree.workers.has('active'));
5153
});

0 commit comments

Comments
 (0)