hammerkit 2.1.0-next.14 → 2.1.0-next.16

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.
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ /**
3
+ * The commands of a run in the runner of a sandbox (see kubernetes/sandbox-runner.ts), as the
4
+ * process of the run and the registry of the server know them. A command runs apart from the
5
+ * process that started it, with its output and exit code in a directory of the sandbox's
6
+ * files, so a run that resumes after a restart of the server takes it up again.
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.isRunnerCommandsPath = exports.createRunnerCommands = exports.MAX_OUTPUT_COMMANDS = exports.COMMAND_ID_PATTERN = exports.RUNNER_COMMANDS_DIRECTORY = void 0;
10
+ const path_1 = require("path");
11
+ // Where the directories of the commands are, relative to the root of the sandbox's files:
12
+ // hammerkit's run state, which changes, pushes, the files API and the sources of tasks leave out
13
+ exports.RUNNER_COMMANDS_DIRECTORY = '.hammerkit/commands';
14
+ // the name of the directory of a run's commands, and of each command in it
15
+ exports.COMMAND_ID_PATTERN = /^[0-9a-f]{24}$/;
16
+ // the commands of a run the registry keeps a record of; past it, the records of the commands
17
+ // that ended make way
18
+ exports.MAX_OUTPUT_COMMANDS = 1000;
19
+ function createRunnerCommands(options = {}) {
20
+ // a task of a run that resumes goes on with the execution it was at: the one in flight when
21
+ // the server stopped, or the last one, which ended
22
+ const executions = new Map();
23
+ for (const record of Object.values(options.resumed ?? {})) {
24
+ executions.set(record.task, Math.max(executions.get(record.task) ?? 0, record.execution));
25
+ }
26
+ return { ...options, executions, inUse: new Set() };
27
+ }
28
+ exports.createRunnerCommands = createRunnerCommands;
29
+ // Whether a path is in the directory of the commands of the sandbox whose files are at `root`:
30
+ // only that one, at the root of the files, not a folder of the same name deeper in them
31
+ function isRunnerCommandsPath(path, root) {
32
+ const directory = (0, path_1.join)(root, ...exports.RUNNER_COMMANDS_DIRECTORY.split('/'));
33
+ return path === directory || path.startsWith(`${directory}${path_1.sep}`);
34
+ }
35
+ exports.isRunnerCommandsPath = isRunnerCommandsPath;
36
+ //# sourceMappingURL=runner-commands.js.map
@@ -49,14 +49,17 @@ function followPodLogs(instance, namespace, labelSelector, container, onLine, op
49
49
  const positionOf = (id) => {
50
50
  let position = positions.get(id);
51
51
  if (!position) {
52
- position = { at: null, since: null, count: 0, reads: 0 };
52
+ const from = Array.isArray(options.from) ? options.from.find((cursor) => cursor?.container === id) : undefined;
53
+ position = from
54
+ ? { at: from.at, since: `${from.at.slice(0, 19)}Z`, count: from.count, reads: 0 }
55
+ : { at: null, since: null, count: 0, reads: 0 };
53
56
  positions.set(id, position);
54
57
  }
55
58
  return position;
56
59
  };
57
60
  // a reader of the log of one instance from where it got to: the lines read before, again
58
61
  // at the start of a log read since their second, are passed over
59
- const reader = (position) => {
62
+ const reader = (id, position) => {
60
63
  const from = position.at;
61
64
  const skip = position.count;
62
65
  let atFrom = 0;
@@ -80,7 +83,7 @@ function followPodLogs(instance, namespace, labelSelector, container, onLine, op
80
83
  position.since = `${match[1]}Z`;
81
84
  position.count = 1;
82
85
  }
83
- onLine(match[3] ?? '');
86
+ onLine(match[3] ?? '', { container: id, at, count: position.count });
84
87
  };
85
88
  };
86
89
  const readEnded = async (pod, id, previous) => {
@@ -98,7 +101,7 @@ function followPodLogs(instance, namespace, labelSelector, container, onLine, op
98
101
  ? { sinceSeconds: Math.max(1, Math.ceil((Date.now() - Date.parse(position.since)) / 1000) + 60) }
99
102
  : {}),
100
103
  });
101
- const read = reader(position);
104
+ const read = reader(id, position);
102
105
  for (const line of String(log ?? '').split(/\r?\n/)) {
103
106
  if (line.length > 0) {
104
107
  read(line);
@@ -139,7 +142,7 @@ function followPodLogs(instance, namespace, labelSelector, container, onLine, op
139
142
  end();
140
143
  }));
141
144
  output.on('end', end);
142
- (0, read_lines_1.readLines)(output, exports.MAX_LOG_LINE_CHARS, reader(position));
145
+ (0, read_lines_1.readLines)(output, exports.MAX_LOG_LINE_CHARS, reader(id, position));
143
146
  streaming++;
144
147
  let controller;
145
148
  try {
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.evictKeptClaims = exports.sweepKeptClaims = exports.expireKeptClaims = exports.recordClaimState = exports.forgetClaimState = exports.rememberedClaimState = exports.useClaimState = exports.isKeptClaim = exports.getClaimName = exports.EXPIRES_ANNOTATION = exports.STATE_ANNOTATION = exports.KEPT_BY_LABEL = exports.VOLUME_SCOPE_LABEL = void 0;
3
+ exports.evictKeptClaims = exports.sweepKeptClaims = exports.expireKeptClaims = exports.isClaimWrittenBy = exports.recordClaimState = exports.markClaimWriter = exports.forgetClaimState = exports.rememberedClaimState = exports.useClaimState = exports.isKeptClaim = exports.getClaimName = exports.WRITER_ANNOTATION = exports.EXPIRES_ANNOTATION = exports.STATE_ANNOTATION = exports.KEPT_BY_LABEL = exports.VOLUME_SCOPE_LABEL = void 0;
4
4
  const crypto_1 = require("crypto");
5
5
  const apply_1 = require("./apply");
6
6
  const sleep_1 = require("../utils/sleep");
@@ -18,6 +18,10 @@ exports.KEPT_BY_LABEL = 'hammerkit.dev/kept-by';
18
18
  exports.STATE_ANNOTATION = 'hammerkit.dev/state';
19
19
  // when it goes, set by the end of the run that mounted it last
20
20
  exports.EXPIRES_ANNOTATION = 'hammerkit.dev/expires';
21
+ // while a Job writes to it and it names no state: which run and state the Job is of, so a
22
+ // run that resumes after a restart of the server takes the Job over only when no other run
23
+ // wrote to the claim since
24
+ exports.WRITER_ANNOTATION = 'hammerkit.dev/writer';
21
25
  // The claim of a work item: a task's on a server that keeps them carries the scope, so a
22
26
  // run of another source never mounts it, and the name of the task, so that only runs with
23
27
  // that task in their plan mount it: two runs of one scope go one at a time when they have a
@@ -116,17 +120,34 @@ async function rememberedClaimState(instance, kubernetes, name) {
116
120
  return claim.metadata?.annotations?.[exports.STATE_ANNOTATION] ?? null;
117
121
  }
118
122
  exports.rememberedClaimState = rememberedClaimState;
119
- // Before a task writes to its kept claim: the outputs in it stop being those of a state.
120
- // Whether the claim was there, with what an earlier run left in it.
123
+ // Before a task writes to its kept claim: the outputs in it stop being those of a state, and
124
+ // the Job that wrote to it last is not the one that writes to it now. Whether the claim was
125
+ // there, with what an earlier run left in it.
121
126
  function forgetClaimState(instance, kubernetes, name) {
122
- return annotate(instance, kubernetes.namespace, name, { [exports.STATE_ANNOTATION]: null });
127
+ return annotate(instance, kubernetes.namespace, name, { [exports.STATE_ANNOTATION]: null, [exports.WRITER_ANNOTATION]: null });
123
128
  }
124
129
  exports.forgetClaimState = forgetClaimState;
130
+ // Once the files of a Job are in the claim, before the Job starts: which run and state it is
131
+ // of (WRITER_ANNOTATION)
132
+ async function markClaimWriter(instance, kubernetes, name, writer) {
133
+ await annotate(instance, kubernetes.namespace, name, { [exports.WRITER_ANNOTATION]: writer });
134
+ }
135
+ exports.markClaimWriter = markClaimWriter;
125
136
  // Once the outputs of `stateKey` are whole in the kept claim
126
137
  async function recordClaimState(instance, kubernetes, name, stateKey) {
127
- await annotate(instance, kubernetes.namespace, name, { [exports.STATE_ANNOTATION]: stateKey });
138
+ await annotate(instance, kubernetes.namespace, name, { [exports.STATE_ANNOTATION]: stateKey, [exports.WRITER_ANNOTATION]: null });
128
139
  }
129
140
  exports.recordClaimState = recordClaimState;
141
+ // Whether the Job of `writer` was the last to write to the claim, and it names no state since
142
+ async function isClaimWrittenBy(instance, kubernetes, name, writer) {
143
+ const claim = await readClaim(instance, kubernetes.namespace, name);
144
+ const annotations = claim?.metadata?.annotations ?? {};
145
+ return (!!claim &&
146
+ !claim.metadata?.deletionTimestamp &&
147
+ !annotations[exports.STATE_ANNOTATION] &&
148
+ annotations[exports.WRITER_ANNOTATION] === writer);
149
+ }
150
+ exports.isClaimWrittenBy = isClaimWrittenBy;
130
151
  // The kept claims in a namespace
131
152
  async function listKeptClaims(instance, namespace, labelSelector = exports.VOLUME_SCOPE_LABEL) {
132
153
  const claims = await instance.coreApi.listNamespacedPersistentVolumeClaim({ namespace, labelSelector });