Skip to content

Self-hosted personal gallery application with online photo management and albums, supporting Live/Motion Photos, EXIF parsing, geolocation recognition, and an explore map.

fix(database): fence media mutations with live site eligibility

Timothy Yin47dd577a479f
diff --git a/packages/database/src/cloud-execution.ts b/packages/database/src/cloud-execution.ts
index f4c8086d..3f2f8fc4 100644
--- a/packages/database/src/cloud-execution.ts
+++ b/packages/database/src/cloud-execution.ts
@@ -22,7 +22,8 @@ function leaseDuration(value: number) {
throw new CoreError(400, 'Invalid lease duration')
return value
}
-/** Source existence/version and deletion are checked on every worker mutation. */
+/** Source identity and live site eligibility are checked on every worker mutation. */
+const activeSite = `EXISTS(SELECT 1 FROM platform_sites s WHERE s.id=cloud_processing_jobs.site_id AND s.suspended=0)`
const currentSource = `EXISTS(SELECT 1 FROM cloud_photos p WHERE p.site_id=cloud_processing_jobs.site_id AND p.id=cloud_processing_jobs.photo_id AND p.source_version=cloud_processing_jobs.source_version AND p.deleted=0)`
/** Database polling is intentional: job creation and enqueue are the same D1
@@ -42,7 +43,7 @@ export function createCloudExecutionRepository(
identifier(lease.attemptId)
identifier(lease.workerId)
}
- const valid = `site_id=? AND id=? AND attempt_id=? AND worker_id=? AND state='running' AND lease_until>? AND ${currentSource}`
+ const valid = `site_id=? AND id=? AND attempt_id=? AND worker_id=? AND state='running' AND lease_until>? AND ${currentSource} AND ${activeSite}`
const validValues = (lease: CloudLease, now: number) =>
[lease.id, lease.attemptId, lease.workerId, positive(now)] as const
async function changed(s: MediaStatement) {
@@ -58,13 +59,13 @@ export function createCloudExecutionRepository(
throw new CoreError(400, 'Invalid lease expiry')
await db.batch([
stmt(
- `UPDATE cloud_processing_jobs SET state='failed',error_code='attempts_exhausted',lease_until=NULL WHERE site_id=? AND state='running' AND lease_until<=? AND attempts>=max_attempts`,
+ `UPDATE cloud_processing_jobs SET state='failed',error_code='attempts_exhausted',lease_until=NULL WHERE site_id=? AND state='running' AND lease_until<=? AND attempts>=max_attempts AND ${activeSite}`,
siteId,
now,
),
stmt(
`UPDATE cloud_processing_jobs SET state='running',worker_id=?,attempt_id=?,lease_until=?,attempts=attempts+1,error_code=NULL
- WHERE site_id=? AND id=(SELECT id FROM cloud_processing_jobs WHERE site_id=? AND attempts<max_attempts AND ${currentSource}
+ WHERE site_id=? AND ${activeSite} AND id=(SELECT id FROM cloud_processing_jobs WHERE site_id=? AND attempts<max_attempts AND ${currentSource}
AND ((state IN ('queued','retry_wait') AND available_at<=?) OR (state='running' AND lease_until<=?)) AND NOT EXISTS(SELECT 1 FROM cloud_processing_jobs active WHERE active.site_id=? AND active.state='running' AND active.lease_until>?) ORDER BY created_at,id LIMIT 1)`,
workerId,
attemptId,
diff --git a/tests/cloud-execution.test.ts b/tests/cloud-execution.test.ts
index f26dcb77..f13e50ba 100644
--- a/tests/cloud-execution.test.ts
+++ b/tests/cloud-execution.test.ts
@@ -24,9 +24,9 @@ test('D1 queue serializes claims, fences expired attempts, retries and refuses d
try {
const db = await mf.getD1Database('DB')
for (const sql of [
- 'CREATE TABLE platform_sites(id INTEGER PRIMARY KEY)',
+ 'CREATE TABLE platform_sites(id INTEGER PRIMARY KEY,suspended INTEGER NOT NULL DEFAULT 0)',
'CREATE TABLE platform_users(id INTEGER PRIMARY KEY)',
- 'INSERT INTO platform_sites VALUES(1),(2)',
+ 'INSERT INTO platform_sites(id) VALUES(1),(2)',
'INSERT INTO platform_users VALUES(1)',
])
await db.prepare(sql).run()
@@ -172,6 +172,50 @@ test('D1 queue serializes claims, fences expired attempts, retries and refuses d
),
{ statusCode: 403 },
)
+ await enqueue('paused-race')
+ await db.prepare('UPDATE platform_sites SET suspended=1 WHERE id=1').run()
+ assert.equal(
+ await queue.claim('worker', 'paused-attempt', 9000, 1000),
+ null,
+ )
+ assert.equal(
+ (
+ await db
+ .prepare(
+ "SELECT attempts FROM cloud_processing_jobs WHERE id='paused-race'",
+ )
+ .first()
+ ).attempts,
+ 0,
+ )
+ await db.prepare('UPDATE platform_sites SET suspended=0 WHERE id=1').run()
+ const paused = (await queue.claim(
+ 'worker',
+ 'unpaused-attempt',
+ 9100,
+ 1000,
+ ))!
+ assert.equal(paused.attempts, 1)
+ // Simulate a request that captured its clock before the deletion mark's lease expiry.
+ await db.prepare('UPDATE platform_sites SET suspended=1 WHERE id=1').run()
+ await db
+ .prepare(
+ "UPDATE cloud_processing_jobs SET lease_until=9500 WHERE id='paused-race'",
+ )
+ .run()
+ assert.equal(await queue.heartbeat(paused, 9200, 1000), false)
+ assert.equal(await queue.complete(paused, result(paused), 9200), false)
+ assert.equal(await queue.fail(paused, 'network_error', true, 9200), false)
+ assert.equal(
+ (
+ await db
+ .prepare(
+ "SELECT attempts FROM cloud_processing_jobs WHERE id='paused-race'",
+ )
+ .first()
+ ).attempts,
+ 1,
+ )
} finally {
await mf.dispose()
}