hammerkit 2.1.0-next.17 → 2.1.0-next.19
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/dist/harness/connect.js +2 -1
- package/dist/harness/sandbox-client.js +7 -3
- package/dist/harness/service-tools.js +11 -2
- package/dist/kubernetes/sync-changes.js +180 -0
- package/dist/planner/work-runtime-kubernetes.js +20 -0
- package/dist/server/executor.js +41 -7
- package/dist/server/kubernetes-backend.js +7 -4
- package/dist/server/run-request.js +11 -0
- package/dist/server/run-worker.js +145 -6
- package/dist/server/runs.js +20 -0
- package/dist/server/sandbox-follow.js +54 -0
- package/dist/server/sandbox-storage.js +38 -1
- package/dist/server/sandbox-sync.js +357 -0
- package/dist/server/sandboxes.js +19 -0
- package/dist/server/server.js +39 -7
- package/package.json +1 -1
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SandboxSync = exports.unsafePath = exports.nextRetryMs = exports.MAX_BATCH_FILES = exports.MAX_BATCH_BYTES = void 0;
|
|
4
|
+
const fs_1 = require("fs");
|
|
5
|
+
const promises_1 = require("fs/promises");
|
|
6
|
+
const path_1 = require("path");
|
|
7
|
+
const promises_2 = require("stream/promises");
|
|
8
|
+
const log_1 = require("../log");
|
|
9
|
+
// the most bytes of files one batch stages; a larger change is not synced, and the log says so
|
|
10
|
+
exports.MAX_BATCH_BYTES = 128 * 1024 * 1024;
|
|
11
|
+
// the most files one batch stages
|
|
12
|
+
exports.MAX_BATCH_FILES = 10000;
|
|
13
|
+
// how deep a walk of the files of a sandbox goes (the API takes at most this)
|
|
14
|
+
const WALK_DEPTH = 64;
|
|
15
|
+
// how long a sync waits before it is tried again while a run has a batch its worker did not
|
|
16
|
+
// apply: the first wait, doubled on each try up to RETRY_MAX_MS
|
|
17
|
+
const RETRY_MS = 500;
|
|
18
|
+
const RETRY_MAX_MS = 10000;
|
|
19
|
+
// a batch staged that long ago and still not applied is stuck: it is removed, and sent again
|
|
20
|
+
const PENDING_MAX_MS = 5 * 60 * 1000;
|
|
21
|
+
/**
|
|
22
|
+
* How long the next retry of a sync waits, after one that waited `previous` ms (undefined for
|
|
23
|
+
* the first): `base` at first, then doubled up to RETRY_MAX_MS.
|
|
24
|
+
*/
|
|
25
|
+
function nextRetryMs(previous, base) {
|
|
26
|
+
return previous === undefined ? Math.min(base, RETRY_MAX_MS) : Math.min(previous * 2, RETRY_MAX_MS);
|
|
27
|
+
}
|
|
28
|
+
exports.nextRetryMs = nextRetryMs;
|
|
29
|
+
// a batch with more than MAX_BATCH_BYTES or MAX_BATCH_FILES is not staged at all
|
|
30
|
+
class BatchTooLargeError extends Error {
|
|
31
|
+
constructor() {
|
|
32
|
+
super(`the changes are more than ${exports.MAX_BATCH_FILES} files or ${exports.MAX_BATCH_BYTES} bytes, the services keep their files`);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Why a path of the sandbox can not be a path of a service's folder, or none. A name with a
|
|
37
|
+
* control character (a newline in particular), an absolute path or a `.` or `..` segment is refused.
|
|
38
|
+
*/
|
|
39
|
+
function unsafePath(path) {
|
|
40
|
+
if (path === '') {
|
|
41
|
+
return 'it is empty';
|
|
42
|
+
}
|
|
43
|
+
if ([...path].some((char) => char.charCodeAt(0) < 0x20 || char.charCodeAt(0) === 0x7f)) {
|
|
44
|
+
return 'it has a control character';
|
|
45
|
+
}
|
|
46
|
+
if (path.startsWith('/') || /^[A-Za-z]:/.test(path)) {
|
|
47
|
+
return 'it is not relative to the sandbox';
|
|
48
|
+
}
|
|
49
|
+
if (path.split('/').some((segment) => segment === '..' || segment === '.')) {
|
|
50
|
+
return 'it has a . or .. segment';
|
|
51
|
+
}
|
|
52
|
+
return undefined;
|
|
53
|
+
}
|
|
54
|
+
exports.unsafePath = unsafePath;
|
|
55
|
+
/**
|
|
56
|
+
* Keeps the services of the service runs on a sandbox in step with its files. Each change of the
|
|
57
|
+
* sandbox (batched by SandboxFollow) goes to each run that is up, as the files that changed: the
|
|
58
|
+
* ones that differ from the commit now, and the ones that differed when the services last got
|
|
59
|
+
* their files, which may be back at the commit by now. A run without that record (the server
|
|
60
|
+
* started since it did) gets every file of the sandbox once. Nothing else of the services is
|
|
61
|
+
* touched, and nothing is restarted here.
|
|
62
|
+
*/
|
|
63
|
+
class SandboxSync {
|
|
64
|
+
constructor(options) {
|
|
65
|
+
this.options = options;
|
|
66
|
+
// per run, the paths that may differ from the commit since the services last got the files
|
|
67
|
+
this.differing = new Map();
|
|
68
|
+
// the batches of each sandbox, one after another
|
|
69
|
+
this.queues = new Map();
|
|
70
|
+
// the batch of each sandbox that waits to start: it reads the files when it starts, so a
|
|
71
|
+
// change that comes meanwhile is in it too
|
|
72
|
+
this.waiting = new Map();
|
|
73
|
+
this.batch = 0;
|
|
74
|
+
// the retry of a sandbox's sync that waits for a batch a run did not apply yet, and how long
|
|
75
|
+
// the last one waited
|
|
76
|
+
this.retries = new Map();
|
|
77
|
+
this.retryDelays = new Map();
|
|
78
|
+
// the runs that were told their changed files are over the cap, once each
|
|
79
|
+
this.overCap = new Set();
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* A service run starts from a copy of the files now: what differs from the commit in the copy
|
|
83
|
+
* is what the services have from the start, and what a later change has to include.
|
|
84
|
+
*/
|
|
85
|
+
async started(run) {
|
|
86
|
+
if (run.source.type !== 'sandbox') {
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
this.differing.set(run.id, await this.changedOf(this.options.files(run.owner, run.source.id)));
|
|
90
|
+
}
|
|
91
|
+
// Stops the retries of the syncs that wait for a batch to be applied: nothing is synced after it
|
|
92
|
+
close() {
|
|
93
|
+
for (const timer of this.retries.values()) {
|
|
94
|
+
clearTimeout(timer);
|
|
95
|
+
}
|
|
96
|
+
this.retries.clear();
|
|
97
|
+
this.retryDelays.clear();
|
|
98
|
+
}
|
|
99
|
+
// A run that ended: nothing is synced for it any more, and what was staged for it goes
|
|
100
|
+
async forget(run) {
|
|
101
|
+
this.differing.delete(run);
|
|
102
|
+
for (const key of [...this.overCap]) {
|
|
103
|
+
if (key.startsWith(`${run}/`)) {
|
|
104
|
+
this.overCap.delete(key);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
await this.remove((0, path_1.join)(this.options.stagingRoot, run));
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* At server start: what a server that stopped staged is no batch of any run now, so the
|
|
111
|
+
* staging folder goes.
|
|
112
|
+
*/
|
|
113
|
+
async sweep() {
|
|
114
|
+
await this.remove(this.options.stagingRoot);
|
|
115
|
+
}
|
|
116
|
+
// The batch of changes of a sandbox, after the ones before it. One that waits already has the
|
|
117
|
+
// newest files when it starts, so a second one is not queued behind it.
|
|
118
|
+
follow(sandbox) {
|
|
119
|
+
const waiting = this.waiting.get(sandbox);
|
|
120
|
+
if (waiting) {
|
|
121
|
+
return waiting;
|
|
122
|
+
}
|
|
123
|
+
const previous = this.queues.get(sandbox) ?? Promise.resolve();
|
|
124
|
+
const next = previous
|
|
125
|
+
.then(() => {
|
|
126
|
+
this.waiting.delete(sandbox);
|
|
127
|
+
return this.sync(sandbox);
|
|
128
|
+
})
|
|
129
|
+
.catch((e) => this.log(`sync sandbox ${sandbox}: ${(0, log_1.getErrorMessage)(e)}`));
|
|
130
|
+
this.waiting.set(sandbox, next);
|
|
131
|
+
this.queues.set(sandbox, next);
|
|
132
|
+
void next.then(() => {
|
|
133
|
+
if (this.queues.get(sandbox) === next) {
|
|
134
|
+
this.queues.delete(sandbox);
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
return next;
|
|
138
|
+
}
|
|
139
|
+
async sync(sandbox) {
|
|
140
|
+
const runs = this.options.upOn(sandbox);
|
|
141
|
+
if (runs.length === 0) {
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
// the files of a sandbox are one owner's: a run of another owner would read them as theirs
|
|
145
|
+
const [{ owner }] = runs;
|
|
146
|
+
if (runs.some((run) => run.owner.iss !== owner.iss || run.owner.sub !== owner.sub)) {
|
|
147
|
+
throw new Error(`the service runs on ${sandbox} have more than one owner, their changes are not synced`);
|
|
148
|
+
}
|
|
149
|
+
const files = this.options.files(owner, sandbox);
|
|
150
|
+
const changed = await this.changedOf(files);
|
|
151
|
+
for (const run of runs) {
|
|
152
|
+
const known = this.differing.get(run.id);
|
|
153
|
+
if (await this.pending(run.id)) {
|
|
154
|
+
// the worker has not applied the last batch of the run: these paths go with the next one
|
|
155
|
+
if (known) {
|
|
156
|
+
this.differing.set(run.id, new Set([...known, ...changed]));
|
|
157
|
+
}
|
|
158
|
+
this.retryLater(sandbox);
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
await this.sendTo(run, sandbox, files, known, changed);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Whether the worker of a run has not applied a batch yet: the worker removes a batch once it
|
|
166
|
+
* applied it. A batch staged longer than PENDING_MAX_MS ago is stuck (a worker that failed to
|
|
167
|
+
* remove it): it is removed here, and the changes are sent again with a fresh batch.
|
|
168
|
+
*/
|
|
169
|
+
async pending(run) {
|
|
170
|
+
const folder = (0, path_1.join)(this.options.stagingRoot, run);
|
|
171
|
+
let pending = false;
|
|
172
|
+
for (const batch of await (0, promises_1.readdir)(folder).catch(() => [])) {
|
|
173
|
+
const path = (0, path_1.join)(folder, batch);
|
|
174
|
+
const modified = await (0, promises_1.stat)(path).then((found) => found.mtimeMs, () => Date.now());
|
|
175
|
+
if (Date.now() - modified > PENDING_MAX_MS) {
|
|
176
|
+
this.log(`the batch ${batch} of ${run} was not applied in five minutes, it is removed and sent again`);
|
|
177
|
+
await this.remove(path);
|
|
178
|
+
}
|
|
179
|
+
else {
|
|
180
|
+
pending = true;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
return pending;
|
|
184
|
+
}
|
|
185
|
+
// Tries the sync of a sandbox again later, once: the batch of a run that is pending goes then.
|
|
186
|
+
// Each try waits longer than the one before, up to RETRY_MAX_MS, until a batch is sent.
|
|
187
|
+
retryLater(sandbox) {
|
|
188
|
+
if (this.retries.has(sandbox)) {
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
const delay = nextRetryMs(this.retryDelays.get(sandbox), this.options.retryMs ?? RETRY_MS);
|
|
192
|
+
this.retryDelays.set(sandbox, delay);
|
|
193
|
+
const timer = setTimeout(() => {
|
|
194
|
+
this.retries.delete(sandbox);
|
|
195
|
+
void this.follow(sandbox);
|
|
196
|
+
}, delay);
|
|
197
|
+
timer.unref?.();
|
|
198
|
+
this.retries.set(sandbox, timer);
|
|
199
|
+
}
|
|
200
|
+
// Says once per run and kind that its changed files are over the cap
|
|
201
|
+
logOverCap(run, kind, line) {
|
|
202
|
+
const key = `${run}/${kind}`;
|
|
203
|
+
if (!this.overCap.has(key)) {
|
|
204
|
+
this.overCap.add(key);
|
|
205
|
+
this.log(line);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Stages and sends the batch of one run. Without a record of what the services have, every
|
|
210
|
+
* file of the sandbox goes. A batch over the caps is not staged: then only the changed paths
|
|
211
|
+
* go, which may leave a file reverted to the commit stale, and they are the record from now.
|
|
212
|
+
*/
|
|
213
|
+
async sendTo(run, sandbox, files, known, changed) {
|
|
214
|
+
// no record of what the services have: every file may differ from what they got
|
|
215
|
+
let paths = known ? new Set([...known, ...changed]) : new Set([...(await this.allFiles(files)), ...changed]);
|
|
216
|
+
let changes;
|
|
217
|
+
try {
|
|
218
|
+
changes = await this.stage(run.id, files, [...paths]);
|
|
219
|
+
}
|
|
220
|
+
catch (e) {
|
|
221
|
+
if (!(e instanceof BatchTooLargeError)) {
|
|
222
|
+
this.log(`the changes of ${sandbox} are not synced to ${run.id}: ${(0, log_1.getErrorMessage)(e)}`);
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
this.logOverCap(run.id, 'more', `the files of ${sandbox} are more than a batch takes, ${run.id} gets the changed files only: a file reverted to the commit may be stale`);
|
|
226
|
+
paths = changed;
|
|
227
|
+
try {
|
|
228
|
+
changes = await this.stage(run.id, files, [...paths]);
|
|
229
|
+
}
|
|
230
|
+
catch (again) {
|
|
231
|
+
if (!(again instanceof BatchTooLargeError)) {
|
|
232
|
+
this.log(`the changes of ${sandbox} are not synced to ${run.id}: ${(0, log_1.getErrorMessage)(again)}`);
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
// the changed files alone are over the cap: they are the record from now, and nothing is sent
|
|
236
|
+
this.logOverCap(run.id, 'alone', `the changed files of ${sandbox} alone are more than a batch takes: ${run.id} gets none of them, they are its baseline`);
|
|
237
|
+
this.differing.set(run.id, changed);
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
if (!changes) {
|
|
242
|
+
this.differing.set(run.id, changed);
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
if (this.options.send(run.id, changes)) {
|
|
246
|
+
this.differing.set(run.id, changed);
|
|
247
|
+
// a batch went: the next wait starts short again
|
|
248
|
+
this.retryDelays.delete(sandbox);
|
|
249
|
+
}
|
|
250
|
+
else {
|
|
251
|
+
// the services did not take them: the next batch has them still
|
|
252
|
+
this.differing.set(run.id, paths);
|
|
253
|
+
await this.remove((0, path_1.dirname)(changes.dir));
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
log(line) {
|
|
257
|
+
this.options.log?.(line);
|
|
258
|
+
}
|
|
259
|
+
async remove(path) {
|
|
260
|
+
await (0, promises_1.rm)(path, { recursive: true, force: true }).catch((e) => this.log(`the staged changes in ${path} could not be removed: ${(0, log_1.getErrorMessage)(e)}`));
|
|
261
|
+
}
|
|
262
|
+
async changedOf(files) {
|
|
263
|
+
const paths = new Set();
|
|
264
|
+
for (const change of await files.changes()) {
|
|
265
|
+
paths.add(change.path);
|
|
266
|
+
if (change.from) {
|
|
267
|
+
paths.add(change.from);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
return paths;
|
|
271
|
+
}
|
|
272
|
+
// every file of the sandbox, page by page
|
|
273
|
+
async allFiles(files) {
|
|
274
|
+
const paths = [];
|
|
275
|
+
let cursor;
|
|
276
|
+
do {
|
|
277
|
+
const page = await files.tree({ depth: WALK_DEPTH, limit: 1000, cursor });
|
|
278
|
+
for (const entry of page.entries) {
|
|
279
|
+
if (entry.type === 'file') {
|
|
280
|
+
paths.push(entry.path);
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
cursor = page.truncated && page.cursor ? page.cursor : undefined;
|
|
284
|
+
} while (cursor);
|
|
285
|
+
return paths;
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* Writes the files of `paths` that are there now into a folder of the batch, and says which
|
|
289
|
+
* paths are gone. A path that is unsafe, or whose file can not be read, is skipped and logged;
|
|
290
|
+
* the rest is synced. Undefined when none of the paths is left to say. Throws when the batch is
|
|
291
|
+
* too large, before anything is written: the sizes are summed first.
|
|
292
|
+
*/
|
|
293
|
+
async stage(run, files, paths) {
|
|
294
|
+
const writes = [];
|
|
295
|
+
const deleted = [];
|
|
296
|
+
let bytes = 0;
|
|
297
|
+
for (const path of paths) {
|
|
298
|
+
const problem = unsafePath(path);
|
|
299
|
+
if (problem) {
|
|
300
|
+
this.log(`skipped ${JSON.stringify(path)} of the sandbox: ${problem}`);
|
|
301
|
+
continue;
|
|
302
|
+
}
|
|
303
|
+
try {
|
|
304
|
+
const stat = await files.stat(path);
|
|
305
|
+
if (stat?.type !== 'file') {
|
|
306
|
+
deleted.push(path);
|
|
307
|
+
continue;
|
|
308
|
+
}
|
|
309
|
+
bytes += stat.size;
|
|
310
|
+
writes.push({ path, exec: stat.exec });
|
|
311
|
+
}
|
|
312
|
+
catch (e) {
|
|
313
|
+
this.log(`skipped ${JSON.stringify(path)} of the sandbox: ${(0, log_1.getErrorMessage)(e)}`);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
if (writes.length > exports.MAX_BATCH_FILES || bytes > exports.MAX_BATCH_BYTES) {
|
|
317
|
+
throw new BatchTooLargeError();
|
|
318
|
+
}
|
|
319
|
+
const folder = (0, path_1.join)(this.options.stagingRoot, run, `${++this.batch}`);
|
|
320
|
+
const dir = (0, path_1.join)(folder, 'files');
|
|
321
|
+
const written = [];
|
|
322
|
+
try {
|
|
323
|
+
for (const { path, exec } of writes) {
|
|
324
|
+
try {
|
|
325
|
+
const raw = await files.raw(path);
|
|
326
|
+
const target = (0, path_1.join)(dir, path);
|
|
327
|
+
await (0, promises_1.mkdir)((0, path_1.dirname)(target), { recursive: true });
|
|
328
|
+
if (raw.content) {
|
|
329
|
+
await (0, promises_1.writeFile)(target, raw.content);
|
|
330
|
+
}
|
|
331
|
+
else if (raw.stream) {
|
|
332
|
+
await (0, promises_2.pipeline)(raw.stream, (0, fs_1.createWriteStream)(target));
|
|
333
|
+
}
|
|
334
|
+
// the exec bit is kept: a script the services run is still executable after the sync
|
|
335
|
+
await (0, promises_1.chmod)(target, exec ? 0o755 : 0o644);
|
|
336
|
+
written.push(path);
|
|
337
|
+
}
|
|
338
|
+
catch (e) {
|
|
339
|
+
// a file that changed or can not be read: the next batch brings it
|
|
340
|
+
this.log(`skipped ${JSON.stringify(path)} of the sandbox: ${(0, log_1.getErrorMessage)(e)}`);
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
if (written.length + deleted.length === 0) {
|
|
344
|
+
await this.remove(folder);
|
|
345
|
+
return undefined;
|
|
346
|
+
}
|
|
347
|
+
await (0, promises_1.mkdir)(dir, { recursive: true });
|
|
348
|
+
return { dir, written, deleted, restart: false };
|
|
349
|
+
}
|
|
350
|
+
catch (e) {
|
|
351
|
+
await this.remove(folder);
|
|
352
|
+
throw e;
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
exports.SandboxSync = SandboxSync;
|
|
357
|
+
//# sourceMappingURL=sandbox-sync.js.map
|
package/dist/server/sandboxes.js
CHANGED
|
@@ -9,6 +9,7 @@ const git_checkout_1 = require("./git-checkout");
|
|
|
9
9
|
const oauth_1 = require("./oauth");
|
|
10
10
|
const runs_1 = require("./runs");
|
|
11
11
|
const repo_1 = require("./repo");
|
|
12
|
+
const sandbox_files_1 = require("./sandbox-files");
|
|
12
13
|
const sandbox_storage_1 = require("./sandbox-storage");
|
|
13
14
|
// How long creating a sandbox or moving it to another commit may take
|
|
14
15
|
const FETCH_TIMEOUT_MS = 10 * 60 * 1000;
|
|
@@ -40,6 +41,8 @@ class SandboxRegistry {
|
|
|
40
41
|
// the measures of runs that are over, which a run's result does not wait for
|
|
41
42
|
this.measuring = new Map();
|
|
42
43
|
this.locks = new Map();
|
|
44
|
+
// what hears of the files of a sandbox changing: an edit, or a run that wrote to them
|
|
45
|
+
this.listeners = new Set();
|
|
43
46
|
this.runs = { holder: () => undefined, stop: async () => undefined };
|
|
44
47
|
this.now = options.now ?? Date.now;
|
|
45
48
|
}
|
|
@@ -47,6 +50,16 @@ class SandboxRegistry {
|
|
|
47
50
|
useRuns(runs) {
|
|
48
51
|
this.runs = runs;
|
|
49
52
|
}
|
|
53
|
+
// Calls `listener` with the id of a sandbox whose files may have changed: after each edit,
|
|
54
|
+
// and after each run that wrote to them (measure). It may come when nothing changed.
|
|
55
|
+
onChange(listener) {
|
|
56
|
+
this.listeners.add(listener);
|
|
57
|
+
}
|
|
58
|
+
changed(id) {
|
|
59
|
+
for (const listener of this.listeners) {
|
|
60
|
+
listener(id);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
50
63
|
get limits() {
|
|
51
64
|
return this.options.limits;
|
|
52
65
|
}
|
|
@@ -261,6 +274,10 @@ class SandboxRegistry {
|
|
|
261
274
|
});
|
|
262
275
|
}
|
|
263
276
|
catch (e) {
|
|
277
|
+
// a file of the sandbox in the way of the ref, which a checkout would overwrite
|
|
278
|
+
if (e instanceof sandbox_files_1.SandboxFileError && e.status === 409) {
|
|
279
|
+
throw new SandboxError(409, repos.scrub(e, credentials.values).message);
|
|
280
|
+
}
|
|
264
281
|
throw new SandboxError(502, repos.scrub(e, credentials.values).message);
|
|
265
282
|
}
|
|
266
283
|
sandbox.ref = ref;
|
|
@@ -455,6 +472,7 @@ class SandboxRegistry {
|
|
|
455
472
|
sandbox.bytes = Math.max(0, sandbox.bytes + plan.bytes);
|
|
456
473
|
sandbox.lastUsedAt = this.timestamp();
|
|
457
474
|
await this.save(sandbox);
|
|
475
|
+
this.changed(id);
|
|
458
476
|
return plan.files;
|
|
459
477
|
});
|
|
460
478
|
}
|
|
@@ -488,6 +506,7 @@ class SandboxRegistry {
|
|
|
488
506
|
}
|
|
489
507
|
sandbox.bytes = await this.options.storage.usage(keyOf(sandbox));
|
|
490
508
|
await this.save(sandbox);
|
|
509
|
+
this.changed(id);
|
|
491
510
|
});
|
|
492
511
|
const settled = measure.catch(() => undefined);
|
|
493
512
|
this.measuring.set(id, settled);
|
package/dist/server/server.js
CHANGED
|
@@ -35,6 +35,8 @@ const proposals_api_1 = require("./proposals-api");
|
|
|
35
35
|
const proposals_1 = require("./proposals");
|
|
36
36
|
const sources_1 = require("./sources");
|
|
37
37
|
const sources_api_1 = require("./sources-api");
|
|
38
|
+
const sandbox_follow_1 = require("./sandbox-follow");
|
|
39
|
+
const sandbox_sync_1 = require("./sandbox-sync");
|
|
38
40
|
const sandboxes_1 = require("./sandboxes");
|
|
39
41
|
const sandbox_storage_1 = require("./sandbox-storage");
|
|
40
42
|
const kubernetes_sandbox_storage_1 = require("./kubernetes-sandbox-storage");
|
|
@@ -480,6 +482,14 @@ async function startRun(config, registry, definitions, sources, sandboxes, plann
|
|
|
480
482
|
(0, http_2.send)(res, 400, { error: 'sleepTtl: a service run from a sandbox does not sleep, start it from a branch' });
|
|
481
483
|
return;
|
|
482
484
|
}
|
|
485
|
+
// only a Kubernetes backend syncs the changed files of a sandbox into the services; a host
|
|
486
|
+
// runs them on the sandbox's own files, so a restart would do nothing there
|
|
487
|
+
if (request.restart?.length && config.backend.type !== 'kubernetes') {
|
|
488
|
+
(0, http_2.send)(res, 400, {
|
|
489
|
+
error: `restart: this server (backend ${config.backend.type}) has no file sync, a host's services run on the sandbox's files`,
|
|
490
|
+
});
|
|
491
|
+
return;
|
|
492
|
+
}
|
|
483
493
|
const sleepTtlText = request.source.type === 'ref' ? request.sleepTtl ?? config.runs.up.sleepTtl : undefined;
|
|
484
494
|
const sleepTtl = request.services.length > 0 && sleepTtlText ? (0, units_1.parseDuration)(sleepTtlText) : undefined;
|
|
485
495
|
const argued = (request.options.args ?? []).length > 0 ? request.tasks[0] : undefined;
|
|
@@ -503,12 +513,6 @@ async function startRun(config, registry, definitions, sources, sandboxes, plann
|
|
|
503
513
|
return;
|
|
504
514
|
}
|
|
505
515
|
}
|
|
506
|
-
if (request.services.length > 0 && request.source.type === 'sandbox' && config.backend.type === 'kubernetes') {
|
|
507
|
-
(0, http_2.send)(res, 400, {
|
|
508
|
-
error: 'on a cluster a service run starts from a branch, push the sandbox and start it from there',
|
|
509
|
-
});
|
|
510
|
-
return;
|
|
511
|
-
}
|
|
512
516
|
const sandbox = request.source.type === 'sandbox' ? sandboxes.registry.get(principal, request.source.id) : undefined;
|
|
513
517
|
if (request.source.type === 'sandbox') {
|
|
514
518
|
if (!sandbox) {
|
|
@@ -659,6 +663,7 @@ async function startRun(config, registry, definitions, sources, sandboxes, plann
|
|
|
659
663
|
...(sleepTtl ? { sleepTtl } : {}),
|
|
660
664
|
name: serviceName,
|
|
661
665
|
endpoints: (0, service_runs_1.toRunEndpoints)(request.endpoints, (0, service_runs_1.getEndpointHosts)(serviceName, principal, request.endpoints), endpointUrl),
|
|
666
|
+
...(request.restart?.length ? { restart: request.restart } : {}),
|
|
662
667
|
}
|
|
663
668
|
: undefined;
|
|
664
669
|
// a run that may sleep keeps what its owner is entitled by, for the check of a wake
|
|
@@ -1001,6 +1006,18 @@ async function startServer(config, environment, options = {}) {
|
|
|
1001
1006
|
}
|
|
1002
1007
|
// a local task of a host runs as the user of this server: what it can write is never served
|
|
1003
1008
|
const localTasksOnHost = config.backend.type === 'host' && config.runtimes.includes('local');
|
|
1009
|
+
// a service run from a sandbox on a cluster gets the changed files of the sandbox into its
|
|
1010
|
+
// services, batched (SandboxFollow), as the changes that differ from the commit
|
|
1011
|
+
const sandboxSyncStaging = (0, path_1.join)(storage, 'sandboxes', 'sync');
|
|
1012
|
+
const sandboxSync = new sandbox_sync_1.SandboxSync({
|
|
1013
|
+
files: (owner, id) => sandboxes.registry.files(owner, id),
|
|
1014
|
+
upOn: (id) => registry.upOn(id),
|
|
1015
|
+
send: (run, changes) => registry.syncFiles(run, changes),
|
|
1016
|
+
stagingRoot: sandboxSyncStaging,
|
|
1017
|
+
log,
|
|
1018
|
+
});
|
|
1019
|
+
// what a server that stopped staged is no batch of any run now
|
|
1020
|
+
await sandboxSync.sweep();
|
|
1004
1021
|
const executorOptions = {
|
|
1005
1022
|
definitions,
|
|
1006
1023
|
planning,
|
|
@@ -1008,7 +1025,13 @@ async function startServer(config, environment, options = {}) {
|
|
|
1008
1025
|
directory: (0, path_1.join)(storage, 'runs'),
|
|
1009
1026
|
cache: (0, path_1.join)(storage, 'cache'),
|
|
1010
1027
|
worker: options.worker,
|
|
1011
|
-
sandboxes: {
|
|
1028
|
+
sandboxes: {
|
|
1029
|
+
...sandboxes,
|
|
1030
|
+
network: config.sandboxes.network,
|
|
1031
|
+
started: async (run) => sandboxSync.started(run),
|
|
1032
|
+
ended: async (run) => sandboxSync.forget(run.id),
|
|
1033
|
+
staging: sandboxSyncStaging,
|
|
1034
|
+
},
|
|
1012
1035
|
localTasksOnHost,
|
|
1013
1036
|
...(config.backend.type === 'host' && config.backend.capabilities
|
|
1014
1037
|
? { capabilityRuntime: config.backend.capabilities.runtime }
|
|
@@ -1132,6 +1155,13 @@ async function startServer(config, environment, options = {}) {
|
|
|
1132
1155
|
holder: (id) => registry.holder(id),
|
|
1133
1156
|
stop: (id) => registry.stopSandbox(id, 'the sandbox was deleted'),
|
|
1134
1157
|
});
|
|
1158
|
+
// A service run from a sandbox follows the files of the sandbox: a burst of edits or runs that
|
|
1159
|
+
// wrote is synced once, when the sandbox has been quiet for a moment (SandboxFollow)
|
|
1160
|
+
const follower = new sandbox_follow_1.SandboxFollow({
|
|
1161
|
+
follow: (id) => sandboxSync.follow(id),
|
|
1162
|
+
log: (line) => log(`follow ${line}`),
|
|
1163
|
+
});
|
|
1164
|
+
sandboxes.registry.onChange((id) => follower.changed(id));
|
|
1135
1165
|
const listener = (req, res) => {
|
|
1136
1166
|
handle(api, verifier, req, res).catch((e) => {
|
|
1137
1167
|
if (e instanceof http_2.HttpError) {
|
|
@@ -1227,6 +1257,8 @@ async function startServer(config, environment, options = {}) {
|
|
|
1227
1257
|
clearInterval(sweeping);
|
|
1228
1258
|
clearInterval(sweepingArtifacts);
|
|
1229
1259
|
proposals.close();
|
|
1260
|
+
follower.close();
|
|
1261
|
+
sandboxSync.close();
|
|
1230
1262
|
await registry.close();
|
|
1231
1263
|
await new Promise((resolve, reject) => {
|
|
1232
1264
|
server.close((err) => (err ? reject(err) : resolve()));
|
package/package.json
CHANGED