@yarkivaev/scada 2.4.0 → 2.4.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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yarkivaev/scada",
3
- "version": "2.4.0",
3
+ "version": "2.4.2",
4
4
  "description": "SCADA domain objects, state persistence, and plant monitoring",
5
5
  "repository": {
6
6
  "type": "git",
@@ -77,11 +77,17 @@ 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
  }
84
84
  }
85
+ function recover(state, timeout) {
86
+ if (timeout && state.fatal === timeout) {
87
+ state.fatal = null;
88
+ retry(state, state.epoch);
89
+ }
90
+ }
85
91
  export function begin(state) {
86
92
  if (state.fatal) { return Promise.reject(state.fatal); }
87
93
  if (state.live) { return Promise.resolve(); }
@@ -89,8 +95,11 @@ export function begin(state) {
89
95
  if (!state.wanted) { return Promise.resolve(); }
90
96
  clearTimeout(state.retry);
91
97
  state.retry = null;
92
- state.opening = deadline(connect(state, state.epoch), state.options.connectTimeoutMs, 'AMQP connect', (error) => {
93
- state.fatal = error; state.wanted = false; state.epoch += 1;
98
+ let timeout;
99
+ const attempt = connect(state, state.epoch);
100
+ void attempt.then(() => { recover(state, timeout); }, () => { recover(state, timeout); });
101
+ state.opening = deadline(attempt, state.options.connectTimeoutMs, 'AMQP connect', (error) => {
102
+ timeout = error; state.fatal = error; state.epoch += 1;
94
103
  }).finally(() => { state.opening = null; });
95
104
  return state.opening;
96
105
  }
@@ -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 = deadline(ctx.open(generation), ctx.connectTimeoutMs, 'AMQP connect', (error) => {
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 = deadline(ctx.open(generation), ctx.connectTimeoutMs, 'AMQP connect', (error) => {
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); },