Rework start/stop api to use runtime-event notification message

This commit is contained in:
Nick O'Leary
2022-06-29 10:27:44 +01:00
parent 68c1e49f62
commit f33848e16b
11 changed files with 107 additions and 140 deletions

View File

@@ -73,10 +73,6 @@ var api = module.exports = {
if (deploymentType === 'reload') {
apiPromise = runtime.flows.loadFlows(true);
} else {
//ensure the runtime running/stopped state matches the deploying editor. If not, then copy the _rev number to flows.rev
if(flows.hasOwnProperty('_rev') && !flows.hasOwnProperty('rev') && (flows.runtimeState !== "stopped" || runtime.flows.started)) {
flows.rev = flows._rev
}
if (flows.hasOwnProperty('rev')) {
var currentVersion = runtime.flows.getFlows().rev;
if (currentVersion !== flows.rev) {
@@ -271,9 +267,7 @@ var api = module.exports = {
getState: async function(opts) {
runtime.log.audit({event: "flows.getState"}, opts.req);
const result = {
state: runtime.flows.started ? "started" : "stopped",
started: !!runtime.flows.started,
rev: runtime.flows.getFlows().rev
state: runtime.flows.state()
}
return result;
},
@@ -282,7 +276,7 @@ var api = module.exports = {
* @param {Object} opts
* @param {Object} opts.req - the request to log (optional)
* @param {User} opts.user - the user calling the api
* @param {string} opts.requestedState - the requested state. Valid values are "start" and "stop".
* @param {string} opts.state - the requested state. Valid values are "start" and "stop".
* @return {Promise<Flow>} - the active flow configuration
* @memberof @node-red/runtime_flows
*/
@@ -295,7 +289,7 @@ var api = module.exports = {
err.code = err.code || errcode || "unexpected_error"
runtime.log.audit({
event: "flows.setState",
state: opts.requestedState || "",
state: opts.state || "",
error: errcode || "unexpected_error",
message: err.code
}, opts.req);
@@ -304,21 +298,22 @@ var api = module.exports = {
const getState = () => {
return {
state: runtime.flows.started ? "started" : "stopped",
started: !!runtime.flows.started,
rev: runtime.flows.getFlows().rev,
state: runtime.flows.state()
}
}
if(!runtime.settings.runtimeState || runtime.settings.runtimeState.enabled !== true) {
throw (makeError("Method Not Allowed", "not_allowed", 405))
}
switch (opts.requestedState) {
switch (opts.state) {
case "start":
try {
try {
runtime.settings.set('flowsRunStateRequested', opts.requestedState);
} catch(err) { }
runtime.settings.set('runtimeFlowState', opts.state);
} catch(err) {}
if (runtime.settings.safeMode) {
delete runtime.settings.safeMode
}
await runtime.flows.startFlows("full")
return getState()
} catch (err) {
@@ -327,15 +322,15 @@ var api = module.exports = {
case "stop":
try {
try {
runtime.settings.set('flowsRunStateRequested', opts.requestedState);
} catch(err) { }
runtime.settings.set('runtimeFlowState', opts.state);
} catch(err) {}
await runtime.flows.stopFlows("full")
return getState()
} catch (err) {
throw (makeError(err, err.code, 500))
}
default:
throw (makeError(`Cannot change flows runtime state to '${opts.requestedState}'}`, "invalid_run_state", 400))
throw (makeError(`Cannot change flows runtime state to '${opts.state}'}`, "invalid_run_state", 400))
}
},
}