@yarkivaev/scada 2.4.1 → 2.4.3
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
|
@@ -24,13 +24,23 @@ export default function intervalFoldPg(pool) {
|
|
|
24
24
|
const start = new Date(sample.ts).toISOString();
|
|
25
25
|
await pool.query(
|
|
26
26
|
`INSERT INTO segments (machine, kind, name, start_time, end_time, duration, resolved, consumed)
|
|
27
|
-
|
|
27
|
+
SELECT $1, $2, $3, $4::timestamptz, $4::timestamptz, 0, TRUE, TRUE
|
|
28
|
+
WHERE NOT EXISTS (
|
|
29
|
+
SELECT 1 FROM (
|
|
30
|
+
SELECT start_time, end_time, duration FROM segments
|
|
31
|
+
WHERE machine = $1 AND kind = $2
|
|
32
|
+
ORDER BY start_time DESC LIMIT 1
|
|
33
|
+
) previous
|
|
34
|
+
WHERE start_time >= $4::timestamptz OR end_time > $4::timestamptz
|
|
35
|
+
OR (duration = 0 AND end_time >= $4::timestamptz)
|
|
36
|
+
)
|
|
37
|
+
ON CONFLICT (machine, kind, start_time) DO NOTHING`,
|
|
28
38
|
[sample.machine, sample.kind, sample.name, start]
|
|
29
39
|
);
|
|
30
40
|
},
|
|
31
41
|
async extend(row, ts) {
|
|
32
42
|
await pool.query(
|
|
33
|
-
`UPDATE segments SET end_time = $1
|
|
43
|
+
`UPDATE segments SET end_time = GREATEST(end_time, $1::timestamptz)
|
|
34
44
|
WHERE machine = $2 AND kind = $3 AND start_time = $4 AND duration = 0`,
|
|
35
45
|
[new Date(ts).toISOString(), row.machine, row.kind, row.start_time]
|
|
36
46
|
);
|
|
@@ -41,7 +51,7 @@ export default function intervalFoldPg(pool) {
|
|
|
41
51
|
const duration = Math.max(1, Math.round((end.getTime() - start.getTime()) / 1000));
|
|
42
52
|
await pool.query(
|
|
43
53
|
`UPDATE segments SET end_time = $1, duration = $2
|
|
44
|
-
WHERE machine = $3 AND kind = $4 AND start_time = $5 AND duration = 0`,
|
|
54
|
+
WHERE machine = $3 AND kind = $4 AND start_time = $5 AND duration = 0 AND end_time < $1::timestamptz`,
|
|
45
55
|
[end.toISOString(), duration, row.machine, row.kind, row.start_time]
|
|
46
56
|
);
|
|
47
57
|
}
|
|
@@ -77,7 +77,7 @@ async function connect(state, epoch) {
|
|
|
77
77
|
if (live.dead) { throw new Error('AMQP session closed during initialization'); }
|
|
78
78
|
await keep(state, live);
|
|
79
79
|
} catch (error) {
|
|
80
|
-
await closeLive(live);
|
|
80
|
+
await closeLive(live).catch((failure) => { state.fatal = failure; throw failure; });
|
|
81
81
|
retry(state, epoch);
|
|
82
82
|
throw error;
|
|
83
83
|
}
|
|
@@ -4,9 +4,9 @@ import brokerClose, { destroyConnection } from '../brokerClose.js';
|
|
|
4
4
|
import deadline from '../deadline.js';
|
|
5
5
|
import positive from '../positive.js';
|
|
6
6
|
|
|
7
|
-
async function connectBroker(url) {
|
|
7
|
+
async function connectBroker(url, options) {
|
|
8
8
|
const { default: amqp } = await import('amqplib');
|
|
9
|
-
return amqp.connect(url, { heartbeat: 10 });
|
|
9
|
+
return amqp.connect(url, { heartbeat: 10, ...options });
|
|
10
10
|
}
|
|
11
11
|
function reportFailure(error) {
|
|
12
12
|
process.stderr.write(`amqp consume failed ${error.message}\n`);
|
|
@@ -36,9 +36,7 @@ function retry(ctx, generation) {
|
|
|
36
36
|
ctx.state.delay = Math.min(pause * 2, 30000);
|
|
37
37
|
ctx.state.timer = ctx.schedule(() => {
|
|
38
38
|
ctx.state.timer = undefined;
|
|
39
|
-
ctx.state.opening =
|
|
40
|
-
ctx.state.fatal = error; ctx.state.running = false; ctx.state.generation += 1;
|
|
41
|
-
});
|
|
39
|
+
ctx.state.opening = ctx.attempt(generation);
|
|
42
40
|
void ctx.state.opening.catch(ctx.report);
|
|
43
41
|
return ctx.state.opening;
|
|
44
42
|
}, pause);
|
|
@@ -65,7 +63,7 @@ async function open(ctx, generation) {
|
|
|
65
63
|
let complete;
|
|
66
64
|
const live = { closed: false, ready: new Promise((resolve) => { complete = resolve; }) };
|
|
67
65
|
try {
|
|
68
|
-
live.connection = await ctx.connect(ctx.url);
|
|
66
|
+
live.connection = await ctx.connect(ctx.url, { timeout: ctx.connectTimeoutMs });
|
|
69
67
|
bind(ctx, generation, live, live.connection);
|
|
70
68
|
await subscribe(ctx, generation, live);
|
|
71
69
|
if (!current(ctx, generation) || live.lost) { await dispose(live, ctx); return; }
|
|
@@ -77,6 +75,20 @@ async function open(ctx, generation) {
|
|
|
77
75
|
retry(ctx, generation);
|
|
78
76
|
} finally { complete(); }
|
|
79
77
|
}
|
|
78
|
+
function recover(ctx, timeout) {
|
|
79
|
+
if (timeout && ctx.state.fatal === timeout) {
|
|
80
|
+
ctx.state.fatal = undefined;
|
|
81
|
+
retry(ctx, ctx.state.generation);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
function attempt(ctx, generation) {
|
|
85
|
+
let timeout;
|
|
86
|
+
const opening = ctx.open(generation);
|
|
87
|
+
void opening.then(() => { recover(ctx, timeout); }, () => { recover(ctx, timeout); });
|
|
88
|
+
return deadline(opening, ctx.connectTimeoutMs, 'AMQP connect', (error) => {
|
|
89
|
+
timeout = error; ctx.state.fatal = error; ctx.state.generation += 1;
|
|
90
|
+
});
|
|
91
|
+
}
|
|
80
92
|
async function begin(ctx) {
|
|
81
93
|
if (ctx.state.fatal) { throw ctx.state.fatal; }
|
|
82
94
|
if (ctx.state.running) { await ctx.state.opening; return; }
|
|
@@ -86,9 +98,7 @@ async function begin(ctx) {
|
|
|
86
98
|
await ctx.state.stopping;
|
|
87
99
|
await ctx.state.cleanup;
|
|
88
100
|
if (current(ctx, generation)) {
|
|
89
|
-
ctx.state.opening =
|
|
90
|
-
ctx.state.fatal = error; ctx.state.running = false; ctx.state.generation += 1;
|
|
91
|
-
});
|
|
101
|
+
ctx.state.opening = attempt(ctx, generation);
|
|
92
102
|
await ctx.state.opening;
|
|
93
103
|
}
|
|
94
104
|
}
|
|
@@ -113,7 +123,8 @@ export default function amqpSession(url, subscribeChannel, options = {}) {
|
|
|
113
123
|
clear: options.clear || clearTimeout,
|
|
114
124
|
report: options.onError || options.report || reportFailure,
|
|
115
125
|
state: { generation: 0, running: false, delay: 1000 },
|
|
116
|
-
open(generation) { return open(ctx, generation); }
|
|
126
|
+
open(generation) { return open(ctx, generation); },
|
|
127
|
+
attempt(generation) { return attempt(ctx, generation); }
|
|
117
128
|
};
|
|
118
129
|
return {
|
|
119
130
|
start() { return begin(ctx); },
|