@yarkivaev/scada 2.6.16 → 2.6.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -4,6 +4,15 @@ function encoded(value) { return Buffer.from(value).toString('base64'); }
|
|
|
4
4
|
function decoded(value) { return Buffer.from(value, 'base64').toString(); }
|
|
5
5
|
function put(key, value, lease) { return { request_put: { key: encoded(key), value: encoded(JSON.stringify(value)), ...(lease && { lease }) } }; }
|
|
6
6
|
function revision(key, value) { return { key: encoded(key), target: 'MOD', result: 'EQUAL', mod_revision: String(value) }; }
|
|
7
|
+
function cursor(call, root, range) {
|
|
8
|
+
return {
|
|
9
|
+
async load(taskId) {
|
|
10
|
+
const row = await range(`${root}/cursors/${taskId}`);
|
|
11
|
+
return row ? JSON.parse(decoded(row.value)) : null;
|
|
12
|
+
},
|
|
13
|
+
save(taskId, value) { return call('/v3/kv/put', put(`${root}/cursors/${taskId}`, value).request_put); }
|
|
14
|
+
};
|
|
15
|
+
}
|
|
7
16
|
export default function controlStore(call, prefix = '/scada/control') {
|
|
8
17
|
const root = prefix.replace(/\/$/u, '');
|
|
9
18
|
const lock = `${root}/leader`;
|
|
@@ -41,6 +50,7 @@ export default function controlStore(call, prefix = '/scada/control') {
|
|
|
41
50
|
return Boolean(result.succeeded);
|
|
42
51
|
},
|
|
43
52
|
async assignment(id) { const row = await range(`${root}/assignments/${id}`); return row ? JSON.parse(decoded(row.value)) : null; },
|
|
53
|
+
cursor: cursor(call, root, range),
|
|
44
54
|
report(id, value) { return call('/v3/kv/put', put(`${root}/reports/${id}`, value).request_put); },
|
|
45
55
|
async publish(catalog, expectedRevision) {
|
|
46
56
|
validateCatalog(catalog);
|
|
@@ -28,12 +28,11 @@ function healthView(runtime, state) {
|
|
|
28
28
|
initialized: state.initialized && (!state.agent || state.agent.status().appliedGeneration !== null) };
|
|
29
29
|
} };
|
|
30
30
|
}
|
|
31
|
-
async function initialize(collector, owned, state, { env, registry }) {
|
|
31
|
+
async function initialize(collector, owned, state, { env, registry, store }) {
|
|
32
32
|
const { runtime } = collector;
|
|
33
33
|
await owned.open?.();
|
|
34
34
|
if (state.stopped) { throw new Error('Collector stopped during recovery'); }
|
|
35
|
-
if (
|
|
36
|
-
const store = controlStore(clientFromEnv(env, 'COLLECTOR_CONTROL'), env.COLLECTOR_CONTROL_PREFIX);
|
|
35
|
+
if (store) {
|
|
37
36
|
state.agent = assignmentAgent({ nodeId: env.COLLECTOR_NODE_ID, file: env.COLLECTOR_ASSIGNMENT_FILE, store, runtime,
|
|
38
37
|
validate(document) { return validateManifest(document, registry); },
|
|
39
38
|
onError(error) { process.stderr.write(`collector control: ${error.name}\n`); } });
|
|
@@ -45,7 +44,9 @@ async function initialize(collector, owned, state, { env, registry }) {
|
|
|
45
44
|
/** Local executable host. The external agent can instead use createRuntime directly. */
|
|
46
45
|
// eslint-disable-next-line max-statements
|
|
47
46
|
export default async function runCollector({ registry, env = process.env, argv = process.argv.slice(2), signals = true, manifestReader = readManifest, ...services }) {
|
|
48
|
-
const
|
|
47
|
+
const store = env.COLLECTOR_CONTROL_ENDPOINTS
|
|
48
|
+
? controlStore(clientFromEnv(env, 'COLLECTOR_CONTROL'), env.COLLECTOR_CONTROL_PREFIX) : null;
|
|
49
|
+
const owned = runtimeServices(env, { ...services, cursor: services.cursor ?? store?.cursor });
|
|
49
50
|
const runtime = createRuntime({ registry, ...owned });
|
|
50
51
|
const state = { initialized: false, failed: false, health: null, reloading: false, stopped: false, detach: null, agent: null };
|
|
51
52
|
const collector = {
|
|
@@ -73,7 +74,7 @@ export default async function runCollector({ registry, env = process.env, argv =
|
|
|
73
74
|
handle: spoolSurface(owned.deliveryQueue, env.STATE_HTTP_TOKEN) }) }); }
|
|
74
75
|
const recoveryMs = Number(env.COLLECTOR_RECOVERY_TIMEOUT_MS ?? 900000);
|
|
75
76
|
if (!Number.isSafeInteger(recoveryMs) || recoveryMs < 1) { throw new TypeError('Positive collector recovery timeout required'); }
|
|
76
|
-
await deadline(() => { return initialize(collector, owned, state, { env, registry }); }, recoveryMs);
|
|
77
|
+
await deadline(() => { return initialize(collector, owned, state, { env, registry, store }); }, recoveryMs);
|
|
77
78
|
} catch (error) {
|
|
78
79
|
Object.assign(state, { failed: true });
|
|
79
80
|
try { await collector.stop(); } catch (cleanup) { throw new AggregateError([error, cleanup], 'Collector startup cleanup failed'); }
|
|
@@ -75,7 +75,7 @@ export default function createRuntime(options) {
|
|
|
75
75
|
maxTasks: positive(options.maxTasks ?? 1000), maxPending: positive(options.maxPending ?? 100),
|
|
76
76
|
stopTimeoutMs: positive(options.stopTimeoutMs ?? 10000), startTimeoutMs: positive(options.startTimeoutMs ?? 10000),
|
|
77
77
|
budget: taskBudget(options.budgets ?? { maxBytes: 16777216, maxRecords: 16384 }),
|
|
78
|
-
services: { stateStore: options.stateStore, secrets: options.secrets, report: options.report, deliveryQueue: options.deliveryQueue } };
|
|
78
|
+
services: { stateStore: options.stateStore, secrets: options.secrets, report: options.report, deliveryQueue: options.deliveryQueue, cursor: options.cursor } };
|
|
79
79
|
if (!(ctx.registry instanceof Map)) { throw new TypeError('registry must be a Map'); }
|
|
80
80
|
return {
|
|
81
81
|
apply(input) { return enqueue(ctx, input, (slot) => { return apply(ctx, input, slot); }); },
|