@yarkivaev/scada 2.9.1 → 2.9.2
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
|
@@ -3,15 +3,12 @@ import writeFencedSegment, {
|
|
|
3
3
|
activeSegmentLease, checkSegmentRevision, segmentLeaseMatches, segmentRevision, serialSegmentTransaction, storeWriteReceipt, writeReceipt
|
|
4
4
|
} from '../../persistence/pg/segmentWriteFence.js';
|
|
5
5
|
|
|
6
|
-
const computedProperties = new Set(['phase_revision', 'casting', 'cadence_rule', 'cadence_threshold_ms',
|
|
7
|
-
'casting_detected_at', 'casting_silence_at', 'source_interrupted']);
|
|
8
|
-
|
|
9
6
|
function properties(value) {
|
|
10
7
|
const decoded = JSON.parse(value ?? 'null');
|
|
11
8
|
return decoded && typeof decoded === 'object' && !Array.isArray(decoded) ? decoded : {};
|
|
12
9
|
}
|
|
13
10
|
|
|
14
|
-
function keepOperatorFields(row, prior) {
|
|
11
|
+
function keepOperatorFields(row, prior, computedProperties) {
|
|
15
12
|
if (!prior) { return row; }
|
|
16
13
|
const tagged = prior.tags && !['[]','null'].includes(prior.tags.trim());
|
|
17
14
|
const preserved = tagged ? {...row,options:prior.options,resolved:prior.resolved} : row;
|
|
@@ -29,20 +26,20 @@ function keepOperatorFields(row, prior) {
|
|
|
29
26
|
}
|
|
30
27
|
}
|
|
31
28
|
|
|
32
|
-
async function preparePhases(client, rows) {
|
|
29
|
+
async function preparePhases(client, rows, computedProperties) {
|
|
33
30
|
const {rows:previous} = await client.query(
|
|
34
31
|
"SELECT start_time,properties,tags,options,resolved FROM segments WHERE machine=$1 AND kind='phase' AND start_time=ANY($2::timestamptz[])",
|
|
35
32
|
[rows[0].machine,rows.map(row => { return row.start_time; })]);
|
|
36
33
|
const byStart = new Map(previous.map(row => { return [new Date(row.start_time).toISOString(),row]; }));
|
|
37
|
-
return rows.map(row => { return keepOperatorFields(row,byStart.get(row.start_time)); });
|
|
34
|
+
return rows.map(row => { return keepOperatorFields(row, byStart.get(row.start_time), computedProperties); });
|
|
38
35
|
}
|
|
39
36
|
|
|
40
|
-
async function applyLive(dispatch, record, client, phaseAnchors) {
|
|
37
|
+
async function applyLive(dispatch, record, client, {phaseAnchors,computedProperties}) {
|
|
41
38
|
const transition = record.type === 'phase-transition';
|
|
42
39
|
if (!transition && (record.type !== 'segment' || record.kind !== 'phase')) {
|
|
43
40
|
return dispatch.accept(record,client);
|
|
44
41
|
}
|
|
45
|
-
const rows = await preparePhases(client,transition ? record.pieces : [record]);
|
|
42
|
+
const rows = await preparePhases(client, transition ? record.pieces : [record], computedProperties);
|
|
46
43
|
if (!transition) { return dispatch.accept(rows[0],client); }
|
|
47
44
|
if (phaseAnchors?.has(record.machine)) { return dispatch.accept({...record,pieces:rows},client); }
|
|
48
45
|
// The ordinary recipient has no transition envelope. Its ordered writes still share this transaction.
|
|
@@ -51,17 +48,17 @@ async function applyLive(dispatch, record, client, phaseAnchors) {
|
|
|
51
48
|
},Promise.resolve());
|
|
52
49
|
}
|
|
53
50
|
|
|
54
|
-
async function applyReplica(dispatch,record,client,
|
|
51
|
+
async function applyReplica(dispatch, record, client, settings) {
|
|
55
52
|
const receipt = await writeReceipt(client,record.machine,record.committedOutputId,record.body);
|
|
56
53
|
if (receipt) { return receipt; }
|
|
57
54
|
// Authority can acknowledge a relay of its own receipt without accepting new replica writes.
|
|
58
|
-
if (!acceptReplicaSegments) { return {applied:false}; }
|
|
55
|
+
if (!settings.acceptReplicaSegments) { return {applied:false}; }
|
|
59
56
|
const {rows:[storage]} = await client.query("SELECT to_regclass('segment_timeline_revisions') AS local_revision");
|
|
60
57
|
const eligible = storage.local_revision
|
|
61
58
|
? await segmentLeaseMatches(client,record.machine,undefined)
|
|
62
59
|
: !await activeSegmentLease(client,record.machine);
|
|
63
60
|
if (!eligible) { return {applied:false}; }
|
|
64
|
-
await applyLive(dispatch,record,client,
|
|
61
|
+
await applyLive(dispatch, record, client, settings);
|
|
65
62
|
const revision = storage.local_revision ? await segmentRevision(client,record.machine) : {};
|
|
66
63
|
const outcome = {applied:true,...revision};
|
|
67
64
|
await storeWriteReceipt(client,record.machine,record.committedOutputId,record.body,outcome);
|
|
@@ -72,7 +69,7 @@ function persist(dispatch, record, client, settings) {
|
|
|
72
69
|
if (record.fence) {
|
|
73
70
|
return writeFencedSegment(client,record.machine,record.fence,record.body,async () => {
|
|
74
71
|
await checkSegmentRevision(client,record.machine,record.fence.revision);
|
|
75
|
-
await applyLive(dispatch,record,client,settings
|
|
72
|
+
await applyLive(dispatch, record, client, settings);
|
|
76
73
|
return {applied:true,...await segmentRevision(client,record.machine)};
|
|
77
74
|
});
|
|
78
75
|
}
|
|
@@ -85,15 +82,16 @@ function protectedRecord(record) {
|
|
|
85
82
|
|
|
86
83
|
/** Uses the same configured dispatch for broker deliveries and synchronous HTTP writes. */
|
|
87
84
|
export default function fencedSegments(pool, dispatch, settings) {
|
|
85
|
+
const policy = {...settings, computedProperties: new Set(['phase_revision', ...(settings.processorProperties ?? [])])};
|
|
88
86
|
function accept(record, client) {
|
|
89
87
|
if (!protectedRecord(record)) { return dispatch.accept(record,client); }
|
|
90
88
|
if (!client) {
|
|
91
89
|
return serialSegmentTransaction(pool,record.machine,runner => {
|
|
92
|
-
return persist(dispatch,record,runner,
|
|
90
|
+
return persist(dispatch,record,runner,policy);
|
|
93
91
|
},settings.acceptTimeoutMs);
|
|
94
92
|
}
|
|
95
93
|
return client.query('SELECT pg_advisory_xact_lock(hashtextextended($1, 0))',[ `history:${record.machine}` ])
|
|
96
|
-
.then(() => {return persist(dispatch,record,client,
|
|
94
|
+
.then(() => {return persist(dispatch,record,client,policy)});
|
|
97
95
|
}
|
|
98
96
|
return {
|
|
99
97
|
dispatch: {
|