@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.
Files changed (301) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +169 -0
  3. package/dist/client/index.d.ts +284 -0
  4. package/dist/client/index.d.ts.map +1 -0
  5. package/dist/client/index.js +166 -0
  6. package/dist/client/index.js.map +1 -0
  7. package/dist/client/types.d.ts +100 -0
  8. package/dist/client/types.d.ts.map +1 -0
  9. package/dist/client/types.js +2 -0
  10. package/dist/client/types.js.map +1 -0
  11. package/dist/component/_generated/api.d.ts +125 -0
  12. package/dist/component/_generated/api.d.ts.map +1 -0
  13. package/dist/component/_generated/api.js +31 -0
  14. package/dist/component/_generated/api.js.map +1 -0
  15. package/dist/component/_generated/component.d.ts +1143 -0
  16. package/dist/component/_generated/component.d.ts.map +1 -0
  17. package/dist/component/_generated/component.js +11 -0
  18. package/dist/component/_generated/component.js.map +1 -0
  19. package/dist/component/_generated/dataModel.d.ts +46 -0
  20. package/dist/component/_generated/dataModel.d.ts.map +1 -0
  21. package/dist/component/_generated/dataModel.js +11 -0
  22. package/dist/component/_generated/dataModel.js.map +1 -0
  23. package/dist/component/_generated/server.d.ts +133 -0
  24. package/dist/component/_generated/server.d.ts.map +1 -0
  25. package/dist/component/_generated/server.js +80 -0
  26. package/dist/component/_generated/server.js.map +1 -0
  27. package/dist/component/actions.d.ts +144 -0
  28. package/dist/component/actions.d.ts.map +1 -0
  29. package/dist/component/actions.js +125 -0
  30. package/dist/component/actions.js.map +1 -0
  31. package/dist/component/catalog/browse_order.d.ts +22 -0
  32. package/dist/component/catalog/browse_order.d.ts.map +1 -0
  33. package/dist/component/catalog/browse_order.js +37 -0
  34. package/dist/component/catalog/browse_order.js.map +1 -0
  35. package/dist/component/catalog/field_source_policy.d.ts +30 -0
  36. package/dist/component/catalog/field_source_policy.d.ts.map +1 -0
  37. package/dist/component/catalog/field_source_policy.js +36 -0
  38. package/dist/component/catalog/field_source_policy.js.map +1 -0
  39. package/dist/component/catalog/merge.d.ts +58 -0
  40. package/dist/component/catalog/merge.d.ts.map +1 -0
  41. package/dist/component/catalog/merge.js +74 -0
  42. package/dist/component/catalog/merge.js.map +1 -0
  43. package/dist/component/catalog/mutations.d.ts +78 -0
  44. package/dist/component/catalog/mutations.d.ts.map +1 -0
  45. package/dist/component/catalog/mutations.js +259 -0
  46. package/dist/component/catalog/mutations.js.map +1 -0
  47. package/dist/component/catalog/promote.d.ts +21 -0
  48. package/dist/component/catalog/promote.d.ts.map +1 -0
  49. package/dist/component/catalog/promote.js +25 -0
  50. package/dist/component/catalog/promote.js.map +1 -0
  51. package/dist/component/catalog/queries.d.ts +421 -0
  52. package/dist/component/catalog/queries.d.ts.map +1 -0
  53. package/dist/component/catalog/queries.js +168 -0
  54. package/dist/component/catalog/queries.js.map +1 -0
  55. package/dist/component/config/mutations.d.ts +11 -0
  56. package/dist/component/config/mutations.d.ts.map +1 -0
  57. package/dist/component/config/mutations.js +30 -0
  58. package/dist/component/config/mutations.js.map +1 -0
  59. package/dist/component/config/queries.d.ts +9 -0
  60. package/dist/component/config/queries.d.ts.map +1 -0
  61. package/dist/component/config/queries.js +20 -0
  62. package/dist/component/config/queries.js.map +1 -0
  63. package/dist/component/convex.config.d.ts +3 -0
  64. package/dist/component/convex.config.d.ts.map +1 -0
  65. package/dist/component/convex.config.js +30 -0
  66. package/dist/component/convex.config.js.map +1 -0
  67. package/dist/component/crons.d.ts +11 -0
  68. package/dist/component/crons.d.ts.map +1 -0
  69. package/dist/component/crons.js +41 -0
  70. package/dist/component/crons.js.map +1 -0
  71. package/dist/component/imports/actions.d.ts +119 -0
  72. package/dist/component/imports/actions.d.ts.map +1 -0
  73. package/dist/component/imports/actions.js +442 -0
  74. package/dist/component/imports/actions.js.map +1 -0
  75. package/dist/component/imports/dedupe.d.ts +29 -0
  76. package/dist/component/imports/dedupe.d.ts.map +1 -0
  77. package/dist/component/imports/dedupe.js +33 -0
  78. package/dist/component/imports/dedupe.js.map +1 -0
  79. package/dist/component/imports/mutations.d.ts +59 -0
  80. package/dist/component/imports/mutations.d.ts.map +1 -0
  81. package/dist/component/imports/mutations.js +151 -0
  82. package/dist/component/imports/mutations.js.map +1 -0
  83. package/dist/component/imports/queries.d.ts +68 -0
  84. package/dist/component/imports/queries.d.ts.map +1 -0
  85. package/dist/component/imports/queries.js +22 -0
  86. package/dist/component/imports/queries.js.map +1 -0
  87. package/dist/component/imports/state.d.ts +36 -0
  88. package/dist/component/imports/state.d.ts.map +1 -0
  89. package/dist/component/imports/state.js +62 -0
  90. package/dist/component/imports/state.js.map +1 -0
  91. package/dist/component/mutations.d.ts +55 -0
  92. package/dist/component/mutations.d.ts.map +1 -0
  93. package/dist/component/mutations.js +74 -0
  94. package/dist/component/mutations.js.map +1 -0
  95. package/dist/component/providers/actions.d.ts +26 -0
  96. package/dist/component/providers/actions.d.ts.map +1 -0
  97. package/dist/component/providers/actions.js +92 -0
  98. package/dist/component/providers/actions.js.map +1 -0
  99. package/dist/component/providers/apple/impl.d.ts +52 -0
  100. package/dist/component/providers/apple/impl.d.ts.map +1 -0
  101. package/dist/component/providers/apple/impl.js +127 -0
  102. package/dist/component/providers/apple/impl.js.map +1 -0
  103. package/dist/component/providers/apple/jwt.d.ts +32 -0
  104. package/dist/component/providers/apple/jwt.d.ts.map +1 -0
  105. package/dist/component/providers/apple/jwt.js +68 -0
  106. package/dist/component/providers/apple/jwt.js.map +1 -0
  107. package/dist/component/providers/apple/mappers.d.ts +20 -0
  108. package/dist/component/providers/apple/mappers.d.ts.map +1 -0
  109. package/dist/component/providers/apple/mappers.js +68 -0
  110. package/dist/component/providers/apple/mappers.js.map +1 -0
  111. package/dist/component/providers/apple/types.d.ts +80 -0
  112. package/dist/component/providers/apple/types.d.ts.map +1 -0
  113. package/dist/component/providers/apple/types.js +7 -0
  114. package/dist/component/providers/apple/types.js.map +1 -0
  115. package/dist/component/providers/deezer/impl.d.ts +37 -0
  116. package/dist/component/providers/deezer/impl.d.ts.map +1 -0
  117. package/dist/component/providers/deezer/impl.js +99 -0
  118. package/dist/component/providers/deezer/impl.js.map +1 -0
  119. package/dist/component/providers/deezer/mappers.d.ts +18 -0
  120. package/dist/component/providers/deezer/mappers.d.ts.map +1 -0
  121. package/dist/component/providers/deezer/mappers.js +53 -0
  122. package/dist/component/providers/deezer/mappers.js.map +1 -0
  123. package/dist/component/providers/deezer/types.d.ts +59 -0
  124. package/dist/component/providers/deezer/types.d.ts.map +1 -0
  125. package/dist/component/providers/deezer/types.js +7 -0
  126. package/dist/component/providers/deezer/types.js.map +1 -0
  127. package/dist/component/providers/fetch.d.ts +69 -0
  128. package/dist/component/providers/fetch.d.ts.map +1 -0
  129. package/dist/component/providers/fetch.js +145 -0
  130. package/dist/component/providers/fetch.js.map +1 -0
  131. package/dist/component/providers/musicbrainz/impl.d.ts +36 -0
  132. package/dist/component/providers/musicbrainz/impl.d.ts.map +1 -0
  133. package/dist/component/providers/musicbrainz/impl.js +74 -0
  134. package/dist/component/providers/musicbrainz/impl.js.map +1 -0
  135. package/dist/component/providers/musicbrainz/mappers.d.ts +10 -0
  136. package/dist/component/providers/musicbrainz/mappers.d.ts.map +1 -0
  137. package/dist/component/providers/musicbrainz/mappers.js +32 -0
  138. package/dist/component/providers/musicbrainz/mappers.js.map +1 -0
  139. package/dist/component/providers/musicbrainz/types.d.ts +24 -0
  140. package/dist/component/providers/musicbrainz/types.d.ts.map +1 -0
  141. package/dist/component/providers/musicbrainz/types.js +7 -0
  142. package/dist/component/providers/musicbrainz/types.js.map +1 -0
  143. package/dist/component/providers/registry.d.ts +22 -0
  144. package/dist/component/providers/registry.d.ts.map +1 -0
  145. package/dist/component/providers/registry.js +33 -0
  146. package/dist/component/providers/registry.js.map +1 -0
  147. package/dist/component/providers/spotify/client.d.ts +25 -0
  148. package/dist/component/providers/spotify/client.d.ts.map +1 -0
  149. package/dist/component/providers/spotify/client.js +30 -0
  150. package/dist/component/providers/spotify/client.js.map +1 -0
  151. package/dist/component/providers/spotify/impl.d.ts +51 -0
  152. package/dist/component/providers/spotify/impl.d.ts.map +1 -0
  153. package/dist/component/providers/spotify/impl.js +131 -0
  154. package/dist/component/providers/spotify/impl.js.map +1 -0
  155. package/dist/component/providers/spotify/mappers.d.ts +18 -0
  156. package/dist/component/providers/spotify/mappers.d.ts.map +1 -0
  157. package/dist/component/providers/spotify/mappers.js +60 -0
  158. package/dist/component/providers/spotify/mappers.js.map +1 -0
  159. package/dist/component/providers/spotify/types.d.ts +97 -0
  160. package/dist/component/providers/spotify/types.d.ts.map +1 -0
  161. package/dist/component/providers/spotify/types.js +7 -0
  162. package/dist/component/providers/spotify/types.js.map +1 -0
  163. package/dist/component/providers/types.d.ts +84 -0
  164. package/dist/component/providers/types.d.ts.map +1 -0
  165. package/dist/component/providers/types.js +10 -0
  166. package/dist/component/providers/types.js.map +1 -0
  167. package/dist/component/providers/wikidata/impl.d.ts +34 -0
  168. package/dist/component/providers/wikidata/impl.d.ts.map +1 -0
  169. package/dist/component/providers/wikidata/impl.js +88 -0
  170. package/dist/component/providers/wikidata/impl.js.map +1 -0
  171. package/dist/component/providers/wikidata/mappers.d.ts +11 -0
  172. package/dist/component/providers/wikidata/mappers.d.ts.map +1 -0
  173. package/dist/component/providers/wikidata/mappers.js +53 -0
  174. package/dist/component/providers/wikidata/mappers.js.map +1 -0
  175. package/dist/component/providers/wikidata/types.d.ts +35 -0
  176. package/dist/component/providers/wikidata/types.d.ts.map +1 -0
  177. package/dist/component/providers/wikidata/types.js +8 -0
  178. package/dist/component/providers/wikidata/types.js.map +1 -0
  179. package/dist/component/queries.d.ts +103 -0
  180. package/dist/component/queries.d.ts.map +1 -0
  181. package/dist/component/queries.js +45 -0
  182. package/dist/component/queries.js.map +1 -0
  183. package/dist/component/schema.d.ts +566 -0
  184. package/dist/component/schema.d.ts.map +1 -0
  185. package/dist/component/schema.js +59 -0
  186. package/dist/component/schema.js.map +1 -0
  187. package/dist/component/sources/actions.d.ts +51 -0
  188. package/dist/component/sources/actions.d.ts.map +1 -0
  189. package/dist/component/sources/actions.js +164 -0
  190. package/dist/component/sources/actions.js.map +1 -0
  191. package/dist/component/sources/mutations.d.ts +25 -0
  192. package/dist/component/sources/mutations.d.ts.map +1 -0
  193. package/dist/component/sources/mutations.js +64 -0
  194. package/dist/component/sources/mutations.js.map +1 -0
  195. package/dist/component/sources/queries.d.ts +19 -0
  196. package/dist/component/sources/queries.d.ts.map +1 -0
  197. package/dist/component/sources/queries.js +20 -0
  198. package/dist/component/sources/queries.js.map +1 -0
  199. package/dist/component/sync/lifecycle.d.ts +46 -0
  200. package/dist/component/sync/lifecycle.d.ts.map +1 -0
  201. package/dist/component/sync/lifecycle.js +87 -0
  202. package/dist/component/sync/lifecycle.js.map +1 -0
  203. package/dist/component/sync/mutations.d.ts +101 -0
  204. package/dist/component/sync/mutations.d.ts.map +1 -0
  205. package/dist/component/sync/mutations.js +132 -0
  206. package/dist/component/sync/mutations.js.map +1 -0
  207. package/dist/component/sync/queries.d.ts +69 -0
  208. package/dist/component/sync/queries.d.ts.map +1 -0
  209. package/dist/component/sync/queries.js +32 -0
  210. package/dist/component/sync/queries.js.map +1 -0
  211. package/dist/component/validators.d.ts +1112 -0
  212. package/dist/component/validators.d.ts.map +1 -0
  213. package/dist/component/validators.js +317 -0
  214. package/dist/component/validators.js.map +1 -0
  215. package/dist/react/index.d.ts +42 -0
  216. package/dist/react/index.d.ts.map +1 -0
  217. package/dist/react/index.js +43 -0
  218. package/dist/react/index.js.map +1 -0
  219. package/dist/shared.d.ts +28 -0
  220. package/dist/shared.d.ts.map +1 -0
  221. package/dist/shared.js +24 -0
  222. package/dist/shared.js.map +1 -0
  223. package/package.json +126 -0
  224. package/src/client/index.ts +558 -0
  225. package/src/client/types.ts +117 -0
  226. package/src/component/_generated/api.ts +141 -0
  227. package/src/component/_generated/component.ts +1814 -0
  228. package/src/component/_generated/dataModel.ts +60 -0
  229. package/src/component/_generated/server.ts +169 -0
  230. package/src/component/actions.ts +156 -0
  231. package/src/component/catalog/browse_order.test.ts +55 -0
  232. package/src/component/catalog/browse_order.ts +43 -0
  233. package/src/component/catalog/field_source_policy.test.ts +47 -0
  234. package/src/component/catalog/field_source_policy.ts +44 -0
  235. package/src/component/catalog/merge.test.ts +187 -0
  236. package/src/component/catalog/merge.ts +134 -0
  237. package/src/component/catalog/mutations.ts +328 -0
  238. package/src/component/catalog/promote.ts +40 -0
  239. package/src/component/catalog/queries.ts +220 -0
  240. package/src/component/config/mutations.ts +29 -0
  241. package/src/component/config/queries.ts +20 -0
  242. package/src/component/convex.config.ts +32 -0
  243. package/src/component/crons.test.ts +8 -0
  244. package/src/component/crons.ts +52 -0
  245. package/src/component/imports/actions.ts +553 -0
  246. package/src/component/imports/dedupe.test.ts +66 -0
  247. package/src/component/imports/dedupe.ts +54 -0
  248. package/src/component/imports/mutations.ts +203 -0
  249. package/src/component/imports/queries.ts +25 -0
  250. package/src/component/imports/state.test.ts +60 -0
  251. package/src/component/imports/state.ts +74 -0
  252. package/src/component/mutations.ts +96 -0
  253. package/src/component/providers/actions.ts +127 -0
  254. package/src/component/providers/apple/impl.test.ts +267 -0
  255. package/src/component/providers/apple/impl.ts +224 -0
  256. package/src/component/providers/apple/jwt.test.ts +153 -0
  257. package/src/component/providers/apple/jwt.ts +99 -0
  258. package/src/component/providers/apple/mappers.test.ts +190 -0
  259. package/src/component/providers/apple/mappers.ts +100 -0
  260. package/src/component/providers/apple/types.ts +77 -0
  261. package/src/component/providers/deezer/impl.test.ts +109 -0
  262. package/src/component/providers/deezer/impl.ts +179 -0
  263. package/src/component/providers/deezer/mappers.test.ts +78 -0
  264. package/src/component/providers/deezer/mappers.ts +72 -0
  265. package/src/component/providers/deezer/types.ts +59 -0
  266. package/src/component/providers/fetch.test.ts +282 -0
  267. package/src/component/providers/fetch.ts +190 -0
  268. package/src/component/providers/musicbrainz/impl.test.ts +79 -0
  269. package/src/component/providers/musicbrainz/impl.ts +132 -0
  270. package/src/component/providers/musicbrainz/mappers.test.ts +75 -0
  271. package/src/component/providers/musicbrainz/mappers.ts +34 -0
  272. package/src/component/providers/musicbrainz/types.ts +20 -0
  273. package/src/component/providers/registry.test.ts +14 -0
  274. package/src/component/providers/registry.ts +48 -0
  275. package/src/component/providers/spotify/client.test.ts +80 -0
  276. package/src/component/providers/spotify/client.ts +57 -0
  277. package/src/component/providers/spotify/impl.test.ts +311 -0
  278. package/src/component/providers/spotify/impl.ts +230 -0
  279. package/src/component/providers/spotify/mappers.test.ts +182 -0
  280. package/src/component/providers/spotify/mappers.ts +81 -0
  281. package/src/component/providers/spotify/types.ts +101 -0
  282. package/src/component/providers/types.ts +95 -0
  283. package/src/component/providers/wikidata/impl.test.ts +81 -0
  284. package/src/component/providers/wikidata/impl.ts +139 -0
  285. package/src/component/providers/wikidata/mappers.test.ts +62 -0
  286. package/src/component/providers/wikidata/mappers.ts +66 -0
  287. package/src/component/providers/wikidata/types.ts +28 -0
  288. package/src/component/queries.ts +51 -0
  289. package/src/component/schema.ts +81 -0
  290. package/src/component/sources/actions.ts +177 -0
  291. package/src/component/sources/mutations.ts +67 -0
  292. package/src/component/sources/queries.ts +21 -0
  293. package/src/component/sync/lifecycle.test.ts +72 -0
  294. package/src/component/sync/lifecycle.ts +121 -0
  295. package/src/component/sync/mutations.ts +154 -0
  296. package/src/component/sync/queries.ts +33 -0
  297. package/src/component/validators.ts +411 -0
  298. package/src/react/index.test.ts +78 -0
  299. package/src/react/index.ts +112 -0
  300. package/src/shared.ts +32 -0
  301. 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
+ });