@vllnt/convex-music 0.1.0-canary.09eea4e
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 +21 -0
- package/README.md +169 -0
- package/dist/client/index.d.ts +284 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +166 -0
- package/dist/client/index.js.map +1 -0
- package/dist/client/types.d.ts +100 -0
- package/dist/client/types.d.ts.map +1 -0
- package/dist/client/types.js +2 -0
- package/dist/client/types.js.map +1 -0
- package/dist/component/_generated/api.d.ts +125 -0
- package/dist/component/_generated/api.d.ts.map +1 -0
- package/dist/component/_generated/api.js +31 -0
- package/dist/component/_generated/api.js.map +1 -0
- package/dist/component/_generated/component.d.ts +1143 -0
- package/dist/component/_generated/component.d.ts.map +1 -0
- package/dist/component/_generated/component.js +11 -0
- package/dist/component/_generated/component.js.map +1 -0
- package/dist/component/_generated/dataModel.d.ts +46 -0
- package/dist/component/_generated/dataModel.d.ts.map +1 -0
- package/dist/component/_generated/dataModel.js +11 -0
- package/dist/component/_generated/dataModel.js.map +1 -0
- package/dist/component/_generated/server.d.ts +133 -0
- package/dist/component/_generated/server.d.ts.map +1 -0
- package/dist/component/_generated/server.js +80 -0
- package/dist/component/_generated/server.js.map +1 -0
- package/dist/component/actions.d.ts +144 -0
- package/dist/component/actions.d.ts.map +1 -0
- package/dist/component/actions.js +125 -0
- package/dist/component/actions.js.map +1 -0
- package/dist/component/catalog/browse_order.d.ts +22 -0
- package/dist/component/catalog/browse_order.d.ts.map +1 -0
- package/dist/component/catalog/browse_order.js +37 -0
- package/dist/component/catalog/browse_order.js.map +1 -0
- package/dist/component/catalog/field_source_policy.d.ts +30 -0
- package/dist/component/catalog/field_source_policy.d.ts.map +1 -0
- package/dist/component/catalog/field_source_policy.js +36 -0
- package/dist/component/catalog/field_source_policy.js.map +1 -0
- package/dist/component/catalog/merge.d.ts +58 -0
- package/dist/component/catalog/merge.d.ts.map +1 -0
- package/dist/component/catalog/merge.js +74 -0
- package/dist/component/catalog/merge.js.map +1 -0
- package/dist/component/catalog/mutations.d.ts +78 -0
- package/dist/component/catalog/mutations.d.ts.map +1 -0
- package/dist/component/catalog/mutations.js +259 -0
- package/dist/component/catalog/mutations.js.map +1 -0
- package/dist/component/catalog/promote.d.ts +21 -0
- package/dist/component/catalog/promote.d.ts.map +1 -0
- package/dist/component/catalog/promote.js +25 -0
- package/dist/component/catalog/promote.js.map +1 -0
- package/dist/component/catalog/queries.d.ts +421 -0
- package/dist/component/catalog/queries.d.ts.map +1 -0
- package/dist/component/catalog/queries.js +168 -0
- package/dist/component/catalog/queries.js.map +1 -0
- package/dist/component/config/mutations.d.ts +11 -0
- package/dist/component/config/mutations.d.ts.map +1 -0
- package/dist/component/config/mutations.js +30 -0
- package/dist/component/config/mutations.js.map +1 -0
- package/dist/component/config/queries.d.ts +9 -0
- package/dist/component/config/queries.d.ts.map +1 -0
- package/dist/component/config/queries.js +20 -0
- package/dist/component/config/queries.js.map +1 -0
- package/dist/component/convex.config.d.ts +3 -0
- package/dist/component/convex.config.d.ts.map +1 -0
- package/dist/component/convex.config.js +30 -0
- package/dist/component/convex.config.js.map +1 -0
- package/dist/component/crons.d.ts +11 -0
- package/dist/component/crons.d.ts.map +1 -0
- package/dist/component/crons.js +41 -0
- package/dist/component/crons.js.map +1 -0
- package/dist/component/imports/actions.d.ts +119 -0
- package/dist/component/imports/actions.d.ts.map +1 -0
- package/dist/component/imports/actions.js +442 -0
- package/dist/component/imports/actions.js.map +1 -0
- package/dist/component/imports/dedupe.d.ts +29 -0
- package/dist/component/imports/dedupe.d.ts.map +1 -0
- package/dist/component/imports/dedupe.js +33 -0
- package/dist/component/imports/dedupe.js.map +1 -0
- package/dist/component/imports/mutations.d.ts +59 -0
- package/dist/component/imports/mutations.d.ts.map +1 -0
- package/dist/component/imports/mutations.js +151 -0
- package/dist/component/imports/mutations.js.map +1 -0
- package/dist/component/imports/queries.d.ts +68 -0
- package/dist/component/imports/queries.d.ts.map +1 -0
- package/dist/component/imports/queries.js +22 -0
- package/dist/component/imports/queries.js.map +1 -0
- package/dist/component/imports/state.d.ts +36 -0
- package/dist/component/imports/state.d.ts.map +1 -0
- package/dist/component/imports/state.js +62 -0
- package/dist/component/imports/state.js.map +1 -0
- package/dist/component/mutations.d.ts +55 -0
- package/dist/component/mutations.d.ts.map +1 -0
- package/dist/component/mutations.js +74 -0
- package/dist/component/mutations.js.map +1 -0
- package/dist/component/providers/actions.d.ts +26 -0
- package/dist/component/providers/actions.d.ts.map +1 -0
- package/dist/component/providers/actions.js +92 -0
- package/dist/component/providers/actions.js.map +1 -0
- package/dist/component/providers/apple/impl.d.ts +52 -0
- package/dist/component/providers/apple/impl.d.ts.map +1 -0
- package/dist/component/providers/apple/impl.js +127 -0
- package/dist/component/providers/apple/impl.js.map +1 -0
- package/dist/component/providers/apple/jwt.d.ts +32 -0
- package/dist/component/providers/apple/jwt.d.ts.map +1 -0
- package/dist/component/providers/apple/jwt.js +68 -0
- package/dist/component/providers/apple/jwt.js.map +1 -0
- package/dist/component/providers/apple/mappers.d.ts +20 -0
- package/dist/component/providers/apple/mappers.d.ts.map +1 -0
- package/dist/component/providers/apple/mappers.js +68 -0
- package/dist/component/providers/apple/mappers.js.map +1 -0
- package/dist/component/providers/apple/types.d.ts +80 -0
- package/dist/component/providers/apple/types.d.ts.map +1 -0
- package/dist/component/providers/apple/types.js +7 -0
- package/dist/component/providers/apple/types.js.map +1 -0
- package/dist/component/providers/deezer/impl.d.ts +37 -0
- package/dist/component/providers/deezer/impl.d.ts.map +1 -0
- package/dist/component/providers/deezer/impl.js +99 -0
- package/dist/component/providers/deezer/impl.js.map +1 -0
- package/dist/component/providers/deezer/mappers.d.ts +18 -0
- package/dist/component/providers/deezer/mappers.d.ts.map +1 -0
- package/dist/component/providers/deezer/mappers.js +53 -0
- package/dist/component/providers/deezer/mappers.js.map +1 -0
- package/dist/component/providers/deezer/types.d.ts +59 -0
- package/dist/component/providers/deezer/types.d.ts.map +1 -0
- package/dist/component/providers/deezer/types.js +7 -0
- package/dist/component/providers/deezer/types.js.map +1 -0
- package/dist/component/providers/fetch.d.ts +69 -0
- package/dist/component/providers/fetch.d.ts.map +1 -0
- package/dist/component/providers/fetch.js +145 -0
- package/dist/component/providers/fetch.js.map +1 -0
- package/dist/component/providers/musicbrainz/impl.d.ts +36 -0
- package/dist/component/providers/musicbrainz/impl.d.ts.map +1 -0
- package/dist/component/providers/musicbrainz/impl.js +74 -0
- package/dist/component/providers/musicbrainz/impl.js.map +1 -0
- package/dist/component/providers/musicbrainz/mappers.d.ts +10 -0
- package/dist/component/providers/musicbrainz/mappers.d.ts.map +1 -0
- package/dist/component/providers/musicbrainz/mappers.js +32 -0
- package/dist/component/providers/musicbrainz/mappers.js.map +1 -0
- package/dist/component/providers/musicbrainz/types.d.ts +24 -0
- package/dist/component/providers/musicbrainz/types.d.ts.map +1 -0
- package/dist/component/providers/musicbrainz/types.js +7 -0
- package/dist/component/providers/musicbrainz/types.js.map +1 -0
- package/dist/component/providers/registry.d.ts +22 -0
- package/dist/component/providers/registry.d.ts.map +1 -0
- package/dist/component/providers/registry.js +33 -0
- package/dist/component/providers/registry.js.map +1 -0
- package/dist/component/providers/spotify/client.d.ts +25 -0
- package/dist/component/providers/spotify/client.d.ts.map +1 -0
- package/dist/component/providers/spotify/client.js +30 -0
- package/dist/component/providers/spotify/client.js.map +1 -0
- package/dist/component/providers/spotify/impl.d.ts +51 -0
- package/dist/component/providers/spotify/impl.d.ts.map +1 -0
- package/dist/component/providers/spotify/impl.js +131 -0
- package/dist/component/providers/spotify/impl.js.map +1 -0
- package/dist/component/providers/spotify/mappers.d.ts +18 -0
- package/dist/component/providers/spotify/mappers.d.ts.map +1 -0
- package/dist/component/providers/spotify/mappers.js +60 -0
- package/dist/component/providers/spotify/mappers.js.map +1 -0
- package/dist/component/providers/spotify/types.d.ts +97 -0
- package/dist/component/providers/spotify/types.d.ts.map +1 -0
- package/dist/component/providers/spotify/types.js +7 -0
- package/dist/component/providers/spotify/types.js.map +1 -0
- package/dist/component/providers/types.d.ts +84 -0
- package/dist/component/providers/types.d.ts.map +1 -0
- package/dist/component/providers/types.js +10 -0
- package/dist/component/providers/types.js.map +1 -0
- package/dist/component/providers/wikidata/impl.d.ts +34 -0
- package/dist/component/providers/wikidata/impl.d.ts.map +1 -0
- package/dist/component/providers/wikidata/impl.js +88 -0
- package/dist/component/providers/wikidata/impl.js.map +1 -0
- package/dist/component/providers/wikidata/mappers.d.ts +11 -0
- package/dist/component/providers/wikidata/mappers.d.ts.map +1 -0
- package/dist/component/providers/wikidata/mappers.js +53 -0
- package/dist/component/providers/wikidata/mappers.js.map +1 -0
- package/dist/component/providers/wikidata/types.d.ts +35 -0
- package/dist/component/providers/wikidata/types.d.ts.map +1 -0
- package/dist/component/providers/wikidata/types.js +8 -0
- package/dist/component/providers/wikidata/types.js.map +1 -0
- package/dist/component/queries.d.ts +103 -0
- package/dist/component/queries.d.ts.map +1 -0
- package/dist/component/queries.js +45 -0
- package/dist/component/queries.js.map +1 -0
- package/dist/component/schema.d.ts +566 -0
- package/dist/component/schema.d.ts.map +1 -0
- package/dist/component/schema.js +59 -0
- package/dist/component/schema.js.map +1 -0
- package/dist/component/sources/actions.d.ts +51 -0
- package/dist/component/sources/actions.d.ts.map +1 -0
- package/dist/component/sources/actions.js +164 -0
- package/dist/component/sources/actions.js.map +1 -0
- package/dist/component/sources/mutations.d.ts +25 -0
- package/dist/component/sources/mutations.d.ts.map +1 -0
- package/dist/component/sources/mutations.js +64 -0
- package/dist/component/sources/mutations.js.map +1 -0
- package/dist/component/sources/queries.d.ts +19 -0
- package/dist/component/sources/queries.d.ts.map +1 -0
- package/dist/component/sources/queries.js +20 -0
- package/dist/component/sources/queries.js.map +1 -0
- package/dist/component/sync/lifecycle.d.ts +46 -0
- package/dist/component/sync/lifecycle.d.ts.map +1 -0
- package/dist/component/sync/lifecycle.js +87 -0
- package/dist/component/sync/lifecycle.js.map +1 -0
- package/dist/component/sync/mutations.d.ts +101 -0
- package/dist/component/sync/mutations.d.ts.map +1 -0
- package/dist/component/sync/mutations.js +132 -0
- package/dist/component/sync/mutations.js.map +1 -0
- package/dist/component/sync/queries.d.ts +69 -0
- package/dist/component/sync/queries.d.ts.map +1 -0
- package/dist/component/sync/queries.js +32 -0
- package/dist/component/sync/queries.js.map +1 -0
- package/dist/component/validators.d.ts +1112 -0
- package/dist/component/validators.d.ts.map +1 -0
- package/dist/component/validators.js +317 -0
- package/dist/component/validators.js.map +1 -0
- package/dist/react/index.d.ts +42 -0
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/index.js +43 -0
- package/dist/react/index.js.map +1 -0
- package/dist/shared.d.ts +28 -0
- package/dist/shared.d.ts.map +1 -0
- package/dist/shared.js +24 -0
- package/dist/shared.js.map +1 -0
- package/package.json +126 -0
- package/src/client/index.ts +558 -0
- package/src/client/types.ts +117 -0
- package/src/component/_generated/api.ts +141 -0
- package/src/component/_generated/component.ts +1814 -0
- package/src/component/_generated/dataModel.ts +60 -0
- package/src/component/_generated/server.ts +169 -0
- package/src/component/actions.ts +156 -0
- package/src/component/catalog/browse_order.test.ts +55 -0
- package/src/component/catalog/browse_order.ts +43 -0
- package/src/component/catalog/field_source_policy.test.ts +47 -0
- package/src/component/catalog/field_source_policy.ts +44 -0
- package/src/component/catalog/merge.test.ts +187 -0
- package/src/component/catalog/merge.ts +134 -0
- package/src/component/catalog/mutations.ts +328 -0
- package/src/component/catalog/promote.ts +40 -0
- package/src/component/catalog/queries.ts +220 -0
- package/src/component/config/mutations.ts +29 -0
- package/src/component/config/queries.ts +20 -0
- package/src/component/convex.config.ts +32 -0
- package/src/component/crons.test.ts +8 -0
- package/src/component/crons.ts +52 -0
- package/src/component/imports/actions.ts +553 -0
- package/src/component/imports/dedupe.test.ts +66 -0
- package/src/component/imports/dedupe.ts +54 -0
- package/src/component/imports/mutations.ts +203 -0
- package/src/component/imports/queries.ts +25 -0
- package/src/component/imports/state.test.ts +60 -0
- package/src/component/imports/state.ts +74 -0
- package/src/component/mutations.ts +96 -0
- package/src/component/providers/actions.ts +127 -0
- package/src/component/providers/apple/impl.test.ts +267 -0
- package/src/component/providers/apple/impl.ts +224 -0
- package/src/component/providers/apple/jwt.test.ts +153 -0
- package/src/component/providers/apple/jwt.ts +99 -0
- package/src/component/providers/apple/mappers.test.ts +190 -0
- package/src/component/providers/apple/mappers.ts +100 -0
- package/src/component/providers/apple/types.ts +77 -0
- package/src/component/providers/deezer/impl.test.ts +109 -0
- package/src/component/providers/deezer/impl.ts +179 -0
- package/src/component/providers/deezer/mappers.test.ts +78 -0
- package/src/component/providers/deezer/mappers.ts +72 -0
- package/src/component/providers/deezer/types.ts +59 -0
- package/src/component/providers/fetch.test.ts +282 -0
- package/src/component/providers/fetch.ts +190 -0
- package/src/component/providers/musicbrainz/impl.test.ts +79 -0
- package/src/component/providers/musicbrainz/impl.ts +132 -0
- package/src/component/providers/musicbrainz/mappers.test.ts +75 -0
- package/src/component/providers/musicbrainz/mappers.ts +34 -0
- package/src/component/providers/musicbrainz/types.ts +20 -0
- package/src/component/providers/registry.test.ts +14 -0
- package/src/component/providers/registry.ts +48 -0
- package/src/component/providers/spotify/client.test.ts +80 -0
- package/src/component/providers/spotify/client.ts +57 -0
- package/src/component/providers/spotify/impl.test.ts +311 -0
- package/src/component/providers/spotify/impl.ts +230 -0
- package/src/component/providers/spotify/mappers.test.ts +182 -0
- package/src/component/providers/spotify/mappers.ts +81 -0
- package/src/component/providers/spotify/types.ts +101 -0
- package/src/component/providers/types.ts +95 -0
- package/src/component/providers/wikidata/impl.test.ts +81 -0
- package/src/component/providers/wikidata/impl.ts +139 -0
- package/src/component/providers/wikidata/mappers.test.ts +62 -0
- package/src/component/providers/wikidata/mappers.ts +66 -0
- package/src/component/providers/wikidata/types.ts +28 -0
- package/src/component/queries.ts +51 -0
- package/src/component/schema.ts +81 -0
- package/src/component/sources/actions.ts +177 -0
- package/src/component/sources/mutations.ts +67 -0
- package/src/component/sources/queries.ts +21 -0
- package/src/component/sync/lifecycle.test.ts +72 -0
- package/src/component/sync/lifecycle.ts +121 -0
- package/src/component/sync/mutations.ts +154 -0
- package/src/component/sync/queries.ts +33 -0
- package/src/component/validators.ts +411 -0
- package/src/react/index.test.ts +78 -0
- package/src/react/index.ts +112 -0
- package/src/shared.ts +32 -0
- package/src/test.ts +22 -0
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The sync + repair lifecycle logic for catalog rows (songtrivia's sync-status /
|
|
3
|
+
* repair machines, generalized). Pure transition + scheduling logic; the
|
|
4
|
+
* mutations + crons enforce it on the rows. Two machines:
|
|
5
|
+
* - sync-status: `pending → running → synced | failed → stale` (freshness).
|
|
6
|
+
* - repair-status: `clean → needs_repair → repairing → clean | failed_repair`.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/** Freshness-sync states of a catalog row. */
|
|
10
|
+
export type SyncStatus =
|
|
11
|
+
| "pending"
|
|
12
|
+
| "running"
|
|
13
|
+
| "synced"
|
|
14
|
+
| "failed"
|
|
15
|
+
| "stale";
|
|
16
|
+
|
|
17
|
+
/** Data-quality repair states of a catalog row. */
|
|
18
|
+
export type RepairStatus =
|
|
19
|
+
| "clean"
|
|
20
|
+
| "needs_repair"
|
|
21
|
+
| "repairing"
|
|
22
|
+
| "failed_repair";
|
|
23
|
+
|
|
24
|
+
const SYNC_TRANSITIONS: Record<SyncStatus, readonly SyncStatus[]> = {
|
|
25
|
+
pending: ["running", "synced", "failed"],
|
|
26
|
+
running: ["synced", "failed", "stale"],
|
|
27
|
+
synced: ["stale"],
|
|
28
|
+
failed: ["running", "synced", "failed", "stale"],
|
|
29
|
+
stale: ["running", "synced"],
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const REPAIR_TRANSITIONS: Record<RepairStatus, readonly RepairStatus[]> = {
|
|
33
|
+
clean: ["needs_repair"],
|
|
34
|
+
needs_repair: ["repairing"],
|
|
35
|
+
repairing: ["clean", "needs_repair", "failed_repair"],
|
|
36
|
+
failed_repair: [],
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/** Whether `from → to` is a legal sync transition. */
|
|
40
|
+
export function canSyncTransition(from: SyncStatus, to: SyncStatus): boolean {
|
|
41
|
+
return SYNC_TRANSITIONS[from].includes(to);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Assert a legal sync transition. */
|
|
45
|
+
export function assertSyncTransition(from: SyncStatus, to: SyncStatus): void {
|
|
46
|
+
if (!canSyncTransition(from, to)) {
|
|
47
|
+
throw new Error(`Invalid sync transition: ${from} -> ${to}`);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Whether `from → to` is a legal repair transition. */
|
|
52
|
+
export function canRepairTransition(
|
|
53
|
+
from: RepairStatus,
|
|
54
|
+
to: RepairStatus,
|
|
55
|
+
): boolean {
|
|
56
|
+
return REPAIR_TRANSITIONS[from].includes(to);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Assert a legal repair transition. */
|
|
60
|
+
export function assertRepairTransition(
|
|
61
|
+
from: RepairStatus,
|
|
62
|
+
to: RepairStatus,
|
|
63
|
+
): void {
|
|
64
|
+
if (!canRepairTransition(from, to)) {
|
|
65
|
+
throw new Error(`Invalid repair transition: ${from} -> ${to}`);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Entity sync retry backoff: `[1h, 6h, 24h]` then `stale`. */
|
|
70
|
+
export const SYNC_RETRY_DELAYS_MS: readonly number[] = [
|
|
71
|
+
60 * 60 * 1000,
|
|
72
|
+
6 * 60 * 60 * 1000,
|
|
73
|
+
24 * 60 * 60 * 1000,
|
|
74
|
+
];
|
|
75
|
+
|
|
76
|
+
/** Max entity sync retries before `stale`. */
|
|
77
|
+
export const MAX_SYNC_RETRIES = 3;
|
|
78
|
+
|
|
79
|
+
/** Max repair attempts before `failed_repair`. */
|
|
80
|
+
export const MAX_REPAIR_ATTEMPTS = 3;
|
|
81
|
+
|
|
82
|
+
/** Backoff before the next sync retry (last value past the end). */
|
|
83
|
+
export function syncRetryDelayMs(retryCount: number): number {
|
|
84
|
+
return SYNC_RETRY_DELAYS_MS[retryCount] ?? 24 * 60 * 60 * 1000;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Whether another sync retry is within budget. */
|
|
88
|
+
export function shouldSyncRetry(retryCount: number): boolean {
|
|
89
|
+
return retryCount < MAX_SYNC_RETRIES;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** Staleness windows by popularity tier (songtrivia defaults). */
|
|
93
|
+
export const STALENESS_MS = {
|
|
94
|
+
high: 7 * 24 * 60 * 60 * 1000,
|
|
95
|
+
medium: 30 * 24 * 60 * 60 * 1000,
|
|
96
|
+
low: 90 * 24 * 60 * 60 * 1000,
|
|
97
|
+
} as const;
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* The freshness window for an entity by popularity: HIGH (≥70) = 7d, MEDIUM
|
|
101
|
+
* (≥40) = 30d, LOW (or unknown) = 90d. Volatile-popular rows refresh faster.
|
|
102
|
+
*/
|
|
103
|
+
export function stalenessWindowMs(popularity: number | undefined): number {
|
|
104
|
+
if (popularity === undefined) return STALENESS_MS.low;
|
|
105
|
+
if (popularity >= 70) return STALENESS_MS.high;
|
|
106
|
+
if (popularity >= 40) return STALENESS_MS.medium;
|
|
107
|
+
return STALENESS_MS.low;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Whether a row is past its freshness window. A row never synced (no
|
|
112
|
+
* `lastSyncedAt`) is stale.
|
|
113
|
+
*/
|
|
114
|
+
export function isStale(
|
|
115
|
+
lastSyncedAt: number | undefined,
|
|
116
|
+
popularity: number | undefined,
|
|
117
|
+
now: number,
|
|
118
|
+
): boolean {
|
|
119
|
+
if (lastSyncedAt === undefined) return true;
|
|
120
|
+
return now - lastSyncedAt > stalenessWindowMs(popularity);
|
|
121
|
+
}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { v } from "convex/values";
|
|
2
|
+
import { type MutationCtx, mutation } from "../_generated/server.js";
|
|
3
|
+
import { artistDoc, trackDoc } from "../validators.js";
|
|
4
|
+
import { isStale } from "./lifecycle.js";
|
|
5
|
+
|
|
6
|
+
/** Default rows scanned per markStale run. */
|
|
7
|
+
const DEFAULT_LIMIT = 200;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Flip past-freshness-window catalog rows from `synced` to `stale` for a kind.
|
|
11
|
+
* The freshness window scales by popularity (volatile-popular rows go stale
|
|
12
|
+
* sooner). Returns how many were newly marked. The auto-import sweep re-syncs
|
|
13
|
+
* stale rows; this is the detection half. Idempotent + bounded; mount-safe.
|
|
14
|
+
*/
|
|
15
|
+
export const markStale = mutation({
|
|
16
|
+
args: {
|
|
17
|
+
kind: v.union(v.literal("artist"), v.literal("track")),
|
|
18
|
+
limit: v.optional(v.number()),
|
|
19
|
+
now: v.optional(v.number()),
|
|
20
|
+
},
|
|
21
|
+
returns: v.number(),
|
|
22
|
+
handler: async (ctx, args) => {
|
|
23
|
+
const at = args.now ?? Date.now();
|
|
24
|
+
const take = args.limit ?? DEFAULT_LIMIT;
|
|
25
|
+
if (args.kind === "artist") {
|
|
26
|
+
const rows = await ctx.db
|
|
27
|
+
.query("artists")
|
|
28
|
+
.withIndex("by_sync", (q) => q.eq("syncStatus", "synced"))
|
|
29
|
+
.take(take);
|
|
30
|
+
const stale = rows.filter((row) =>
|
|
31
|
+
isStale(row.lastSyncedAt, row.popularity, at),
|
|
32
|
+
);
|
|
33
|
+
await Promise.all(
|
|
34
|
+
stale.map((row) =>
|
|
35
|
+
ctx.db.patch("artists", row._id, {
|
|
36
|
+
syncStatus: "stale",
|
|
37
|
+
updatedAt: at,
|
|
38
|
+
}),
|
|
39
|
+
),
|
|
40
|
+
);
|
|
41
|
+
return stale.length;
|
|
42
|
+
}
|
|
43
|
+
const rows = await ctx.db
|
|
44
|
+
.query("tracks")
|
|
45
|
+
.withIndex("by_sync", (q) => q.eq("syncStatus", "synced"))
|
|
46
|
+
.take(take);
|
|
47
|
+
const stale = rows.filter((row) =>
|
|
48
|
+
isStale(row.lastSyncedAt, row.popularity, at),
|
|
49
|
+
);
|
|
50
|
+
await Promise.all(
|
|
51
|
+
stale.map((row) =>
|
|
52
|
+
ctx.db.patch("tracks", row._id, {
|
|
53
|
+
syncStatus: "stale",
|
|
54
|
+
updatedAt: at,
|
|
55
|
+
}),
|
|
56
|
+
),
|
|
57
|
+
);
|
|
58
|
+
return stale.length;
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
/** Default lease before a `running` row is considered stuck (10 min). */
|
|
63
|
+
const DEFAULT_SYNC_LEASE_MS = 10 * 60 * 1000;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Atomically claim the next `stale` catalog row of a kind for re-sync: find one
|
|
67
|
+
* stale row via the index and flip it to `running` in the SAME transaction. The
|
|
68
|
+
* find + claim are one mutation, so two concurrent refresh sweeps can never both
|
|
69
|
+
* take the same row — Convex serializes the conflicting patch and the loser
|
|
70
|
+
* re-reads (the row is now `running`, no longer `stale`) and moves on. Returns
|
|
71
|
+
* the claimed row (so the caller has its `providers`), or `null` when none are
|
|
72
|
+
* stale. The re-import's upsert returns it to `synced`; `recoverStuckSyncs`
|
|
73
|
+
* salvages a lease whose holder crashed. Replaces a blind patch-by-id, which had
|
|
74
|
+
* no compare-and-set and could double-claim across action/mutation boundaries.
|
|
75
|
+
*/
|
|
76
|
+
export const claimNextStale = mutation({
|
|
77
|
+
args: { kind: v.union(v.literal("artist"), v.literal("track")) },
|
|
78
|
+
returns: v.union(v.null(), artistDoc, trackDoc),
|
|
79
|
+
handler: async (ctx, args) => {
|
|
80
|
+
const now = Date.now();
|
|
81
|
+
if (args.kind === "artist") {
|
|
82
|
+
const row = await ctx.db
|
|
83
|
+
.query("artists")
|
|
84
|
+
.withIndex("by_sync", (q) => q.eq("syncStatus", "stale"))
|
|
85
|
+
.first();
|
|
86
|
+
if (row === null) return null;
|
|
87
|
+
await ctx.db.patch("artists", row._id, { syncStatus: "running", updatedAt: now });
|
|
88
|
+
return row;
|
|
89
|
+
}
|
|
90
|
+
const row = await ctx.db
|
|
91
|
+
.query("tracks")
|
|
92
|
+
.withIndex("by_sync", (q) => q.eq("syncStatus", "stale"))
|
|
93
|
+
.first();
|
|
94
|
+
if (row === null) return null;
|
|
95
|
+
await ctx.db.patch("tracks", row._id, { syncStatus: "running", updatedAt: now });
|
|
96
|
+
return row;
|
|
97
|
+
},
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Recover rows stuck in `running` past the lease (a re-sync that crashed before
|
|
102
|
+
* completing) by salvaging them back to `stale` for re-pickup. Bounded +
|
|
103
|
+
* idempotent; returns how many were recovered.
|
|
104
|
+
*
|
|
105
|
+
* Stuck = `updatedAt` older than `leaseMs` (default 10 min). A refresh re-syncs a
|
|
106
|
+
* single row (one provider fetch + a few upserts) in seconds, so the lease is far
|
|
107
|
+
* longer than a healthy run; only a genuinely crashed run is reclaimed. If a
|
|
108
|
+
* deployment runs much slower single-row refreshes, raise `leaseMs` above that
|
|
109
|
+
* duration so a still-running sync is never false-positive reclaimed.
|
|
110
|
+
*/
|
|
111
|
+
export const recoverStuckSyncs = mutation({
|
|
112
|
+
args: {
|
|
113
|
+
kind: v.union(v.literal("artist"), v.literal("track")),
|
|
114
|
+
leaseMs: v.optional(v.number()),
|
|
115
|
+
now: v.optional(v.number()),
|
|
116
|
+
limit: v.optional(v.number()),
|
|
117
|
+
},
|
|
118
|
+
returns: v.number(),
|
|
119
|
+
handler: async (ctx, args) => {
|
|
120
|
+
const at = args.now ?? Date.now();
|
|
121
|
+
const cutoff = at - (args.leaseMs ?? DEFAULT_SYNC_LEASE_MS);
|
|
122
|
+
const take = args.limit ?? DEFAULT_LIMIT;
|
|
123
|
+
if (args.kind === "artist") {
|
|
124
|
+
const rows = await ctx.db
|
|
125
|
+
.query("artists")
|
|
126
|
+
.withIndex("by_sync", (q) => q.eq("syncStatus", "running"))
|
|
127
|
+
.take(take);
|
|
128
|
+
const stuck = rows.filter((row) => row.updatedAt < cutoff);
|
|
129
|
+
await Promise.all(
|
|
130
|
+
stuck.map((row) =>
|
|
131
|
+
ctx.db.patch("artists", row._id, {
|
|
132
|
+
syncStatus: "stale",
|
|
133
|
+
updatedAt: at,
|
|
134
|
+
}),
|
|
135
|
+
),
|
|
136
|
+
);
|
|
137
|
+
return stuck.length;
|
|
138
|
+
}
|
|
139
|
+
const rows = await ctx.db
|
|
140
|
+
.query("tracks")
|
|
141
|
+
.withIndex("by_sync", (q) => q.eq("syncStatus", "running"))
|
|
142
|
+
.take(take);
|
|
143
|
+
const stuck = rows.filter((row) => row.updatedAt < cutoff);
|
|
144
|
+
await Promise.all(
|
|
145
|
+
stuck.map((row) =>
|
|
146
|
+
ctx.db.patch("tracks", row._id, {
|
|
147
|
+
syncStatus: "stale",
|
|
148
|
+
updatedAt: at,
|
|
149
|
+
}),
|
|
150
|
+
),
|
|
151
|
+
);
|
|
152
|
+
return stuck.length;
|
|
153
|
+
},
|
|
154
|
+
});
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { v } from "convex/values";
|
|
2
|
+
import { query } from "../_generated/server.js";
|
|
3
|
+
import { artistDoc, trackDoc } from "../validators.js";
|
|
4
|
+
|
|
5
|
+
/** Default rows returned per listStale call. */
|
|
6
|
+
const DEFAULT_LIMIT = 100;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* List catalog rows currently marked `stale` for a kind (bounded). The
|
|
10
|
+
* auto-import refresh sweep consumes this to re-sync them; also useful for
|
|
11
|
+
* "what's stale" observability. Rows come from a query (always existing), so
|
|
12
|
+
* the refresh runner needs no load-by-id.
|
|
13
|
+
*/
|
|
14
|
+
export const listStale = query({
|
|
15
|
+
args: {
|
|
16
|
+
kind: v.union(v.literal("artist"), v.literal("track")),
|
|
17
|
+
limit: v.optional(v.number()),
|
|
18
|
+
},
|
|
19
|
+
returns: v.union(v.array(artistDoc), v.array(trackDoc)),
|
|
20
|
+
handler: async (ctx, args) => {
|
|
21
|
+
const take = args.limit ?? DEFAULT_LIMIT;
|
|
22
|
+
if (args.kind === "artist") {
|
|
23
|
+
return await ctx.db
|
|
24
|
+
.query("artists")
|
|
25
|
+
.withIndex("by_sync", (q) => q.eq("syncStatus", "stale"))
|
|
26
|
+
.take(take);
|
|
27
|
+
}
|
|
28
|
+
return await ctx.db
|
|
29
|
+
.query("tracks")
|
|
30
|
+
.withIndex("by_sync", (q) => q.eq("syncStatus", "stale"))
|
|
31
|
+
.take(take);
|
|
32
|
+
},
|
|
33
|
+
});
|