-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathactions.js
More file actions
164 lines (136 loc) · 4.46 KB
/
actions.js
File metadata and controls
164 lines (136 loc) · 4.46 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/* eslint-disable import/no-dynamic-require */
/* eslint-disable consistent-return */
const { join } = require('path');
const logger = require('./common').logger.prefix('actions');
const hooks = require('./hooks');
const actions = {};
const running = {};
const loadAction = (type, name) => {
const file = join(__dirname, `${type}s`, name);
try {
// eslint-disable-next-line global-require
return require(file);
} catch (e) {
hooks.trigger('error', e);
}
};
const watchActionEvents = (events, emitter) => {
events.forEach((eventName) => {
emitter.on(eventName, (data) => {
hooks.trigger('event', eventName, data);
hooks.trigger(eventName, data);
});
});
};
const actionStopped = (id) => {
delete running[id];
};
const actionRunning = (type, id, action, name, opts, emitter) => {
logger.info(`Running: ${name} ${id}`);
if (!action || !name) logger.info(`Error on actionRunning: ${name} -> ${action}`);
running[id] = { name, action };
emitter.once('end', (idEmitter, err, out) => {
if (err) {
hooks.trigger('error', err);
logger.info(`Error: ${JSON.stringify(err)}`);
}
logger.info(`Stopped ${type}: ${name} ${id}`);
actionStopped(idEmitter);
setTimeout(() => {
hooks.trigger(type, 'stopped', idEmitter, name, opts, err, out);
}, 1000);
if (action.events) emitter.removeAllListeners();
});
if (!action.events) return;
watchActionEvents(action.events, emitter);
};
const start = (type, id, name, opts, cb) => {
// Action with same id is not executed
if (running[id]) {
const err = new Error(`Already running: ${running[id].name} with id: ${id}`);
hooks.emit('error', err);
return cb?.(err);
}
if (Object.values(running).filter((x) => x.name === name).length > 0) {
const err = new Error(`Already running: ${name} ${id}`);
hooks.emit('error', err);
return cb?.(err);
}
logger.info(`Starting ${type}: ${name} ${id}`);
const action = loadAction(type, name);
if (!action) return;
action.type = type;
action.options = typeof opts === 'function' ? {} : opts;
// eslint-disable-next-line consistent-return
action.start(id, opts, (err, emitter) => {
if (cb) cb(err);
if (err) {
logger.info(`Failed: ${name} -> ${err.message}`);
if (type === 'action') {
hooks.trigger('error', err);
return hooks.trigger('action', 'failed', id, name, opts, err);
}
return hooks.trigger('error', err);
}
let options = opts;
if (!opts) options = {};
if (type === 'action') {
setTimeout(() => {
hooks.trigger(type, 'started', id, name, options);
}, 500);
}
if (emitter) {
actionRunning(type, id, action, name, options, emitter);
} else {
// no emitter was returned, so we have no way of knowing when
// this action will stop. given that the command watcher needs to know
// when an action finished in order to update the list, we'll manually
// trigger this event after 10 seconds.
setTimeout(() => {
hooks.trigger(type, 'stopped', id, name, options);
}, 10000);
}
});
};
actions.start = (id, name, opts, cb) => {
start('action', id, name, opts, cb);
};
actions.stop = (id, name, opts) => {
if (!Object.hasOwn(running, id)) {
hooks.trigger('action', 'stopped', id, name, opts);
return;
}
const { action } = running[id];
try {
if (!action) {
logger.info(`Action ${running[id].name}, with id: ${id} not running!`);
hooks.trigger('action', 'stopped', id, name, opts);
} else if (!action.stop) {
logger.info(`Action ${running[id].name}, with id: ${id} not stoppable!`);
} else {
logger.info(`Stopping: ${running[id].name}, with id: ${id}`);
action.stop();
actionStopped(id);
}
} catch (err) {
logger.error(`Error stopping action ${id}: ${err}`);
}
};
actions.stopByName = (name, opts) => {
const entries = Object.entries(running).filter(([, val]) => val.name === name);
if (entries.length === 0) {
logger.info(`No running action found with name: ${name}`);
return;
}
entries.forEach(([id]) => {
actions.stop(id, name, opts);
});
};
actions.stop_all = () => {
Object.keys(running).forEach((id) => {
if (Object.hasOwn(running, id)) {
actions.stop(id);
}
});
};
module.exports = actions;