@context-use/open-sync 0.3.1 → 0.4.0
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 +5 -2
- package/src/cron.ts +2 -0
- package/src/execution/cron-client.ts +47 -0
- package/src/execution/cron-invocation.ts +23 -0
- package/src/execution/cron.ts +137 -0
- package/src/execution/schedule.ts +57 -0
- package/src/execution/worker.ts +47 -54
- package/src/open-sync.ts +7 -1
- package/src/repositories/acquisition/contract.ts +0 -1
- package/src/repositories/acquisition/sqlite.ts +0 -8
- package/src/repositories/delivery/contract.ts +0 -1
- package/src/repositories/delivery/sqlite.ts +0 -9
- package/src/runtime.ts +32 -2
- package/src/services/acquisition.ts +0 -3
- package/src/services/delivery.ts +0 -3
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@context-use/open-sync",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": "./src/open-sync.ts",
|
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
"./delivery": "./src/models/delivery.ts",
|
|
10
10
|
"./json": "./src/models/json.ts",
|
|
11
11
|
"./http": "./src/http/index.ts",
|
|
12
|
+
"./cron": "./src/cron.ts",
|
|
12
13
|
"./engine": "./src/runtime.ts",
|
|
13
14
|
"./build": "./src/build.ts",
|
|
14
15
|
"./assets": "./src/models/asset.ts"
|
|
@@ -25,8 +26,10 @@
|
|
|
25
26
|
"dependencies": {
|
|
26
27
|
"@cfworker/json-schema": "4.1.1",
|
|
27
28
|
"@oomol-lab/open-connector": "1.7.0",
|
|
29
|
+
"@types/proper-lockfile": "4.1.4",
|
|
28
30
|
"elysia": "^1.4.30",
|
|
29
|
-
"minisearch": "^7.2.0"
|
|
31
|
+
"minisearch": "^7.2.0",
|
|
32
|
+
"proper-lockfile": "4.1.2"
|
|
30
33
|
},
|
|
31
34
|
"devDependencies": {
|
|
32
35
|
"typescript": "6.0.3"
|
package/src/cron.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { request } from 'node:http';
|
|
2
|
+
|
|
3
|
+
const STARTUP_TIMEOUT_MS = 30_000;
|
|
4
|
+
const RETRY_INTERVAL_MS = 100;
|
|
5
|
+
const HTTP_OK = 200;
|
|
6
|
+
|
|
7
|
+
/** Wait for the existing worker to finish, retrying only while its socket is unavailable. */
|
|
8
|
+
export default async function requestCron(socketPath: string): Promise<void> {
|
|
9
|
+
const deadline = Date.now() + STARTUP_TIMEOUT_MS;
|
|
10
|
+
for (;;) {
|
|
11
|
+
try {
|
|
12
|
+
await run(socketPath);
|
|
13
|
+
return;
|
|
14
|
+
} catch (error) {
|
|
15
|
+
const unavailable =
|
|
16
|
+
error instanceof Error &&
|
|
17
|
+
'code' in error &&
|
|
18
|
+
(error.code === 'ECONNREFUSED' || error.code === 'ENOENT');
|
|
19
|
+
if (!unavailable || Date.now() >= deadline) {
|
|
20
|
+
throw error;
|
|
21
|
+
}
|
|
22
|
+
await Bun.sleep(RETRY_INTERVAL_MS);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function run(socketPath: string): Promise<void> {
|
|
28
|
+
// biome-ignore lint/complexity/useMaxParams: Native Promise executor signature.
|
|
29
|
+
return new Promise((resolve, reject) => {
|
|
30
|
+
const client = request(
|
|
31
|
+
{ socketPath, path: '/run', method: 'POST', agent: false },
|
|
32
|
+
(response) => {
|
|
33
|
+
response.on('error', reject);
|
|
34
|
+
response.on('end', () => {
|
|
35
|
+
if (response.statusCode === HTTP_OK) {
|
|
36
|
+
resolve();
|
|
37
|
+
} else {
|
|
38
|
+
reject(new Error(`Open Sync cron failed: ${response.statusCode}`));
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
response.resume();
|
|
42
|
+
},
|
|
43
|
+
);
|
|
44
|
+
client.on('error', reject);
|
|
45
|
+
client.end();
|
|
46
|
+
});
|
|
47
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import requestCron from './cron-client';
|
|
2
|
+
|
|
3
|
+
const TITLE_PREFIX = 'open-sync-';
|
|
4
|
+
|
|
5
|
+
/** Handle Bun's compiled cron invocation before loading application configuration or services. */
|
|
6
|
+
export async function runOpenSyncCron(): Promise<boolean> {
|
|
7
|
+
const argument = process.argv.find((value) => value.startsWith(`--cron-title=${TITLE_PREFIX}`));
|
|
8
|
+
if (!argument) {
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
|
+
const encoded = argument.slice(`--cron-title=${TITLE_PREFIX}`.length);
|
|
12
|
+
const socket = Buffer.from(encoded, 'base64url').toString();
|
|
13
|
+
if (!socket.startsWith('/') || Buffer.from(socket).toString('base64url') !== encoded) {
|
|
14
|
+
throw new Error('Invalid Open Sync cron invocation.');
|
|
15
|
+
}
|
|
16
|
+
await requestCron(socket);
|
|
17
|
+
return true;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function cronTitle(socket: string): string {
|
|
21
|
+
// The command must find its worker even when Unix cron supplies a different cwd/environment.
|
|
22
|
+
return `${TITLE_PREFIX}${Buffer.from(socket).toString('base64url')}`;
|
|
23
|
+
}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { chmod, lstat, mkdir, unlink, writeFile } from 'node:fs/promises';
|
|
2
|
+
import { request } from 'node:http';
|
|
3
|
+
import { join, resolve } from 'node:path';
|
|
4
|
+
import { cronTitle } from './cron-invocation';
|
|
5
|
+
import { updateSchedule } from './schedule';
|
|
6
|
+
|
|
7
|
+
const PRIVATE_DIRECTORY_MODE = 0o700;
|
|
8
|
+
const PRIVATE_SOCKET_MODE = 0o600;
|
|
9
|
+
const HTTP_OK = 200;
|
|
10
|
+
const HTTP_NOT_FOUND = 404;
|
|
11
|
+
const HTTP_UNAVAILABLE = 503;
|
|
12
|
+
const PROBE_TIMEOUT_MS = 1_000;
|
|
13
|
+
|
|
14
|
+
/** One half-hourly check for every sync owned by this runtime. */
|
|
15
|
+
export async function startCron(input: {
|
|
16
|
+
runtime: { runDue(): Promise<void> };
|
|
17
|
+
/** A private, stable directory owned by this runtime. */
|
|
18
|
+
directory: string;
|
|
19
|
+
/** Report a failed run; the recurring job remains registered for the next invocation. */
|
|
20
|
+
onError(error: unknown): void;
|
|
21
|
+
}) {
|
|
22
|
+
const directory = resolve(input.directory);
|
|
23
|
+
await mkdir(directory, { recursive: true, mode: PRIVATE_DIRECTORY_MODE });
|
|
24
|
+
await chmod(directory, PRIVATE_DIRECTORY_MODE);
|
|
25
|
+
const socket = join(directory, 'worker.sock');
|
|
26
|
+
await removeStaleSocket(socket);
|
|
27
|
+
const title = cronTitle(socket);
|
|
28
|
+
let entrypoint = Bun.main;
|
|
29
|
+
if (!Bun.isStandaloneExecutable) {
|
|
30
|
+
// Bun consumes cron arguments in source mode and invokes default.scheduled().
|
|
31
|
+
// Only the absolute import and socket path are generated; the client remains ordinary code.
|
|
32
|
+
entrypoint = join(directory, 'scheduled.ts');
|
|
33
|
+
await writeFile(
|
|
34
|
+
entrypoint,
|
|
35
|
+
`import requestCron from ${JSON.stringify(import.meta.resolve('./cron-client.ts'))};\n` +
|
|
36
|
+
`export default { scheduled: () => requestCron(${JSON.stringify(socket)}) };\n`,
|
|
37
|
+
{ mode: PRIVATE_SOCKET_MODE },
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
let closed = false;
|
|
41
|
+
const server = Bun.serve({
|
|
42
|
+
unix: socket,
|
|
43
|
+
async fetch(request) {
|
|
44
|
+
this.timeout(request, 0);
|
|
45
|
+
const path = new URL(request.url).pathname;
|
|
46
|
+
if (path === '/health') {
|
|
47
|
+
return new Response(null, { status: HTTP_OK });
|
|
48
|
+
}
|
|
49
|
+
if (path !== '/run' || request.method !== 'POST') {
|
|
50
|
+
return new Response(null, { status: HTTP_NOT_FOUND });
|
|
51
|
+
}
|
|
52
|
+
if (closed) {
|
|
53
|
+
return new Response(null, { status: HTTP_UNAVAILABLE });
|
|
54
|
+
}
|
|
55
|
+
try {
|
|
56
|
+
await input.runtime.runDue();
|
|
57
|
+
return new Response(null, { status: HTTP_OK });
|
|
58
|
+
} catch (error) {
|
|
59
|
+
input.onError(error);
|
|
60
|
+
return new Response(null, { status: HTTP_UNAVAILABLE });
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
let closing: Promise<void> | undefined;
|
|
65
|
+
const close = () => {
|
|
66
|
+
closing ??= (async () => {
|
|
67
|
+
closed = true;
|
|
68
|
+
try {
|
|
69
|
+
await updateSchedule(() => Bun.cron.remove(title));
|
|
70
|
+
} finally {
|
|
71
|
+
await server.stop(true);
|
|
72
|
+
}
|
|
73
|
+
})();
|
|
74
|
+
return closing;
|
|
75
|
+
};
|
|
76
|
+
try {
|
|
77
|
+
await chmod(socket, PRIVATE_SOCKET_MODE);
|
|
78
|
+
await updateSchedule(() => Bun.cron(entrypoint, '*/30 * * * *', title));
|
|
79
|
+
return { close, [Symbol.asyncDispose]: close };
|
|
80
|
+
} catch (error) {
|
|
81
|
+
await server.stop(true);
|
|
82
|
+
throw error;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
async function removeStaleSocket(socket: string): Promise<void> {
|
|
87
|
+
const existing = await lstat(socket).catch((error: NodeJS.ErrnoException) => {
|
|
88
|
+
if (error.code !== 'ENOENT') {
|
|
89
|
+
throw error;
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
if (!existing) {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
if (!existing.isSocket()) {
|
|
96
|
+
throw new Error('The cron worker socket path is occupied by another file.');
|
|
97
|
+
}
|
|
98
|
+
try {
|
|
99
|
+
await probeWorker(socket);
|
|
100
|
+
} catch (error) {
|
|
101
|
+
if (unavailableSocket(error)) {
|
|
102
|
+
await unlink(socket);
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
throw error;
|
|
106
|
+
}
|
|
107
|
+
throw new Error('An Open Sync cron worker already owns this directory.');
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function unavailableSocket(error: unknown): boolean {
|
|
111
|
+
return (
|
|
112
|
+
error instanceof Error &&
|
|
113
|
+
'code' in error &&
|
|
114
|
+
['ECONNREFUSED', 'ENOENT'].includes(String(error.code))
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function probeWorker(socket: string): Promise<number> {
|
|
119
|
+
// biome-ignore lint/complexity/useMaxParams: Native Promise executor signature.
|
|
120
|
+
return new Promise((resolve, reject) => {
|
|
121
|
+
const client = request(
|
|
122
|
+
{
|
|
123
|
+
socketPath: socket,
|
|
124
|
+
path: '/health',
|
|
125
|
+
agent: false,
|
|
126
|
+
signal: AbortSignal.timeout(PROBE_TIMEOUT_MS),
|
|
127
|
+
},
|
|
128
|
+
(response) => {
|
|
129
|
+
response.on('error', reject);
|
|
130
|
+
response.on('end', () => resolve(response.statusCode!));
|
|
131
|
+
response.resume();
|
|
132
|
+
},
|
|
133
|
+
);
|
|
134
|
+
client.on('error', reject);
|
|
135
|
+
client.end();
|
|
136
|
+
});
|
|
137
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { tmpdir } from 'node:os';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { lock } from 'proper-lockfile';
|
|
4
|
+
|
|
5
|
+
const LOCK_STALE_MS = 30_000;
|
|
6
|
+
const LOCK_RETRIES = 40;
|
|
7
|
+
const LOCK_RETRY_MS = 1_000;
|
|
8
|
+
let updating = Promise.resolve();
|
|
9
|
+
|
|
10
|
+
/** Bun replaces the user's entire crontab; serialize changes across Open Sync hosts. */
|
|
11
|
+
export function updateSchedule(operation: () => Promise<void>): Promise<void> {
|
|
12
|
+
const pending = updating.then(async () => {
|
|
13
|
+
const release = await lock(join(tmpdir(), `open-sync-crontab-${process.getuid!()}`), {
|
|
14
|
+
realpath: false,
|
|
15
|
+
stale: LOCK_STALE_MS,
|
|
16
|
+
retries: { retries: LOCK_RETRIES, minTimeout: LOCK_RETRY_MS, maxTimeout: LOCK_RETRY_MS },
|
|
17
|
+
});
|
|
18
|
+
try {
|
|
19
|
+
if (process.platform === 'linux') {
|
|
20
|
+
await verifyCrontabReadable();
|
|
21
|
+
}
|
|
22
|
+
await operation();
|
|
23
|
+
} finally {
|
|
24
|
+
await release();
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
updating = pending.catch(() => undefined);
|
|
28
|
+
return pending;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Bun 1.4 treats every `crontab -l` exit 1 as an empty table. Reject permission/service errors
|
|
32
|
+
// before asking it to replace the table; only the documented missing-table responses are safe.
|
|
33
|
+
async function verifyCrontabReadable(): Promise<void> {
|
|
34
|
+
const child = Bun.spawn(['crontab', '-l'], {
|
|
35
|
+
env: { ...process.env, LC_ALL: 'C' },
|
|
36
|
+
stdout: 'pipe',
|
|
37
|
+
stderr: 'pipe',
|
|
38
|
+
timeout: 10_000,
|
|
39
|
+
});
|
|
40
|
+
const [code, output, error] = await Promise.all([
|
|
41
|
+
child.exited,
|
|
42
|
+
new Response(child.stdout).text(),
|
|
43
|
+
new Response(child.stderr).text(),
|
|
44
|
+
]);
|
|
45
|
+
if (code === 0) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
if (
|
|
49
|
+
code === 1 &&
|
|
50
|
+
!output &&
|
|
51
|
+
(/^no crontab for [^\r\n]+$/i.test(error.trim()) ||
|
|
52
|
+
/^crontab: can't open '[^/'\r\n]+': No such file or directory$/.test(error.trim()))
|
|
53
|
+
) {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
throw new Error(`Cannot read crontab: ${error.trim() || `exit ${code}`}`);
|
|
57
|
+
}
|
package/src/execution/worker.ts
CHANGED
|
@@ -13,7 +13,6 @@ export interface WorkerControl {
|
|
|
13
13
|
}
|
|
14
14
|
export class Worker implements WorkerControl {
|
|
15
15
|
readonly #lifetime = new AbortController();
|
|
16
|
-
#timer?: ReturnType<typeof setTimeout>;
|
|
17
16
|
#started = false;
|
|
18
17
|
readonly #acquisitions = new Map<
|
|
19
18
|
Promise<void>,
|
|
@@ -26,8 +25,10 @@ export class Worker implements WorkerControl {
|
|
|
26
25
|
#cleaning?: Promise<void>;
|
|
27
26
|
#cleanupRequested = false;
|
|
28
27
|
#capacityReleased = false;
|
|
29
|
-
#cleanupTimer?: ReturnType<typeof setTimeout>;
|
|
30
28
|
#closing?: Promise<void>;
|
|
29
|
+
#draining?: Promise<void>;
|
|
30
|
+
#drainRequested = false;
|
|
31
|
+
#resume?: () => void;
|
|
31
32
|
constructor(
|
|
32
33
|
private readonly input: {
|
|
33
34
|
acquisition: AcquisitionService;
|
|
@@ -48,30 +49,13 @@ export class Worker implements WorkerControl {
|
|
|
48
49
|
return;
|
|
49
50
|
}
|
|
50
51
|
this.#started = true;
|
|
51
|
-
this.
|
|
52
|
+
this.wake();
|
|
52
53
|
}
|
|
53
54
|
wake(): void {
|
|
54
|
-
this.
|
|
55
|
-
}
|
|
56
|
-
private schedule(delay: number): void {
|
|
57
|
-
if (!this.#started || this.#lifetime.signal.aborted) {
|
|
55
|
+
if ((!this.#started && !this.#draining) || this.#lifetime.signal.aborted) {
|
|
58
56
|
return;
|
|
59
57
|
}
|
|
60
|
-
|
|
61
|
-
// Yield to the event loop even when another step is immediately runnable.
|
|
62
|
-
const maxTimerDelay = 2_147_483_647;
|
|
63
|
-
this.#timer = setTimeout(
|
|
64
|
-
() => {
|
|
65
|
-
this.#timer = undefined;
|
|
66
|
-
try {
|
|
67
|
-
this.dispatch();
|
|
68
|
-
} catch {
|
|
69
|
-
this.input.log({ code: 'runtime_failed' });
|
|
70
|
-
this.schedule(this.input.timing.retryMs);
|
|
71
|
-
}
|
|
72
|
-
},
|
|
73
|
-
Math.min(delay, maxTimerDelay),
|
|
74
|
-
);
|
|
58
|
+
void this.runDue().catch(() => this.input.log({ code: 'runtime_failed' }));
|
|
75
59
|
}
|
|
76
60
|
/** A deterministic dispatch round for hosts/tests that do not start the background worker. */
|
|
77
61
|
tick(): Promise<void> {
|
|
@@ -81,6 +65,41 @@ export class Worker implements WorkerControl {
|
|
|
81
65
|
this.cleanup(),
|
|
82
66
|
);
|
|
83
67
|
}
|
|
68
|
+
/** Drain runnable pages and deliveries, sharing one run across concurrent host triggers. */
|
|
69
|
+
runDue(): Promise<void> {
|
|
70
|
+
this.ensureOpen();
|
|
71
|
+
this.#drainRequested = true;
|
|
72
|
+
this.#resume?.();
|
|
73
|
+
this.#draining ??= Promise.resolve().then(() => this.drain());
|
|
74
|
+
return this.#draining;
|
|
75
|
+
}
|
|
76
|
+
private async drain(): Promise<void> {
|
|
77
|
+
try {
|
|
78
|
+
while (!this.#lifetime.signal.aborted) {
|
|
79
|
+
this.#drainRequested = false;
|
|
80
|
+
const changed = Promise.withResolvers<void>();
|
|
81
|
+
this.#resume = changed.resolve;
|
|
82
|
+
this.dispatch();
|
|
83
|
+
const active = this.#acquisitions.size + this.#deliveries.size;
|
|
84
|
+
if (active) {
|
|
85
|
+
await changed.promise;
|
|
86
|
+
}
|
|
87
|
+
await this.cleanup();
|
|
88
|
+
if (this.#lifetime.signal.aborted) {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
if (active || this.#drainRequested) {
|
|
92
|
+
// Continue pages immediately, while allowing cancellation and host requests to run.
|
|
93
|
+
await new Promise<void>((resolve) => setImmediate(resolve));
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
} finally {
|
|
99
|
+
this.#resume = undefined;
|
|
100
|
+
this.#draining = undefined;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
84
103
|
private dispatch(): void {
|
|
85
104
|
this.ensureOpen();
|
|
86
105
|
const { acquisition, delivery, timing } = this.input;
|
|
@@ -95,7 +114,7 @@ export class Worker implements WorkerControl {
|
|
|
95
114
|
.catch(() => this.input.log({ code: 'acquisition_failed' }))
|
|
96
115
|
.finally(() => {
|
|
97
116
|
this.#acquisitions.delete(task);
|
|
98
|
-
this.
|
|
117
|
+
this.wake();
|
|
99
118
|
});
|
|
100
119
|
this.#acquisitions.set(task, { ownerId: lease.ownerId, syncId: lease.sync.id, abort });
|
|
101
120
|
}
|
|
@@ -113,34 +132,10 @@ export class Worker implements WorkerControl {
|
|
|
113
132
|
.catch(() => this.input.log({ code: 'delivery_failed' }))
|
|
114
133
|
.finally(() => {
|
|
115
134
|
this.#deliveries.delete(task);
|
|
116
|
-
this.
|
|
135
|
+
this.wake();
|
|
117
136
|
});
|
|
118
137
|
this.#deliveries.set(task, { ownerId: lease.ownerId, syncId: lease.delivery.syncId, abort });
|
|
119
138
|
}
|
|
120
|
-
const due = [
|
|
121
|
-
this.#acquisitions.size < timing.sourceConcurrency ? acquisition.nextDue() : undefined,
|
|
122
|
-
this.#deliveries.size < timing.deliveryConcurrency ? delivery.nextDue() : undefined,
|
|
123
|
-
].filter((value): value is number => value !== undefined);
|
|
124
|
-
if (due.length) {
|
|
125
|
-
this.schedule(Math.max(0, Math.min(...due) - Date.now()));
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
private completed(): void {
|
|
129
|
-
this.wake();
|
|
130
|
-
if (this.#started) {
|
|
131
|
-
void this.cleanup().then(
|
|
132
|
-
() => this.wake(),
|
|
133
|
-
() => {
|
|
134
|
-
this.input.log({ code: 'cleanup_failed' });
|
|
135
|
-
if (!this.#lifetime.signal.aborted && !this.#cleanupTimer) {
|
|
136
|
-
this.#cleanupTimer = setTimeout(() => {
|
|
137
|
-
this.#cleanupTimer = undefined;
|
|
138
|
-
this.completed();
|
|
139
|
-
}, this.input.timing.retryMs);
|
|
140
|
-
}
|
|
141
|
-
},
|
|
142
|
-
);
|
|
143
|
-
}
|
|
144
139
|
}
|
|
145
140
|
private cleanup(): Promise<void> {
|
|
146
141
|
this.#cleanupRequested = true;
|
|
@@ -157,8 +152,6 @@ export class Worker implements WorkerControl {
|
|
|
157
152
|
this.#capacityReleased = false;
|
|
158
153
|
this.input.acquisition.capacityReleased();
|
|
159
154
|
}
|
|
160
|
-
clearTimeout(this.#cleanupTimer);
|
|
161
|
-
this.#cleanupTimer = undefined;
|
|
162
155
|
} finally {
|
|
163
156
|
this.#cleaning = undefined;
|
|
164
157
|
}
|
|
@@ -172,7 +165,7 @@ export class Worker implements WorkerControl {
|
|
|
172
165
|
}
|
|
173
166
|
async cancelAcquisition(input: Resource): Promise<void> {
|
|
174
167
|
await this.cancelTasks({ scope: input, tasks: this.#acquisitions });
|
|
175
|
-
this.
|
|
168
|
+
this.wake();
|
|
176
169
|
}
|
|
177
170
|
async cancelSync(input: Resource): Promise<void> {
|
|
178
171
|
await Promise.all([
|
|
@@ -180,7 +173,7 @@ export class Worker implements WorkerControl {
|
|
|
180
173
|
this.cancelTasks({ scope: input, tasks: this.#deliveries }),
|
|
181
174
|
]);
|
|
182
175
|
this.#capacityReleased = true;
|
|
183
|
-
this.
|
|
176
|
+
this.wake();
|
|
184
177
|
await this.cleanup().catch(() => this.input.log({ code: 'cleanup_failed' }));
|
|
185
178
|
}
|
|
186
179
|
private async cancelTasks({
|
|
@@ -204,10 +197,10 @@ export class Worker implements WorkerControl {
|
|
|
204
197
|
return this.#closing;
|
|
205
198
|
}
|
|
206
199
|
private async stop(): Promise<void> {
|
|
207
|
-
clearTimeout(this.#timer);
|
|
208
|
-
clearTimeout(this.#cleanupTimer);
|
|
209
200
|
this.#lifetime.abort('interrupted');
|
|
201
|
+
this.#resume?.();
|
|
210
202
|
await Promise.allSettled([...this.#acquisitions.keys(), ...this.#deliveries.keys()]);
|
|
203
|
+
await this.#draining?.catch(() => undefined);
|
|
211
204
|
await this.cleanup();
|
|
212
205
|
}
|
|
213
206
|
}
|
package/src/open-sync.ts
CHANGED
|
@@ -158,6 +158,7 @@ export async function createOpenSync(options: OpenSyncOptions): Promise<OpenSync
|
|
|
158
158
|
api,
|
|
159
159
|
providers: providerApi(providers),
|
|
160
160
|
start: () => runtime.start(),
|
|
161
|
+
runDue: () => runtime.runDue(),
|
|
161
162
|
async fetch(request: Request): Promise<Response> {
|
|
162
163
|
if (lifetime.signal.aborted) {
|
|
163
164
|
return new Response(null, { status: 503 });
|
|
@@ -205,7 +206,12 @@ export interface OpenSyncRuntime {
|
|
|
205
206
|
api: SyncApi;
|
|
206
207
|
providers: ProviderApi;
|
|
207
208
|
fetch(request: Request): Promise<Response>;
|
|
208
|
-
|
|
209
|
+
/**
|
|
210
|
+
* Register one half-hourly OS job and start processing work.
|
|
211
|
+
* Compiled hosts must call runOpenSyncCron() from @context-use/open-sync/cron before initialization.
|
|
212
|
+
*/
|
|
213
|
+
start(): Promise<void>;
|
|
214
|
+
runDue(): Promise<void>;
|
|
209
215
|
close(): Promise<void>;
|
|
210
216
|
}
|
|
211
217
|
export type { ProviderApi, SyncApi } from './api';
|
|
@@ -10,7 +10,6 @@ export interface AcquisitionLease extends Scope {
|
|
|
10
10
|
}
|
|
11
11
|
export interface AcquisitionRepository {
|
|
12
12
|
capacityReleased(): void;
|
|
13
|
-
nextDue(): number | undefined;
|
|
14
13
|
claim(leaseMs: number): AcquisitionLease | undefined;
|
|
15
14
|
hasCapacity(): boolean;
|
|
16
15
|
commit(input: { lease: AcquisitionLease; page: SyncStep; definition: SyncDefinition }): void;
|
|
@@ -20,14 +20,6 @@ export class SqliteAcquisition implements AcquisitionRepository {
|
|
|
20
20
|
.query("UPDATE syncs SET next_due_at=? WHERE enabled=1 AND status='waiting_for_capacity'")
|
|
21
21
|
.run(Date.now());
|
|
22
22
|
}
|
|
23
|
-
nextDue(): number | undefined {
|
|
24
|
-
return (
|
|
25
|
-
this.input.db
|
|
26
|
-
.query<{ due: number | null }, []>(`
|
|
27
|
-
SELECT MIN(COALESCE(expires_at,next_due_at)) AS due FROM syncs WHERE enabled=1`)
|
|
28
|
-
.get()?.due ?? undefined
|
|
29
|
-
);
|
|
30
|
-
}
|
|
31
23
|
claim(leaseMs: number) {
|
|
32
24
|
return claimAcquisition({ ...this.input, leaseMs });
|
|
33
25
|
}
|
|
@@ -9,7 +9,6 @@ export interface DeliveryLease extends Scope {
|
|
|
9
9
|
attempt: number;
|
|
10
10
|
}
|
|
11
11
|
export interface DeliveryRepository {
|
|
12
|
-
nextDue(): number | undefined;
|
|
13
12
|
claim(leaseMs: number): DeliveryLease | undefined;
|
|
14
13
|
complete(input: { lease: DeliveryLease; result: DeliveryResult; delay: number }): void;
|
|
15
14
|
status(scope: Scope): QueueStatus;
|
|
@@ -13,15 +13,6 @@ const headOfQueue = `NOT EXISTS (SELECT 1 FROM deliveries prior
|
|
|
13
13
|
|
|
14
14
|
export class SqliteDeliveries implements DeliveryRepository {
|
|
15
15
|
constructor(private readonly db: Database) {}
|
|
16
|
-
nextDue(): number | undefined {
|
|
17
|
-
return (
|
|
18
|
-
this.db
|
|
19
|
-
.query<{ due: number | null }, []>(`SELECT MIN(
|
|
20
|
-
CASE WHEN d.state='leased' THEN MAX(d.due_at,d.expires_at) ELSE d.due_at END) AS due
|
|
21
|
-
FROM deliveries d WHERE d.state!='blocked' AND ${headOfQueue}`)
|
|
22
|
-
.get()?.due ?? undefined
|
|
23
|
-
);
|
|
24
|
-
}
|
|
25
16
|
claim(leaseMs: number): DeliveryLease | undefined {
|
|
26
17
|
return this.db
|
|
27
18
|
.transaction(() => {
|
package/src/runtime.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { syncApi } from './api';
|
|
2
2
|
import { openDatabase } from './db/client';
|
|
3
|
+
import { startCron } from './execution/cron';
|
|
3
4
|
import { type Logger, safeLogger } from './execution/diagnostics';
|
|
4
5
|
import type { ProviderGateway } from './execution/provider';
|
|
5
6
|
import { Worker } from './execution/worker';
|
|
@@ -93,13 +94,42 @@ export function createSyncRuntime(options: SyncRuntimeOptions) {
|
|
|
93
94
|
limits,
|
|
94
95
|
timeoutMs: timing.timeoutMs,
|
|
95
96
|
});
|
|
97
|
+
let starting: Promise<void> | undefined;
|
|
98
|
+
let cron: Awaited<ReturnType<typeof startCron>> | undefined;
|
|
96
99
|
let closing: Promise<void> | undefined;
|
|
97
100
|
return {
|
|
98
101
|
api: syncApi(api),
|
|
99
|
-
|
|
102
|
+
/** Requires a working OS scheduler supported by Bun.cron. Registration failures reject startup. */
|
|
103
|
+
start(): Promise<void> {
|
|
104
|
+
worker.ensureOpen();
|
|
105
|
+
if (closing) {
|
|
106
|
+
fail('closed');
|
|
107
|
+
}
|
|
108
|
+
starting ??= (async () => {
|
|
109
|
+
cron = await startCron({
|
|
110
|
+
runtime: worker,
|
|
111
|
+
directory: `${options.databasePath}.cron`,
|
|
112
|
+
onError: () => log({ code: 'runtime_failed' }),
|
|
113
|
+
});
|
|
114
|
+
worker.start();
|
|
115
|
+
})();
|
|
116
|
+
return starting;
|
|
117
|
+
},
|
|
118
|
+
runDue: () => worker.runDue(),
|
|
100
119
|
tick: () => worker.tick(),
|
|
101
120
|
close: () => {
|
|
102
|
-
closing ??=
|
|
121
|
+
closing ??= (async () => {
|
|
122
|
+
try {
|
|
123
|
+
await starting?.catch(() => undefined);
|
|
124
|
+
await worker.close();
|
|
125
|
+
} finally {
|
|
126
|
+
try {
|
|
127
|
+
await cron?.close();
|
|
128
|
+
} finally {
|
|
129
|
+
db.close();
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
})();
|
|
103
133
|
return closing;
|
|
104
134
|
},
|
|
105
135
|
};
|
package/src/services/delivery.ts
CHANGED
|
@@ -20,9 +20,6 @@ export class DeliveryService {
|
|
|
20
20
|
claim() {
|
|
21
21
|
return this.input.repository.claim(this.input.timing.leaseMs);
|
|
22
22
|
}
|
|
23
|
-
nextDue() {
|
|
24
|
-
return this.input.repository.nextDue();
|
|
25
|
-
}
|
|
26
23
|
async execute(input: { lease: DeliveryLease; signal: AbortSignal }): Promise<boolean> {
|
|
27
24
|
const { repository, registry, timing } = this.input;
|
|
28
25
|
const { lease, signal } = input;
|