-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
111 lines (89 loc) · 3.18 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
'use strict';
const { Router } = require('express');
const UnknownErrorResponse = { error: { code: 1000, message: 'Unknown error' } };
const AlreadyStartedErrorResponse = { error: { code: 1001, message: 'Service already started' } };
const InappropriateConditionErrorResponse = { error: { code: 1002, message: 'Inappropriate conditions' } };
const InvalidScaleSizeErrorResponse = { error: { code: 1003, message: 'Invalid scale size' } };
const WorkerStatusErrorResponse = {
error: { code: 1004, message: 'One or more workers have error state' },
};
/**
* @param {Master} options.master
* @return {createApplication.Router}
*/
module.exports = options => {
const { master } = options;
if (!master) {
throw new Error('"master" option is required');
}
if (master.constructor.name !== 'Master') {
throw new Error('"master" option must be valid instance of "Master"');
}
const router = new Router();
router.get('/status', (req, res) => {
try {
res.send({ status: master.getStatus() });
} catch (err) {
res.status(500).send(UnknownErrorResponse);
}
});
router.get('/status/workers', (req, res) => {
try {
res.send({ workers: master.getWorkersStatuses() });
} catch (err) {
res.status(500).send(UnknownErrorResponse);
}
});
router.put('/start', async (req, res) => {
try {
await master.start();
res.send({ status: master.getStatus() });
} catch (err) {
if (err.constructor.name === 'AlreadyInitializedError') {
return res.status(400).send(AlreadyStartedErrorResponse);
}
res.status(500).send(UnknownErrorResponse);
}
});
router.put('/scale', async (req, res) => {
const { size } = req.body;
try {
await master.rescale(size);
res.send({ workers: master.getWorkersStatuses() });
} catch (err) {
if (err.constructor.name === 'InappropriateConditionError') {
return res.status(400).send(InappropriateConditionErrorResponse);
} else if (err.constructor.name === 'InvalidArgumentError') {
return res.status(400).send(InvalidScaleSizeErrorResponse);
}
res.status(500).send(UnknownErrorResponse);
}
});
router.put('/shutdown', async (req, res) => {
try {
await master.gracefulShutdown();
res.send({ status: master.getStatus() });
} catch (err) {
if (err.constructor.name === 'InappropriateConditionError') {
return res.status(400).send(InappropriateConditionErrorResponse);
} else if (err.constructor.name === 'WorkerStatusError') {
return res.status(500).send(WorkerStatusErrorResponse);
}
res.status(500).send(UnknownErrorResponse);
}
});
router.put('/reload', async (req, res) => {
try {
await master.gracefulReload();
res.send({ workers: master.getWorkersStatuses() });
} catch (err) {
if (err.constructor.name === 'InappropriateConditionError') {
return res.status(400).send(InappropriateConditionErrorResponse);
} else if (err.constructor.name === 'WorkerStatusError') {
return res.status(500).send(WorkerStatusErrorResponse);
}
res.status(500).send(UnknownErrorResponse);
}
});
return router;
};