@hearthlog/ledger 0.10.1 → 0.10.2
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 +2 -2
- package/scripts/maintenance.ts +3 -0
- package/src/ledger-app.ts +7 -4
- package/src/maintenance-steps.ts +32 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hearthlog/ledger",
|
|
3
|
-
"version": "0.10.
|
|
3
|
+
"version": "0.10.2",
|
|
4
4
|
"description": "Hearthlog's Ledger: the event store Worker (Elysia over SQLite Durable Objects), its contracts and tests, and the logical backup tooling",
|
|
5
5
|
"license": "AGPL-3.0-only",
|
|
6
6
|
"repository": {
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
"dependencies": {
|
|
31
31
|
"@sinclair/typebox": "0.34.52",
|
|
32
32
|
"elysia": "1.4.30",
|
|
33
|
-
"@hearthlog/contracts": "0.10.
|
|
33
|
+
"@hearthlog/contracts": "0.10.2"
|
|
34
34
|
},
|
|
35
35
|
"peerDependencies": {
|
|
36
36
|
"@hearthlog/generated": "*"
|
package/scripts/maintenance.ts
CHANGED
|
@@ -41,6 +41,9 @@ async function main(): Promise<void> {
|
|
|
41
41
|
if (state.job?.status === 'complete') break;
|
|
42
42
|
state = await request('/step', { requestId });
|
|
43
43
|
console.log(JSON.stringify(state.job));
|
|
44
|
+
if (!values.once && state.job && !['complete', 'failed', 'paused'].includes(state.job.status)) {
|
|
45
|
+
await new Promise(resolve => setTimeout(resolve, 500));
|
|
46
|
+
}
|
|
44
47
|
if (state.job?.status === 'failed') throw new Error(state.job.error ?? 'Maintenance failed');
|
|
45
48
|
} while (!values.once && state.job?.status !== 'complete' && state.job?.status !== 'paused');
|
|
46
49
|
console.log(JSON.stringify({ logId: values.log, requestId, status: state.job?.status,
|
package/src/ledger-app.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { MaintenanceSteps } from './maintenance-steps';
|
|
1
2
|
import { DurableObject } from "cloudflare:workers";
|
|
2
3
|
import { LogMaintenanceStore } from './log-maintenance';
|
|
3
4
|
import type { CheckpointChunk, LogMaintenance, CompactionPreview } from '@hearthlog/contracts/history/checkpoint';
|
|
@@ -142,7 +143,7 @@ interface SocketSessionRow extends Record<string, SqlStorageValue> {
|
|
|
142
143
|
}
|
|
143
144
|
|
|
144
145
|
export class LedgerApp extends DurableObject<Env> {
|
|
145
|
-
private
|
|
146
|
+
private readonly maintenanceSteps = new MaintenanceSteps<LogMaintenance>(work => this.ctx.waitUntil(work));
|
|
146
147
|
|
|
147
148
|
private maintenance(assertWritable: () => void = () => undefined): LogMaintenanceStore {
|
|
148
149
|
const appId = this.ctx.storage.sql.exec<{ app_id: string }>('SELECT app_id FROM app_metadata WHERE singleton = 1').one().app_id;
|
|
@@ -187,9 +188,11 @@ export class LedgerApp extends DurableObject<Env> {
|
|
|
187
188
|
async stepLogMaintenance(access: AccessClaims, logId: string, id: string): Promise<RpcResult<LogMaintenance>> {
|
|
188
189
|
return rpcCall(async () => {
|
|
189
190
|
this.writableAccess(access);
|
|
190
|
-
const
|
|
191
|
-
|
|
192
|
-
const result = await
|
|
191
|
+
const parsedLog = parseUuid(logId, 'log id');
|
|
192
|
+
const parsedId = parseUuid(id, 'request id');
|
|
193
|
+
const result = await this.maintenanceSteps.advance(`${parsedLog}:${parsedId}`,
|
|
194
|
+
() => this.maintenance(() => { this.writableAccess(access); }).step(parsedLog, parsedId),
|
|
195
|
+
() => this.maintenance().state(parsedLog, parsedId));
|
|
193
196
|
this.currentAccess(access);
|
|
194
197
|
return rpcSuccess(result);
|
|
195
198
|
});
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/** One in-flight step per operation, with a bounded wait for HTTP/RPC callers.
|
|
2
|
+
* Losing a response must not enqueue another copy of the same running step.
|
|
3
|
+
*/
|
|
4
|
+
export class MaintenanceSteps<T> {
|
|
5
|
+
private queue: Promise<unknown> = Promise.resolve();
|
|
6
|
+
private readonly running = new Map<string, Promise<T>>();
|
|
7
|
+
|
|
8
|
+
constructor(private readonly keepAlive: (work: Promise<unknown>) => void, private readonly responseWaitMs = 1000) {}
|
|
9
|
+
|
|
10
|
+
async advance(key: string, work: () => Promise<T>, current: () => T): Promise<T> {
|
|
11
|
+
let run = this.running.get(key);
|
|
12
|
+
if (!run) {
|
|
13
|
+
run = this.queue.then(work);
|
|
14
|
+
this.running.set(key, run);
|
|
15
|
+
const settled = run.then(() => undefined, () => undefined).then(() => {
|
|
16
|
+
this.running.delete(key);
|
|
17
|
+
});
|
|
18
|
+
this.queue = settled;
|
|
19
|
+
this.keepAlive(settled);
|
|
20
|
+
}
|
|
21
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
22
|
+
try {
|
|
23
|
+
return await Promise.race([run, new Promise<T>((resolve, reject) => {
|
|
24
|
+
timer = setTimeout(() => {
|
|
25
|
+
try { resolve(current()); } catch (cause) { reject(cause); }
|
|
26
|
+
}, this.responseWaitMs);
|
|
27
|
+
})]);
|
|
28
|
+
} finally {
|
|
29
|
+
clearTimeout(timer);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|