@nosana/node 1.1.73-dev.mr80.17352210002 → 1.1.73-dev.mr80.17376331679

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.
Files changed (29) hide show
  1. package/dist/.env +1 -1
  2. package/dist/package.json +2 -2
  3. package/dist/packages/job-runtime/src/provider/ContainerProvider.d.ts +8 -0
  4. package/dist/packages/job-runtime/src/provider/ContainerProvider.js +175 -121
  5. package/dist/packages/job-runtime/src/provider/ContainerStateManager.d.ts +7 -0
  6. package/dist/packages/job-runtime/src/provider/ContainerStateManager.js +14 -0
  7. package/dist/packages/job-runtime/src/provider/ports.d.ts +3 -1
  8. package/dist/packages/job-runtime/src/runtime/runJob.js +1 -1
  9. package/dist/packages/job-runtime/src/taskManager/TaskManager.d.ts +10 -1
  10. package/dist/packages/job-runtime/src/taskManager/TaskManager.js +34 -7
  11. package/dist/packages/job-runtime/src/taskManager/operations/enforceOperationTimeout.d.ts +4 -0
  12. package/dist/packages/job-runtime/src/taskManager/operations/enforceOperationTimeout.js +9 -2
  13. package/dist/packages/job-runtime/src/taskManager/operations/runTaskManagerOperation.js +9 -3
  14. package/dist/src/NodeManager/index.js +6 -0
  15. package/dist/src/NodeManager/node/Node.js +9 -2
  16. package/dist/src/NodeManager/node/job/JobRuntimeSession.d.ts +26 -3
  17. package/dist/src/NodeManager/node/job/JobRuntimeSession.js +54 -8
  18. package/dist/src/NodeManager/node/job/jobHandler.d.ts +4 -2
  19. package/dist/src/NodeManager/node/job/jobHandler.js +5 -3
  20. package/dist/src/NodeManager/provider/Provider.d.ts +9 -0
  21. package/dist/src/NodeManager/provider/Provider.js +26 -0
  22. package/dist/src/exitCodes.d.ts +35 -2
  23. package/dist/src/exitCodes.js +52 -2
  24. package/dist/src/version/index.d.ts +16 -0
  25. package/dist/src/version/index.js +56 -45
  26. package/dist/src/version/updateWatcher.d.ts +35 -0
  27. package/dist/src/version/updateWatcher.js +81 -0
  28. package/npm-shrinkwrap.json +109 -93
  29. package/package.json +2 -2
@@ -20,6 +20,7 @@ import { abortControllerSelector, nodeAbortControllerSelector, } from './abort/a
20
20
  import { pollForRun } from './utils/poll.js';
21
21
  import { configs } from '../configs/configs.js';
22
22
  import { reportError } from '../monitoring/reportError.js';
23
+ import { updateWatcher } from '../../version/updateWatcher.js';
23
24
  import { StopReasons } from '../../../packages/job-runtime/src/host/index.js';
24
25
  /** How often, and how far apart, the run is read before startup gives up. */
25
26
  const LIVE_JOB_READ_ATTEMPTS = 3;
@@ -183,7 +184,13 @@ export class BasicNode {
183
184
  }
184
185
  return true;
185
186
  };
186
- const run = await this.runHandler.startRunMonitoring(periodicHealthcheck);
187
+ /**
188
+ * The two waits of a cycle, this one and the job's below, are where
189
+ * the node may leave for an update: queued, its position is onchain,
190
+ * and running a job, the job's sidecar runs it on and the next node
191
+ * takes it over. The claim and the finish in between are not.
192
+ */
193
+ const run = await updateWatcher.leaveDuring(this.runHandler.startRunMonitoring(periodicHealthcheck));
187
194
  /**
188
195
  * Once we have found a run in the queue, we want to stop this run monitoring
189
196
  */
@@ -246,7 +253,7 @@ export class BasicNode {
246
253
  * Run the flow asynchronously and handle errors via a Promise.
247
254
  * This lets us run the job in the background (async) and still get an error in this main process.
248
255
  */
249
- await this.jobHandler.runWithErrorHandling();
256
+ await updateWatcher.leaveDuring(this.jobHandler.runWithErrorHandling());
250
257
  /**
251
258
  * Wait for the job to expire before continuing if the setup was successful.
252
259
  */
@@ -57,8 +57,29 @@ export declare class JobRuntimeSession {
57
57
  * exited: serves its socket again, the requests it has been retrying
58
58
  * included, and waits for it as start() does. Its seed and what it wrote
59
59
  * since are where it left them.
60
+ *
61
+ * With `renew`, a sidecar still running on an older job runtime image than
62
+ * the node's (the node was updated mid-job) is replaced by one on the
63
+ * node's, which takes the job up from the data volume they share and the
64
+ * operations' containers, which run on. The replacement failing is logged
65
+ * and the job followed as it is: a sidecar that runs is not given up on.
60
66
  */
61
- attach(): Promise<void>;
67
+ attach(options?: {
68
+ renew?: boolean;
69
+ }): Promise<void>;
70
+ private renew;
71
+ /**
72
+ * Starts the job's sidecar on the job's directory and its data volume,
73
+ * replacing one there is. A sidecar started where one ran before takes
74
+ * the job up from the records the volume holds.
75
+ */
76
+ private startSidecar;
77
+ /**
78
+ * Starts the sidecar again after it went before its result: the exited
79
+ * container as it was, or, with the container gone (removed by hand, or
80
+ * by a runtime that auto-removed it), a new one on the same volume.
81
+ */
82
+ private startAgain;
62
83
  /** Asks the sidecar to stop the job; it exits once its result is written. */
63
84
  stop(reason: StopReason): void;
64
85
  /**
@@ -84,8 +105,10 @@ export declare class JobRuntimeSession {
84
105
  private follow;
85
106
  /**
86
107
  * Waits for the sidecar to exit with its result. One that exits without
87
- * it (a crash, the runtime restarting) is started again, and resumes the
88
- * job from its data volume; one that keeps doing so is given up on.
108
+ * it (a crash, the runtime restarting) is started again, and one that is
109
+ * gone without it (removed from under the node) is started anew; either
110
+ * takes the job up from its data volume. One that keeps going is given up
111
+ * on. Only this session removing the sidecar makes it gone for good.
89
112
  */
90
113
  private waitForExit;
91
114
  }
@@ -69,7 +69,7 @@ export class JobRuntimeSession {
69
69
  };
70
70
  writeJsonAtomic(path.join(this.dir, JOB_RUNTIME_FILES.seed), seed);
71
71
  await this.host.listen();
72
- await provider.startJobRuntime(this.options.jobAddress, this.dir, inline ? 'inline' : 'onchain', this.options.console);
72
+ await this.startSidecar();
73
73
  }
74
74
  catch (error) {
75
75
  await this.remove();
@@ -82,11 +82,54 @@ export class JobRuntimeSession {
82
82
  * exited: serves its socket again, the requests it has been retrying
83
83
  * included, and waits for it as start() does. Its seed and what it wrote
84
84
  * since are where it left them.
85
+ *
86
+ * With `renew`, a sidecar still running on an older job runtime image than
87
+ * the node's (the node was updated mid-job) is replaced by one on the
88
+ * node's, which takes the job up from the data volume they share and the
89
+ * operations' containers, which run on. The replacement failing is logged
90
+ * and the job followed as it is: a sidecar that runs is not given up on.
85
91
  */
86
- async attach() {
92
+ async attach(options = {}) {
87
93
  await this.host.listen();
94
+ if (options.renew)
95
+ await this.renew();
88
96
  this.follow();
89
97
  }
98
+ async renew() {
99
+ const { provider, jobAddress } = this.options;
100
+ if (!(await provider.isJobRuntimeOutdated(jobAddress)))
101
+ return;
102
+ console.log(`The runtime of job ${jobAddress} is on an older job runtime image; replacing it with the node's`);
103
+ try {
104
+ await this.startSidecar();
105
+ }
106
+ catch (error) {
107
+ console.error(`Failed to replace the runtime of job ${jobAddress}: ${describeError(error)}`);
108
+ }
109
+ }
110
+ /**
111
+ * Starts the job's sidecar on the job's directory and its data volume,
112
+ * replacing one there is. A sidecar started where one ran before takes
113
+ * the job up from the records the volume holds.
114
+ */
115
+ startSidecar() {
116
+ const { provider, jobAddress, inline } = this.options;
117
+ return provider.startJobRuntime(jobAddress, this.dir, inline ? 'inline' : 'onchain', this.options.console);
118
+ }
119
+ /**
120
+ * Starts the sidecar again after it went before its result: the exited
121
+ * container as it was, or, with the container gone (removed by hand, or
122
+ * by a runtime that auto-removed it), a new one on the same volume.
123
+ */
124
+ async startAgain() {
125
+ const { provider, jobAddress } = this.options;
126
+ if (await provider.hasJobRuntime(jobAddress)) {
127
+ await provider.restartJobRuntime(jobAddress);
128
+ }
129
+ else {
130
+ await this.startSidecar();
131
+ }
132
+ }
90
133
  /** Asks the sidecar to stop the job; it exits once its result is written. */
91
134
  stop(reason) {
92
135
  this.host.stop(reason);
@@ -150,8 +193,10 @@ export class JobRuntimeSession {
150
193
  }
151
194
  /**
152
195
  * Waits for the sidecar to exit with its result. One that exits without
153
- * it (a crash, the runtime restarting) is started again, and resumes the
154
- * job from its data volume; one that keeps doing so is given up on.
196
+ * it (a crash, the runtime restarting) is started again, and one that is
197
+ * gone without it (removed from under the node) is started anew; either
198
+ * takes the job up from its data volume. One that keeps going is given up
199
+ * on. Only this session removing the sidecar makes it gone for good.
155
200
  */
156
201
  async waitForExit() {
157
202
  const { provider, jobAddress } = this.options;
@@ -159,17 +204,18 @@ export class JobRuntimeSession {
159
204
  const exit = await provider.waitForJobRuntime(jobAddress);
160
205
  const status = this.status;
161
206
  if (this.removing ||
162
- !exit ||
163
207
  status?.result ||
164
- exit.exitCode === 0 ||
208
+ exit?.exitCode === 0 ||
165
209
  restarts >= MAX_RUNTIME_RESTARTS) {
166
210
  return status;
167
211
  }
168
- console.error(`The runtime of job ${jobAddress} exited (${exit.exitCode}) before its result; starting it again`);
212
+ console.error(exit
213
+ ? `The runtime of job ${jobAddress} exited (${exit.exitCode}) before its result; starting it again`
214
+ : `The runtime of job ${jobAddress} is gone before its result; starting another`);
169
215
  await new Promise((resolve) => setTimeout(resolve, RUNTIME_RESTART_DELAY_MS));
170
216
  if (this.removing)
171
217
  return this.status;
172
- await provider.restartJobRuntime(jobAddress).catch((error) => console.error(`Failed to start the runtime of job ${jobAddress} again: ${describeError(error)}`));
218
+ await this.startAgain().catch((error) => console.error(`Failed to start the runtime of job ${jobAddress} again: ${describeError(error)}`));
173
219
  }
174
220
  }
175
221
  }
@@ -61,8 +61,10 @@ export declare class JobHandler {
61
61
  /**
62
62
  * Starts the job's runtime, or takes over the one the job already has: a
63
63
  * node that restarted (an update, a crash) mid-job finds the job's sidecar
64
- * where it left it, still running or exited with its result. False when it
65
- * could not start; the job is then finished as failed.
64
+ * where it left it, still running or exited with its result. One running
65
+ * on an older job runtime image than this node's is replaced first (see
66
+ * JobRuntimeSession.attach). False when it could not start; the job is then
67
+ * finished as failed.
66
68
  */
67
69
  start(job: Job): Promise<boolean>;
68
70
  /**
@@ -196,15 +196,17 @@ export class JobHandler {
196
196
  /**
197
197
  * Starts the job's runtime, or takes over the one the job already has: a
198
198
  * node that restarted (an update, a crash) mid-job finds the job's sidecar
199
- * where it left it, still running or exited with its result. False when it
200
- * could not start; the job is then finished as failed.
199
+ * where it left it, still running or exited with its result. One running
200
+ * on an older job runtime image than this node's is replaced first (see
201
+ * JobRuntimeSession.attach). False when it could not start; the job is then
202
+ * finished as failed.
201
203
  */
202
204
  async start(job) {
203
205
  const jobId = this.jobId();
204
206
  const runtime = this.session(job);
205
207
  try {
206
208
  if (await this.provider.hasJobRuntime(jobId)) {
207
- await runtime.attach();
209
+ await runtime.attach({ renew: true });
208
210
  console.log(`Reattached to the runtime of job ${jobId}`);
209
211
  }
210
212
  else {
@@ -45,6 +45,15 @@ export declare class Provider {
45
45
  * minutes, before its last error is thrown.
46
46
  */
47
47
  hasJobRuntime(jobAddress: string): Promise<boolean>;
48
+ /**
49
+ * Whether the job's sidecar runs on an image other than the configured one
50
+ * as the node has it locally (pulled by pullJobRuntimeImage before the job
51
+ * loop): a node updated mid-job meets the sidecar of the version before it.
52
+ * An exited sidecar is not outdated, nor is one whose image or the
53
+ * configured one cannot be read: an image the node does not have is no
54
+ * reason to replace a sidecar that runs.
55
+ */
56
+ isJobRuntimeOutdated(jobAddress: string): Promise<boolean>;
48
57
  /**
49
58
  * Waits for the job's sidecar to exit, and answers its exit code;
50
59
  * undefined once it is gone. A wait the runtime drops (its daemon
@@ -168,6 +168,8 @@ export class Provider {
168
168
  consoleOutput = 'text') {
169
169
  const { jobRuntime, frp, network } = configs();
170
170
  const name = JOB_RUNTIME_CONTAINER_PREFIX + jobAddress;
171
+ // A sidecar replaced comes back at another address.
172
+ this.jobRuntimeUrls.delete(jobAddress);
171
173
  if (!this.jobRuntimeVolumesSwept) {
172
174
  await this.removeOrphanedJobRuntimeVolumes(jobAddress);
173
175
  this.jobRuntimeVolumesSwept = true;
@@ -270,6 +272,30 @@ export class Provider {
270
272
  }
271
273
  }
272
274
  }
275
+ /**
276
+ * Whether the job's sidecar runs on an image other than the configured one
277
+ * as the node has it locally (pulled by pullJobRuntimeImage before the job
278
+ * loop): a node updated mid-job meets the sidecar of the version before it.
279
+ * An exited sidecar is not outdated, nor is one whose image or the
280
+ * configured one cannot be read: an image the node does not have is no
281
+ * reason to replace a sidecar that runs.
282
+ */
283
+ async isJobRuntimeOutdated(jobAddress) {
284
+ try {
285
+ const [sidecar, image] = await Promise.all([
286
+ this.containerOrchestration
287
+ .getContainer(JOB_RUNTIME_CONTAINER_PREFIX + jobAddress)
288
+ .inspect(),
289
+ this.containerOrchestration
290
+ .getImage(configs().jobRuntime.containerImage)
291
+ .then((image) => image.inspect()),
292
+ ]);
293
+ return sidecar.State.Running && sidecar.Image !== image.Id;
294
+ }
295
+ catch {
296
+ return false;
297
+ }
298
+ }
273
299
  /**
274
300
  * Waits for the job's sidecar to exit, and answers its exit code;
275
301
  * undefined once it is gone. A wait the runtime drops (its daemon
@@ -1,6 +1,6 @@
1
1
  /**
2
- * How the node asks the auto-update wrapper for another process. Keep in sync
3
- * with `auto-update/src/index.ts`.
2
+ * How the node asks the auto-update wrapper for another process, and for a
3
+ * version to be installed. Keep in sync with `auto-update/src/index.ts`.
4
4
  *
5
5
  * The node always leaves with RESPAWN_EXIT_CODE. Every wrapper ever deployed
6
6
  * reinstalls and respawns on it, and stops on anything else, so it is the one
@@ -8,10 +8,20 @@
8
8
  * plain restart, in the job loop or not, which version) travels as an
9
9
  * `ExitRequest` over IPC when the wrapper opened a channel; wrappers without
10
10
  * one treat every exit as an update, which is the old behaviour.
11
+ *
12
+ * A wrapper new enough installs a version while the node runs (`requestInstall`),
13
+ * so the exit for an update costs the node's restart alone and nothing waits
14
+ * on a registry.
11
15
  */
12
16
  export declare const RESPAWN_EXIT_CODE = 129;
13
17
  /** Set by the wrapper on the process that replaces a node in the job loop. */
14
18
  export declare const IN_JOB_LOOP_ENV = "NOSANA_NODE_IN_JOB_LOOP";
19
+ /**
20
+ * Set to `true` by a wrapper that installs on request (`InstallRequest`). An
21
+ * older wrapper ignores the request, so a node only waits for an answer when
22
+ * this says one comes.
23
+ */
24
+ export declare const WRAPPER_INSTALLS_ENV = "NOSANA_NODE_WRAPPER_INSTALLS";
15
25
  /**
16
26
  * Sent to the wrapper before exiting. Add fields freely; a wrapper ignores
17
27
  * what it does not know. A wrapper that does not know a `type` treats the
@@ -25,8 +35,31 @@ export type ExitRequest = {
25
35
  type: 'restart';
26
36
  inJobLoop: boolean;
27
37
  };
38
+ /**
39
+ * Sent to the wrapper while the node runs: install this version beside the
40
+ * running one, and answer with an `InstallResult` once it is in (or not).
41
+ */
42
+ export type InstallRequest = {
43
+ type: 'install';
44
+ version: string;
45
+ };
46
+ /** The wrapper's answer to an `InstallRequest`, `error` saying why not. */
47
+ export type InstallResult = {
48
+ type: 'installed';
49
+ version: string;
50
+ ok: boolean;
51
+ error?: string;
52
+ };
28
53
  /**
29
54
  * Ask the wrapper for another process. Tells it over IPC when there is a
30
55
  * channel, then exits with RESPAWN_EXIT_CODE either way. Never resolves.
31
56
  */
32
57
  export declare function requestExit(request: ExitRequest): Promise<never>;
58
+ /**
59
+ * Ask the wrapper to install `version` beside the running node, so that the
60
+ * exit that follows (requestExit, an update) only restarts the node into it.
61
+ * Resolves with the wrapper's answer, or undefined when none is to be had: a
62
+ * wrapper that does not install on request, no channel, or no answer in
63
+ * time. The exit then has the wrapper install, as every wrapper does.
64
+ */
65
+ export declare function requestInstall(version: string): Promise<InstallResult | undefined>;
@@ -1,6 +1,6 @@
1
1
  /**
2
- * How the node asks the auto-update wrapper for another process. Keep in sync
3
- * with `auto-update/src/index.ts`.
2
+ * How the node asks the auto-update wrapper for another process, and for a
3
+ * version to be installed. Keep in sync with `auto-update/src/index.ts`.
4
4
  *
5
5
  * The node always leaves with RESPAWN_EXIT_CODE. Every wrapper ever deployed
6
6
  * reinstalls and respawns on it, and stops on anything else, so it is the one
@@ -8,12 +8,35 @@
8
8
  * plain restart, in the job loop or not, which version) travels as an
9
9
  * `ExitRequest` over IPC when the wrapper opened a channel; wrappers without
10
10
  * one treat every exit as an update, which is the old behaviour.
11
+ *
12
+ * A wrapper new enough installs a version while the node runs (`requestInstall`),
13
+ * so the exit for an update costs the node's restart alone and nothing waits
14
+ * on a registry.
11
15
  */
12
16
  export const RESPAWN_EXIT_CODE = 129;
13
17
  /** How long the wrapper is given to take the message before leaving anyway. */
14
18
  const SEND_DEADLINE_MS = 2 * 1000;
19
+ /**
20
+ * How long an install is given before the node leaves without the answer,
21
+ * and has the wrapper install the way every wrapper does, with no node running.
22
+ */
23
+ const INSTALL_DEADLINE_MS = 30 * 60 * 1000;
15
24
  /** Set by the wrapper on the process that replaces a node in the job loop. */
16
25
  export const IN_JOB_LOOP_ENV = 'NOSANA_NODE_IN_JOB_LOOP';
26
+ /**
27
+ * Set to `true` by a wrapper that installs on request (`InstallRequest`). An
28
+ * older wrapper ignores the request, so a node only waits for an answer when
29
+ * this says one comes.
30
+ */
31
+ export const WRAPPER_INSTALLS_ENV = 'NOSANA_NODE_WRAPPER_INSTALLS';
32
+ function isInstallResult(message) {
33
+ const result = message;
34
+ return (typeof result === 'object' &&
35
+ result !== null &&
36
+ result.type === 'installed' &&
37
+ typeof result.version === 'string' &&
38
+ typeof result.ok === 'boolean');
39
+ }
17
40
  /**
18
41
  * Ask the wrapper for another process. Tells it over IPC when there is a
19
42
  * channel, then exits with RESPAWN_EXIT_CODE either way. Never resolves.
@@ -31,3 +54,30 @@ export function requestExit(request) {
31
54
  }
32
55
  });
33
56
  }
57
+ /**
58
+ * Ask the wrapper to install `version` beside the running node, so that the
59
+ * exit that follows (requestExit, an update) only restarts the node into it.
60
+ * Resolves with the wrapper's answer, or undefined when none is to be had: a
61
+ * wrapper that does not install on request, no channel, or no answer in
62
+ * time. The exit then has the wrapper install, as every wrapper does.
63
+ */
64
+ export function requestInstall(version) {
65
+ if (!process.connected || process.env[WRAPPER_INSTALLS_ENV] !== 'true') {
66
+ return Promise.resolve(undefined);
67
+ }
68
+ return new Promise((resolve) => {
69
+ const done = (result) => {
70
+ clearTimeout(deadline);
71
+ process.off('message', onMessage);
72
+ resolve(result);
73
+ };
74
+ const onMessage = (message) => {
75
+ if (isInstallResult(message) && message.version === version)
76
+ done(message);
77
+ };
78
+ const deadline = setTimeout(() => done(undefined), INSTALL_DEADLINE_MS);
79
+ process.on('message', onMessage);
80
+ const request = { type: 'install', version };
81
+ process.send?.(request);
82
+ });
83
+ }
@@ -1,2 +1,18 @@
1
+ /**
2
+ * The version on this node's release channel (`next` for a release
3
+ * candidate, `latest` otherwise) when it is newer than the one running;
4
+ * undefined otherwise, and for a dev build. A registry that cannot be read
5
+ * is logged and counts as no newer version.
6
+ */
7
+ export declare function newerVersion(): Promise<string | undefined>;
8
+ /**
9
+ * Leaves for `version`: has the wrapper install it beside this node first,
10
+ * where the wrapper does that, so the exit costs the node's restart alone.
11
+ * Resolves, the node running on, only when the wrapper would not install it
12
+ * (pinned, or the registry failed it); the next check asks again.
13
+ *
14
+ * @param inJobLoop the process that replaces this one stays in the job loop.
15
+ */
16
+ export declare function leaveForVersion(version: string, inJobLoop: boolean): Promise<void>;
1
17
  /** @param inJobLoop the process that replaces this one stays in the job loop. */
2
18
  export declare function validateCLIVersion(inJobLoop?: boolean): Promise<void>;
@@ -1,62 +1,73 @@
1
1
  import chalk from 'chalk';
2
2
  import { pkg } from '../static/index.js';
3
- import { requestExit } from '../exitCodes.js';
3
+ import { requestExit, requestInstall } from '../exitCodes.js';
4
4
  import { fetchDistTags } from './registry.js';
5
- function requiresNewVersion(required, current) {
6
- return parseInt(required) > parseInt(current);
5
+ /** Major, then minor, then patch; a `-rc` suffix does not count. */
6
+ function isNewer(candidate, current) {
7
+ const parts = (version) => version.split('.').map((part) => parseInt(part));
8
+ const [major, minor, patch] = parts(candidate);
9
+ const [currentMajor, currentMinor, currentPatch] = parts(current);
10
+ if (major !== currentMajor)
11
+ return major > currentMajor;
12
+ if (minor !== currentMinor)
13
+ return minor > currentMinor;
14
+ return patch > currentPatch;
7
15
  }
8
- function exitCleanly(currentVersion, latestVersion, inJobLoop) {
9
- console.log(chalk.red(`You are currently running Nosana CLI version ${currentVersion}. Version ${latestVersion} has been released, please update your CLI to the latest version using npm install ${pkg.name}.`));
10
- return requestExit({
11
- type: 'update',
12
- inJobLoop,
13
- requestedVersion: latestVersion,
14
- });
15
- }
16
- /** @param inJobLoop the process that replaces this one stays in the job loop. */
17
- export async function validateCLIVersion(inJobLoop = false) {
18
- // The version the wrapper was told to seed with, for exercising the update
19
- // cycle: let it reach the job loop, whose check still runs and requests the
20
- // update from there. The version installed then no longer matches, so its
21
- // startup check runs as normal.
22
- if (!inJobLoop && pkg.version === process.env.NOSANA_NODE_INITIAL_VERSION) {
23
- console.warn(chalk.yellow(`Running seeded version ${pkg.version}, deferring the version check to the job loop`));
24
- return;
16
+ /**
17
+ * The version on this node's release channel (`next` for a release
18
+ * candidate, `latest` otherwise) when it is newer than the one running;
19
+ * undefined otherwise, and for a dev build. A registry that cannot be read
20
+ * is logged and counts as no newer version.
21
+ */
22
+ export async function newerVersion() {
23
+ if (pkg.version.startsWith('0.0.0')) {
24
+ console.warn(chalk.yellow('Running in dev mode, skipping version check'));
25
+ return undefined;
25
26
  }
26
27
  try {
27
- const [current_major, current_minor, current_patch] = pkg.version.split('.');
28
- if (parseInt(current_major) === 0 &&
29
- parseInt(current_minor) === 0 &&
30
- parseInt(current_patch) === 0) {
31
- console.warn(chalk.yellow('Running in dev mode, skipping version check'));
32
- return;
33
- }
34
28
  const distTags = await fetchDistTags(pkg.name);
35
29
  const distTag = pkg.version.includes('-rc') ? 'next' : 'latest';
36
30
  const registryLatestVersion = distTags?.[distTag];
37
31
  if (!registryLatestVersion || typeof registryLatestVersion !== 'string') {
38
32
  throw new Error('Could not retrieve valid package information from npm');
39
33
  }
40
- const [required_major, required_minor, required_patch] = registryLatestVersion.split('.');
41
- if (parseInt(required_major) === parseInt(current_major)) {
42
- if (parseInt(required_minor) === parseInt(current_minor)) {
43
- if (requiresNewVersion(required_patch, current_patch)) {
44
- await exitCleanly(pkg.version, registryLatestVersion, inJobLoop);
45
- }
46
- }
47
- else {
48
- if (requiresNewVersion(required_minor, current_minor)) {
49
- await exitCleanly(pkg.version, registryLatestVersion, inJobLoop);
50
- }
51
- }
52
- }
53
- else {
54
- if (requiresNewVersion(required_major, current_major)) {
55
- await exitCleanly(pkg.version, registryLatestVersion, inJobLoop);
56
- }
57
- }
34
+ return isNewer(registryLatestVersion, pkg.version)
35
+ ? registryLatestVersion
36
+ : undefined;
58
37
  }
59
38
  catch (error) {
60
39
  console.log(`${chalk.red("Failed to fetch CLI's minimum required version.")}\n${error}`);
40
+ return undefined;
41
+ }
42
+ }
43
+ /**
44
+ * Leaves for `version`: has the wrapper install it beside this node first,
45
+ * where the wrapper does that, so the exit costs the node's restart alone.
46
+ * Resolves, the node running on, only when the wrapper would not install it
47
+ * (pinned, or the registry failed it); the next check asks again.
48
+ *
49
+ * @param inJobLoop the process that replaces this one stays in the job loop.
50
+ */
51
+ export async function leaveForVersion(version, inJobLoop) {
52
+ console.log(chalk.red(`You are currently running Nosana CLI version ${pkg.version}. Version ${version} has been released, please update your CLI to the latest version using npm install ${pkg.name}.`));
53
+ const installed = await requestInstall(version);
54
+ if (installed && !installed.ok) {
55
+ console.log(chalk.yellow(`Version ${version} could not be installed${installed.error ? `: ${installed.error}` : ''}; carrying on with ${pkg.version}.`));
56
+ return;
57
+ }
58
+ await requestExit({ type: 'update', inJobLoop, requestedVersion: version });
59
+ }
60
+ /** @param inJobLoop the process that replaces this one stays in the job loop. */
61
+ export async function validateCLIVersion(inJobLoop = false) {
62
+ // The version the wrapper was told to seed with, for exercising the update
63
+ // cycle: let it reach the job loop, whose check still runs and requests the
64
+ // update from there. The version installed then no longer matches, so its
65
+ // startup check runs as normal.
66
+ if (!inJobLoop && pkg.version === process.env.NOSANA_NODE_INITIAL_VERSION) {
67
+ console.warn(chalk.yellow(`Running seeded version ${pkg.version}, deferring the version check to the job loop`));
68
+ return;
61
69
  }
70
+ const version = await newerVersion();
71
+ if (version)
72
+ await leaveForVersion(version, inJobLoop);
62
73
  }
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Watches the registry for a release while the node is in the job loop, and
3
+ * has the wrapper install it beside the running node as soon as there is one.
4
+ * Then the node leaves for it at the next moment leaving costs the job
5
+ * nothing (`leaveDuring`): while it waits for a run, or for its job's sidecar,
6
+ * which runs the job on without it and is taken over by the node that comes
7
+ * back. Anywhere else in the cycle (claiming, finishing) the node is
8
+ * mid-transaction, and leaves at the next of those waits instead.
9
+ *
10
+ * A node of an older wrapper, which does not install on request, leaves all
11
+ * the same and has the wrapper install, which is the downtime there was.
12
+ */
13
+ export declare class UpdateWatcher {
14
+ private timer;
15
+ private polling;
16
+ private found;
17
+ private resolveReady;
18
+ /**
19
+ * Resolves with the version once it is installed and the node can leave for
20
+ * it; never otherwise.
21
+ */
22
+ readonly ready: Promise<string>;
23
+ /** Starts watching; one already watching, or done, is left as it is. */
24
+ start(): void;
25
+ stop(): void;
26
+ /**
27
+ * Waits for `wait`, unless an update is ready first: then the node leaves
28
+ * for it from here. Resolves with `wait` all the same when the wrapper
29
+ * would not install the update after all.
30
+ */
31
+ leaveDuring<T>(wait: Promise<T>): Promise<T>;
32
+ private poll;
33
+ }
34
+ /** The node's one watcher, started as it enters the job loop. */
35
+ export declare const updateWatcher: UpdateWatcher;