@anhlp/alp-client 0.0.1-beta.3 → 0.0.1-beta.5

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/src/supervise.js CHANGED
@@ -27,9 +27,10 @@ export async function daemonHeld(home) {
27
27
  }
28
28
 
29
29
  /** Polls until an alpd for `home` is ready; returns its socket. */
30
- async function whenReady(home, timeoutMs) {
30
+ async function whenReady(home, timeoutMs, signal) {
31
31
  const deadline = Date.now() + timeoutMs;
32
32
  while (Date.now() < deadline) {
33
+ signal?.throwIfAborted();
33
34
  const lock = await readLock(home);
34
35
  if (lockAlive(lock) && lock.ready) return lock.socket;
35
36
  await new Promise(resolve => setTimeout(resolve, 100));
@@ -41,34 +42,39 @@ async function whenReady(home, timeoutMs) {
41
42
  * Starts alpd for `home` unless it runs, and lifts a hold: someone asked for it.
42
43
  * Returns its socket.
43
44
  */
44
- export async function startDaemon({ home, entry, env = process.env, timeoutMs = 15_000, automatic = false }) {
45
+ export async function startDaemon({ home, entry, env = process.env, timeoutMs = 15_000, automatic = false, signal }) {
45
46
  return withDaemonLifecycle(home, 'start', async () => {
47
+ signal?.throwIfAborted();
46
48
  if (automatic) {
47
49
  if (await daemonHeld(home)) return;
48
50
  } else await releaseHold(home);
49
51
  const lock = await readLock(home);
50
- if (lockAlive(lock)) return lock.ready ? lock.socket : whenReady(home, timeoutMs);
52
+ signal?.throwIfAborted();
53
+ if (lockAlive(lock)) return lock.ready ? lock.socket : whenReady(home, timeoutMs, signal);
51
54
  const service = serviceFor({ home, env });
52
55
  if (service && await installedProgram(service)) {
56
+ signal?.throwIfAborted();
53
57
  await startService(service);
54
- return whenReady(home, timeoutMs);
58
+ return whenReady(home, timeoutMs, signal);
55
59
  }
56
- return ensureDaemon({ home, entry, env, timeoutMs, lifecycleLocked: true });
57
- });
60
+ return ensureDaemon({ home, entry, env, timeoutMs, lifecycleLocked: true, signal });
61
+ }, { signal });
58
62
  }
59
63
 
60
64
  /**
61
65
  * Starts alpd now and starts it again whenever it is found down on `misses` checks in a
62
66
  * row, `intervalMs` apart, unless the user holds it. Two misses let `alp daemon restart`
63
67
  * and `install` finish their own stop and start first. Returns a function that stops
64
- * watching, and resolves once a start it began has settled; alpd keeps running.
68
+ * watching, and resolves once a start it began has settled; alpd keeps running. `by` names the
69
+ * caller in the log line of the first start ("alpd started with <by>").
65
70
  */
66
- export function superviseDaemon({ home, entry, env = process.env, intervalMs = 5_000, misses = 2, log = () => {} }) {
71
+ export function superviseDaemon({ home, entry, env = process.env, intervalMs = 5_000, misses = 2, log = () => {}, by }) {
67
72
  let stopped = false;
68
73
  let missed = 0;
69
74
  let starting;
70
- const start = reason => {
71
- starting ??= startDaemon({ home, entry: typeof entry === 'function' ? entry() : entry, env, automatic: reason !== 'started with Paseo' })
75
+ // The first start is the caller's own request (it lifts a hold); later ones are automatic.
76
+ const start = (reason, automatic = true) => {
77
+ starting ??= startDaemon({ home, entry: typeof entry === 'function' ? entry() : entry, env, automatic })
72
78
  .then(socket => { if (socket) log(`alpd ${reason}`); }, error => log(`could not start alpd: ${error?.message ?? error}`))
73
79
  .finally(() => { starting = undefined; missed = 0; });
74
80
  return starting;
@@ -80,7 +86,8 @@ export function superviseDaemon({ home, entry, env = process.env, intervalMs = 5
80
86
  // Stopped while this check looked: start nothing.
81
87
  if (++missed >= misses && !stopped) await start('was down; started it again');
82
88
  };
83
- const initial = start('started with Paseo');
89
+ // `by` names the supervising service in the log, for example `Paseo` or `alp web`.
90
+ const initial = start(by ? `started with ${by}` : 'started', false);
84
91
  const timer = setInterval(() => { void check(); }, intervalMs);
85
92
  timer.unref?.();
86
93
  const stop = () => { stopped = true; clearInterval(timer); return starting ?? Promise.resolve(); };