@context-use/open-sync 0.2.1 → 0.3.1
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/LICENSE +3 -0
- package/package.json +7 -5
- package/src/api.ts +18 -16
- package/src/connector/client.ts +61 -28
- package/src/connector/failure.ts +49 -0
- package/src/connector/file.ts +104 -0
- package/src/connector/response.ts +22 -1
- package/src/db/client.ts +1 -11
- package/src/db/providers.sql +2 -3
- package/src/db/providers.ts +1 -11
- package/src/db/schema.sql +41 -36
- package/src/execution/abortable.ts +12 -0
- package/src/execution/diagnostics.ts +1 -2
- package/src/execution/provider.ts +1 -0
- package/src/execution/worker.ts +162 -39
- package/src/http/controller.ts +63 -42
- package/src/models/asset.ts +61 -0
- package/src/models/definition.ts +30 -15
- package/src/models/delivery.ts +25 -31
- package/src/models/error.ts +12 -1
- package/src/models/limits.ts +8 -3
- package/src/models/metadata.ts +42 -0
- package/src/models/page.ts +68 -13
- package/src/models/provider-path.ts +25 -0
- package/src/models/providers.ts +13 -2
- package/src/models/record.ts +22 -0
- package/src/models/registry.ts +15 -15
- package/src/models/source-http-error.ts +37 -0
- package/src/models/sync.ts +71 -0
- package/src/models/validation.ts +1 -1
- package/src/open-sync.ts +26 -7
- package/src/repositories/acquisition/contract.ts +18 -10
- package/src/repositories/acquisition/lease.ts +80 -94
- package/src/repositories/acquisition/outbox.ts +22 -22
- package/src/repositories/acquisition/records.ts +44 -13
- package/src/repositories/acquisition/sqlite.ts +78 -31
- package/src/repositories/assets/capacity.ts +16 -0
- package/src/repositories/assets/contract.ts +25 -0
- package/src/repositories/assets/filesystem.ts +103 -0
- package/src/repositories/assets/sqlite.ts +174 -0
- package/src/repositories/catalog/contract.ts +16 -19
- package/src/repositories/catalog/sqlite.ts +98 -121
- package/src/repositories/delivery/contract.ts +7 -11
- package/src/repositories/delivery/lease.ts +16 -0
- package/src/repositories/delivery/sqlite.ts +57 -34
- package/src/repositories/queue-usage.ts +7 -8
- package/src/repositories/rows.ts +10 -29
- package/src/runtime.ts +27 -6
- package/src/services/acquisition.ts +178 -52
- package/src/services/delivery.ts +39 -19
- package/src/services/management.ts +88 -51
- package/src/services/providers/service.ts +100 -10
- package/src/models/installation.ts +0 -37
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import type { Database } from 'bun:sqlite';
|
|
2
|
+
import type { AssetMetadata, AssetRef, DeliveryAsset } from '../../models/asset';
|
|
3
|
+
import { assetKey } from '../../models/asset';
|
|
4
|
+
import { fail } from '../../models/error';
|
|
5
|
+
import { canonicalJson } from '../../models/json';
|
|
6
|
+
import { normalizeSourceTimestamps } from '../../models/metadata';
|
|
7
|
+
import { identifier } from '../../models/validation';
|
|
8
|
+
import type { AcquisitionLease } from '../acquisition/contract';
|
|
9
|
+
import { assertAcquisition } from '../acquisition/lease';
|
|
10
|
+
import { assertDelivery } from '../delivery/lease';
|
|
11
|
+
import { assetBytes } from './capacity';
|
|
12
|
+
import type { AssetRepository } from './contract';
|
|
13
|
+
|
|
14
|
+
const retained = `EXISTS (SELECT 1 FROM deliveries d WHERE d.owner_id=a.owner_id AND d.id=a.delivery_id)
|
|
15
|
+
OR EXISTS (SELECT 1 FROM syncs s WHERE s.owner_id=a.owner_id AND s.id=a.sync_id
|
|
16
|
+
AND s.generation=a.generation AND s.enabled=1 AND s.expires_at>?)`;
|
|
17
|
+
|
|
18
|
+
export class SqliteAssets implements AssetRepository {
|
|
19
|
+
constructor(private readonly input: { db: Database; maxBytes: number }) {}
|
|
20
|
+
stage(input: Parameters<AssetRepository['stage']>[0]): string {
|
|
21
|
+
const { db } = this.input;
|
|
22
|
+
validateMetadata(input.asset);
|
|
23
|
+
if (input.unavailable !== undefined) {
|
|
24
|
+
identifier(input.unavailable);
|
|
25
|
+
}
|
|
26
|
+
return db
|
|
27
|
+
.transaction(() => {
|
|
28
|
+
assertAcquisition({ db, lease: input.lease });
|
|
29
|
+
const id = crypto.randomUUID();
|
|
30
|
+
const descriptor = canonicalJson({
|
|
31
|
+
...input.asset,
|
|
32
|
+
...normalizeSourceTimestamps(input.asset),
|
|
33
|
+
...(input.unavailable === undefined ? {} : { unavailable: input.unavailable }),
|
|
34
|
+
}).json;
|
|
35
|
+
db.query(`INSERT INTO delivery_assets(id,owner_id,sync_id,generation,asset_id,asset_version,descriptor,ready)
|
|
36
|
+
VALUES (?,?,?,?,?,?,?,?)`).run(
|
|
37
|
+
id,
|
|
38
|
+
input.lease.ownerId,
|
|
39
|
+
input.lease.sync.id,
|
|
40
|
+
input.lease.generation,
|
|
41
|
+
input.asset.id,
|
|
42
|
+
input.asset.version,
|
|
43
|
+
descriptor,
|
|
44
|
+
Number(input.unavailable !== undefined),
|
|
45
|
+
);
|
|
46
|
+
return id;
|
|
47
|
+
})
|
|
48
|
+
.immediate();
|
|
49
|
+
}
|
|
50
|
+
captured(input: Parameters<AssetRepository['captured']>[0]): void {
|
|
51
|
+
const { db } = this.input;
|
|
52
|
+
db.transaction(() => {
|
|
53
|
+
assertAcquisition({ db, lease: input.lease });
|
|
54
|
+
const row = db
|
|
55
|
+
.query<{ descriptor: string }, (string | number)[]>(
|
|
56
|
+
'SELECT descriptor FROM delivery_assets WHERE id=? AND owner_id=? AND sync_id=? AND generation=? AND bytes=? AND ready=0',
|
|
57
|
+
)
|
|
58
|
+
.get(
|
|
59
|
+
input.id,
|
|
60
|
+
input.lease.ownerId,
|
|
61
|
+
input.lease.sync.id,
|
|
62
|
+
input.lease.generation,
|
|
63
|
+
input.size,
|
|
64
|
+
);
|
|
65
|
+
if (!row) {
|
|
66
|
+
fail('asset_reservation_missing');
|
|
67
|
+
}
|
|
68
|
+
db.query('UPDATE delivery_assets SET descriptor=?,ready=1 WHERE id=?').run(
|
|
69
|
+
canonicalJson({ ...JSON.parse(row!.descriptor), size: input.size, sha256: input.sha256 })
|
|
70
|
+
.json,
|
|
71
|
+
input.id,
|
|
72
|
+
);
|
|
73
|
+
}).immediate();
|
|
74
|
+
}
|
|
75
|
+
reserve(input: Parameters<AssetRepository['reserve']>[0]): void {
|
|
76
|
+
const { db, maxBytes } = this.input;
|
|
77
|
+
db.transaction(() => {
|
|
78
|
+
assertAcquisition({ db, lease: input.lease });
|
|
79
|
+
const row = db
|
|
80
|
+
.query<{ bytes: number }, (string | number)[]>(
|
|
81
|
+
'SELECT bytes FROM delivery_assets WHERE id=? AND owner_id=? AND sync_id=? AND generation=? AND ready=0',
|
|
82
|
+
)
|
|
83
|
+
.get(input.id, input.lease.ownerId, input.lease.sync.id, input.lease.generation);
|
|
84
|
+
if (!row || input.bytes < row.bytes) {
|
|
85
|
+
fail('asset_reservation_conflict');
|
|
86
|
+
}
|
|
87
|
+
const delta = input.bytes - row!.bytes;
|
|
88
|
+
if (delta + assetBytes({ db, lease: input.lease }) > maxBytes) {
|
|
89
|
+
fail('step_exceeds_asset_capacity');
|
|
90
|
+
}
|
|
91
|
+
if (delta + assetBytes({ db }) > maxBytes) {
|
|
92
|
+
fail('waiting_for_capacity');
|
|
93
|
+
}
|
|
94
|
+
db.query('UPDATE delivery_assets SET bytes=? WHERE id=?').run(input.bytes, input.id);
|
|
95
|
+
}).immediate();
|
|
96
|
+
}
|
|
97
|
+
discarded(id: string): void {
|
|
98
|
+
const deleted = this.input.db
|
|
99
|
+
.query<{ owner_id: string; sync_id: string }, [string, number]>(
|
|
100
|
+
`DELETE FROM delivery_assets AS a WHERE id=? AND NOT (${retained}) RETURNING owner_id,sync_id`,
|
|
101
|
+
)
|
|
102
|
+
.get(id, Date.now());
|
|
103
|
+
if (deleted) {
|
|
104
|
+
this.input.db
|
|
105
|
+
.query(
|
|
106
|
+
"UPDATE syncs SET next_due_at=? WHERE enabled=1 AND status='waiting_for_capacity' AND NOT (owner_id=? AND id=?)",
|
|
107
|
+
)
|
|
108
|
+
.run(Date.now(), deleted.owner_id, deleted.sync_id);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
garbage(): string[] {
|
|
112
|
+
return this.input.db
|
|
113
|
+
.query<{ id: string }, [number]>(`SELECT a.id FROM delivery_assets a WHERE NOT (${retained})`)
|
|
114
|
+
.all(Date.now())
|
|
115
|
+
.map(({ id }) => id);
|
|
116
|
+
}
|
|
117
|
+
retained(id: string): boolean {
|
|
118
|
+
// Any ledger row is retained by the filesystem sweep. Garbage cleanup owns its deletion.
|
|
119
|
+
return !!this.input.db.query('SELECT 1 FROM delivery_assets WHERE id=?').get(id);
|
|
120
|
+
}
|
|
121
|
+
read(input: Parameters<AssetRepository['read']>[0]): string {
|
|
122
|
+
assertDelivery({ db: this.input.db, lease: input.lease });
|
|
123
|
+
const row = this.input.db
|
|
124
|
+
.query<{ id: string; descriptor: string }, string[]>(
|
|
125
|
+
'SELECT id,descriptor FROM delivery_assets WHERE owner_id=? AND delivery_id=? AND asset_id=? AND asset_version=? AND ready=1',
|
|
126
|
+
)
|
|
127
|
+
.get(input.lease.ownerId, input.lease.delivery.id, input.asset.id, input.asset.version);
|
|
128
|
+
if (!row) {
|
|
129
|
+
return fail('not_found');
|
|
130
|
+
}
|
|
131
|
+
if ('unavailable' in JSON.parse(row.descriptor)) {
|
|
132
|
+
fail('asset_content_missing');
|
|
133
|
+
}
|
|
134
|
+
return row.id;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** Called inside the checkpoint transaction. A previous acquisition generation cannot supply bytes. */
|
|
139
|
+
export function capturedAssets(input: {
|
|
140
|
+
db: Database;
|
|
141
|
+
lease: AcquisitionLease;
|
|
142
|
+
refs: readonly AssetRef[];
|
|
143
|
+
}): DeliveryAsset[] {
|
|
144
|
+
const refs = new Map(input.refs.map((ref) => [assetKey(ref), ref]));
|
|
145
|
+
return [...refs.keys()].sort().map((key) => {
|
|
146
|
+
const ref = refs.get(key)!;
|
|
147
|
+
const row = input.db
|
|
148
|
+
.query<{ descriptor: string }, (string | number)[]>(
|
|
149
|
+
'SELECT descriptor FROM delivery_assets WHERE owner_id=? AND sync_id=? AND generation=? AND asset_id=? AND asset_version=? AND ready=1',
|
|
150
|
+
)
|
|
151
|
+
.get(input.lease.ownerId, input.lease.sync.id, input.lease.generation, ref.id, ref.version);
|
|
152
|
+
if (!row) {
|
|
153
|
+
return fail('asset_not_captured');
|
|
154
|
+
}
|
|
155
|
+
return JSON.parse(row.descriptor) as DeliveryAsset;
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
function validateMetadata(asset: AssetMetadata) {
|
|
159
|
+
identifier(asset.id);
|
|
160
|
+
identifier(asset.version);
|
|
161
|
+
const maxName = 1024;
|
|
162
|
+
const maxType = 256;
|
|
163
|
+
if (
|
|
164
|
+
typeof asset.name !== 'string' ||
|
|
165
|
+
asset.name.length > maxName ||
|
|
166
|
+
!asset.name ||
|
|
167
|
+
typeof asset.mediaType !== 'string' ||
|
|
168
|
+
!asset.mediaType ||
|
|
169
|
+
asset.mediaType.length > maxType ||
|
|
170
|
+
/[\r\n]/.test(asset.mediaType)
|
|
171
|
+
) {
|
|
172
|
+
fail('invalid_asset_metadata');
|
|
173
|
+
}
|
|
174
|
+
}
|
|
@@ -1,24 +1,21 @@
|
|
|
1
|
-
import type { ConnectionRef
|
|
2
|
-
import type { Destination } from '../../models/delivery';
|
|
1
|
+
import type { ConnectionRef } from '../../models/definition';
|
|
3
2
|
import type { Resource, Scope } from '../../models/identity';
|
|
4
|
-
import type { CreateInstallation, Installation, SyncRun } from '../../models/installation';
|
|
5
3
|
import type { JsonObject, JsonValue } from '../../models/json';
|
|
4
|
+
import type { CreateSync, Sync, SyncPoll } from '../../models/sync';
|
|
6
5
|
|
|
7
6
|
export interface CatalogRepository {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
setEnabled(input: Resource & { enabled: boolean }): Installation;
|
|
23
|
-
queue(input: Resource & { checkpoint?: JsonValue }): void;
|
|
7
|
+
createSync(
|
|
8
|
+
input: Omit<CreateSync, 'destination'> & {
|
|
9
|
+
destination: { type: string; config: JsonObject };
|
|
10
|
+
initialCheckpoint: JsonValue;
|
|
11
|
+
},
|
|
12
|
+
): Sync;
|
|
13
|
+
sync(input: Resource): Sync;
|
|
14
|
+
syncs(scope: Scope): Sync[];
|
|
15
|
+
polls(input: Resource & { before?: number }): { polls: SyncPoll[]; nextCursor: number | null };
|
|
16
|
+
connectSync(input: Resource & { connection: ConnectionRef }): Sync;
|
|
17
|
+
setEnabled(input: Resource & { enabled: boolean }): Sync;
|
|
18
|
+
runNow(input: Resource): void;
|
|
19
|
+
resync(input: Resource & { checkpoint: JsonValue }): void;
|
|
20
|
+
removeSync(input: Resource): void;
|
|
24
21
|
}
|
|
@@ -1,140 +1,94 @@
|
|
|
1
1
|
import type { Database } from 'bun:sqlite';
|
|
2
|
-
import type { ConnectionRef
|
|
2
|
+
import type { ConnectionRef } from '../../models/definition';
|
|
3
3
|
import { fail } from '../../models/error';
|
|
4
4
|
import type { Resource, Scope } from '../../models/identity';
|
|
5
|
-
import type { CreateInstallation } from '../../models/installation';
|
|
6
5
|
import { canonicalJson, type JsonObject, type JsonValue } from '../../models/json';
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
6
|
+
import type { CreateSync, SyncPoll } from '../../models/sync';
|
|
7
|
+
import { readSync } from '../rows';
|
|
9
8
|
import type { CatalogRepository } from './contract';
|
|
10
9
|
|
|
10
|
+
const defaultIntervalMs = 60_000;
|
|
11
|
+
|
|
11
12
|
export class SqliteCatalog implements CatalogRepository {
|
|
12
13
|
constructor(private readonly db: Database) {}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
'SELECT manifest FROM definitions WHERE id=? AND version=?',
|
|
20
|
-
)
|
|
21
|
-
.get(definition.id, definition.version);
|
|
22
|
-
if (previous && previous.manifest !== manifest) {
|
|
23
|
-
fail('definition_conflict');
|
|
24
|
-
}
|
|
25
|
-
this.db
|
|
26
|
-
.query('INSERT OR IGNORE INTO definitions VALUES (?,?,?,?)')
|
|
27
|
-
.run(definition.id, definition.version, definition.artifactId, manifest);
|
|
28
|
-
})
|
|
29
|
-
.immediate();
|
|
30
|
-
}
|
|
31
|
-
createDestination(input: Scope & { type: string; version: string; config: JsonObject }) {
|
|
32
|
-
const id = `dest_${crypto.randomUUID()}`;
|
|
33
|
-
this.db
|
|
34
|
-
.query('INSERT INTO destinations VALUES (?,?,?,?,?)')
|
|
35
|
-
.run(input.ownerId, id, input.type, input.version, canonicalJson(input.config).json);
|
|
36
|
-
return readDestination({ db: this.db, scope: { ...input, id } });
|
|
37
|
-
}
|
|
38
|
-
destinations(scope: Scope) {
|
|
39
|
-
return this.db
|
|
40
|
-
.query<{ id: string }, [string]>(
|
|
41
|
-
'SELECT id FROM destinations WHERE owner_id=? ORDER BY rowid',
|
|
42
|
-
)
|
|
43
|
-
.all(scope.ownerId)
|
|
44
|
-
.map(({ id }) => readDestination({ db: this.db, scope: { ...scope, id } }));
|
|
45
|
-
}
|
|
46
|
-
createInstallation(input: CreateInstallation & { initialCheckpoint: JsonValue }) {
|
|
14
|
+
createSync(
|
|
15
|
+
input: Omit<CreateSync, 'destination'> & {
|
|
16
|
+
destination: { type: string; config: JsonObject };
|
|
17
|
+
initialCheckpoint: JsonValue;
|
|
18
|
+
},
|
|
19
|
+
) {
|
|
47
20
|
const id = `sync_${crypto.randomUUID()}`;
|
|
48
21
|
this.db
|
|
49
|
-
.
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
input.intervalMs ?? defaultTiming.leaseMs,
|
|
67
|
-
Date.now(),
|
|
68
|
-
input.enabled === false ? 'disabled' : 'ready',
|
|
69
|
-
);
|
|
70
|
-
})
|
|
71
|
-
.immediate();
|
|
72
|
-
return this.installation({ ...input, id });
|
|
22
|
+
.query(`INSERT INTO syncs (owner_id,id,definition_id,connection,config,destination_type,destination_config,enabled,checkpoint,interval_ms,next_due_at,status)
|
|
23
|
+
VALUES (?,?,?,?,?,?,?,?,?,?,?,?)`)
|
|
24
|
+
.run(
|
|
25
|
+
input.ownerId,
|
|
26
|
+
id,
|
|
27
|
+
input.definition,
|
|
28
|
+
input.connection ? canonicalJson(input.connection).json : null,
|
|
29
|
+
canonicalJson(input.config).json,
|
|
30
|
+
input.destination.type,
|
|
31
|
+
canonicalJson(input.destination.config).json,
|
|
32
|
+
Number(input.enabled ?? true),
|
|
33
|
+
canonicalJson(input.initialCheckpoint).json,
|
|
34
|
+
input.intervalMs ?? defaultIntervalMs,
|
|
35
|
+
Date.now(),
|
|
36
|
+
input.enabled === false ? 'disabled' : 'ready',
|
|
37
|
+
);
|
|
38
|
+
return this.sync({ ...input, id });
|
|
73
39
|
}
|
|
74
|
-
|
|
75
|
-
return
|
|
40
|
+
sync(input: Resource) {
|
|
41
|
+
return readSync({ db: this.db, scope: input });
|
|
76
42
|
}
|
|
77
|
-
|
|
43
|
+
syncs(scope: Scope) {
|
|
78
44
|
return this.db
|
|
79
|
-
.query<{ id: string }, [string]>(
|
|
80
|
-
'SELECT id FROM installations WHERE owner_id=? ORDER BY rowid',
|
|
81
|
-
)
|
|
45
|
+
.query<{ id: string }, [string]>('SELECT id FROM syncs WHERE owner_id=? ORDER BY rowid')
|
|
82
46
|
.all(scope.ownerId)
|
|
83
|
-
.map(({ id }) => this.
|
|
47
|
+
.map(({ id }) => this.sync({ ...scope, id }));
|
|
84
48
|
}
|
|
85
|
-
|
|
86
|
-
this.
|
|
87
|
-
const
|
|
49
|
+
polls(input: Resource & { before?: number }) {
|
|
50
|
+
this.sync(input);
|
|
51
|
+
const pageSize = 20;
|
|
88
52
|
const rows = this.db
|
|
89
53
|
.query<
|
|
90
|
-
|
|
91
|
-
id: string;
|
|
92
|
-
state: string;
|
|
93
|
-
started_at: number;
|
|
94
|
-
completed_at: number | null;
|
|
95
|
-
pages: number;
|
|
96
|
-
checkpoint_revision: number;
|
|
97
|
-
},
|
|
54
|
+
SyncPoll,
|
|
98
55
|
[string, string, number, number]
|
|
99
|
-
>(
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
.all(input.ownerId, input.id,
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
id: row.id,
|
|
106
|
-
state: row.state,
|
|
107
|
-
startedAt: row.started_at,
|
|
108
|
-
completedAt: row.completed_at,
|
|
109
|
-
pages: row.pages,
|
|
110
|
-
checkpointRevision: row.checkpoint_revision,
|
|
111
|
-
})),
|
|
112
|
-
hasMore: rows.length > limit,
|
|
113
|
-
pageSize: limit,
|
|
114
|
-
};
|
|
56
|
+
>(`SELECT id,started_at AS startedAt,completed_at AS completedAt,
|
|
57
|
+
state,error_code AS errorCode,records_processed AS recordsProcessed,records_queued AS recordsQueued
|
|
58
|
+
FROM sync_polls WHERE owner_id=? AND sync_id=? AND id<? ORDER BY id DESC LIMIT ?`)
|
|
59
|
+
.all(input.ownerId, input.id, input.before ?? Number.MAX_SAFE_INTEGER, pageSize + 1);
|
|
60
|
+
const polls = rows.slice(0, pageSize);
|
|
61
|
+
return { polls, nextCursor: rows.length > pageSize ? polls.at(-1)!.id : null };
|
|
115
62
|
}
|
|
116
|
-
|
|
63
|
+
connectSync(input: Resource & { connection: ConnectionRef }) {
|
|
117
64
|
return this.db
|
|
118
65
|
.transaction(() => {
|
|
119
|
-
const
|
|
120
|
-
if (
|
|
66
|
+
const sync = this.sync(input);
|
|
67
|
+
if (sync.connection || sync.enabled) {
|
|
121
68
|
fail('already_connected');
|
|
122
69
|
}
|
|
123
70
|
this.db
|
|
124
|
-
.query(`UPDATE
|
|
125
|
-
|
|
71
|
+
.query(`UPDATE syncs SET connection=?,enabled=1,
|
|
72
|
+
status='ready',error_code=NULL,next_due_at=? WHERE owner_id=? AND id=?`)
|
|
126
73
|
.run(canonicalJson(input.connection).json, Date.now(), input.ownerId, input.id);
|
|
127
|
-
return this.
|
|
74
|
+
return this.sync(input);
|
|
128
75
|
})
|
|
129
76
|
.immediate();
|
|
130
77
|
}
|
|
131
78
|
setEnabled(input: Resource & { enabled: boolean }) {
|
|
132
79
|
return this.db
|
|
133
80
|
.transaction(() => {
|
|
134
|
-
this.
|
|
81
|
+
const sync = this.sync(input);
|
|
82
|
+
if (sync.enabled === input.enabled) {
|
|
83
|
+
return sync;
|
|
84
|
+
}
|
|
85
|
+
this.db
|
|
86
|
+
.query(`UPDATE sync_polls SET state=?,error_code=NULL
|
|
87
|
+
WHERE owner_id=? AND sync_id=? AND completed_at IS NULL`)
|
|
88
|
+
.run(input.enabled ? 'ready' : 'disabled', input.ownerId, input.id);
|
|
135
89
|
this.db
|
|
136
90
|
.query(
|
|
137
|
-
|
|
91
|
+
`UPDATE syncs SET enabled=?,status=?,error_code=NULL,next_due_at=?,generation=generation+1,expires_at=NULL WHERE owner_id=? AND id=?`,
|
|
138
92
|
)
|
|
139
93
|
.run(
|
|
140
94
|
Number(input.enabled),
|
|
@@ -143,38 +97,61 @@ export class SqliteCatalog implements CatalogRepository {
|
|
|
143
97
|
input.ownerId,
|
|
144
98
|
input.id,
|
|
145
99
|
);
|
|
146
|
-
this.
|
|
147
|
-
.query(
|
|
148
|
-
"UPDATE runs SET state='cancelled',completed_at=? WHERE owner_id=? AND installation_id=? AND state='running'",
|
|
149
|
-
)
|
|
150
|
-
.run(Date.now(), input.ownerId, input.id);
|
|
151
|
-
return this.installation(input);
|
|
100
|
+
return this.sync(input);
|
|
152
101
|
})
|
|
153
102
|
.immediate();
|
|
154
103
|
}
|
|
155
|
-
|
|
104
|
+
runNow(input: Resource): void {
|
|
156
105
|
this.db
|
|
157
106
|
.transaction(() => {
|
|
158
|
-
if (!this.
|
|
107
|
+
if (!this.sync(input).enabled) {
|
|
159
108
|
fail('disabled');
|
|
160
109
|
}
|
|
161
110
|
if (
|
|
162
111
|
this.db
|
|
163
|
-
.query(
|
|
164
|
-
.get(input.ownerId, input.id)
|
|
112
|
+
.query(`SELECT 1 FROM syncs WHERE owner_id=? AND id=? AND expires_at>?`)
|
|
113
|
+
.get(input.ownerId, input.id, Date.now())
|
|
165
114
|
) {
|
|
166
115
|
fail('busy');
|
|
167
116
|
}
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
117
|
+
this.db
|
|
118
|
+
.query(`UPDATE syncs SET next_due_at=? WHERE owner_id=? AND id=?`)
|
|
119
|
+
.run(Date.now(), input.ownerId, input.id);
|
|
120
|
+
})
|
|
121
|
+
.immediate();
|
|
122
|
+
}
|
|
123
|
+
resync(input: Resource & { checkpoint: JsonValue }): void {
|
|
124
|
+
this.db
|
|
125
|
+
.transaction(() => {
|
|
126
|
+
if (!this.sync(input).enabled) {
|
|
127
|
+
fail('disabled');
|
|
174
128
|
}
|
|
175
129
|
this.db
|
|
176
|
-
.query(
|
|
130
|
+
.query(`UPDATE sync_polls SET state='interrupted',error_code='resync_requested',completed_at=?
|
|
131
|
+
WHERE owner_id=? AND sync_id=? AND completed_at IS NULL`)
|
|
177
132
|
.run(Date.now(), input.ownerId, input.id);
|
|
133
|
+
this.db
|
|
134
|
+
.query(
|
|
135
|
+
`UPDATE syncs SET checkpoint=?,status='ready',error_code=NULL,next_due_at=?,resync=1,failure_count=0,generation=generation+1,expires_at=NULL WHERE owner_id=? AND id=?`,
|
|
136
|
+
)
|
|
137
|
+
.run(canonicalJson(input.checkpoint).json, Date.now(), input.ownerId, input.id);
|
|
138
|
+
})
|
|
139
|
+
.immediate();
|
|
140
|
+
}
|
|
141
|
+
removeSync(input: Resource): void {
|
|
142
|
+
this.db
|
|
143
|
+
.transaction(() => {
|
|
144
|
+
this.sync(input);
|
|
145
|
+
this.db
|
|
146
|
+
.query('DELETE FROM deliveries WHERE owner_id=? AND sync_id=?')
|
|
147
|
+
.run(input.ownerId, input.id);
|
|
148
|
+
this.db
|
|
149
|
+
.query('DELETE FROM record_state WHERE owner_id=? AND sync_id=?')
|
|
150
|
+
.run(input.ownerId, input.id);
|
|
151
|
+
this.db.query('DELETE FROM syncs WHERE owner_id=? AND id=?').run(input.ownerId, input.id);
|
|
152
|
+
this.db
|
|
153
|
+
.query(`UPDATE syncs SET next_due_at=? WHERE enabled=1 AND status='waiting_for_capacity'`)
|
|
154
|
+
.run(Date.now());
|
|
178
155
|
})
|
|
179
156
|
.immediate();
|
|
180
157
|
}
|
|
@@ -1,23 +1,19 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
Delivery,
|
|
3
|
-
DeliveryPage,
|
|
4
|
-
DeliveryResult,
|
|
5
|
-
Destination,
|
|
6
|
-
QueueStatus,
|
|
7
|
-
} from '../../models/delivery';
|
|
1
|
+
import type { Deliverable, DeliveryPage, DeliveryResult, QueueStatus } from '../../models/delivery';
|
|
8
2
|
import type { Resource, Scope } from '../../models/identity';
|
|
3
|
+
import type { Sync } from '../../models/sync';
|
|
9
4
|
|
|
10
5
|
export interface DeliveryLease extends Scope {
|
|
11
|
-
delivery:
|
|
12
|
-
destination:
|
|
13
|
-
workerId: string;
|
|
6
|
+
delivery: Omit<Deliverable, 'openAsset'>;
|
|
7
|
+
destination: Sync['destination'];
|
|
14
8
|
generation: number;
|
|
15
9
|
attempt: number;
|
|
16
10
|
}
|
|
17
11
|
export interface DeliveryRepository {
|
|
12
|
+
nextDue(): number | undefined;
|
|
18
13
|
claim(leaseMs: number): DeliveryLease | undefined;
|
|
19
14
|
complete(input: { lease: DeliveryLease; result: DeliveryResult; delay: number }): void;
|
|
20
15
|
status(scope: Scope): QueueStatus;
|
|
21
|
-
pending(input: Scope & {
|
|
16
|
+
pending(input: Scope & { syncId: string; before?: number }): DeliveryPage;
|
|
17
|
+
deliverable(input: Resource & { syncId: string }): Omit<Deliverable, 'openAsset'>;
|
|
22
18
|
retry(input: Resource): void;
|
|
23
19
|
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Database } from 'bun:sqlite';
|
|
2
|
+
import { fail } from '../../models/error';
|
|
3
|
+
import type { DeliveryLease } from './contract';
|
|
4
|
+
|
|
5
|
+
export function assertDelivery(input: { db: Database; lease: DeliveryLease }): void {
|
|
6
|
+
const { db, lease } = input;
|
|
7
|
+
if (
|
|
8
|
+
!db
|
|
9
|
+
.query(
|
|
10
|
+
"SELECT 1 FROM deliveries WHERE owner_id=? AND id=? AND state='leased' AND generation=? AND expires_at>?",
|
|
11
|
+
)
|
|
12
|
+
.get(lease.ownerId, lease.delivery.id, lease.generation, Date.now())
|
|
13
|
+
) {
|
|
14
|
+
fail('lease_lost');
|
|
15
|
+
}
|
|
16
|
+
}
|