@@ -11,6 +11,8 @@ use sqlx::{PgPool, Row as _};
1111use uuid:: Uuid ;
1212
1313use crate :: error:: Result ;
14+ use crate :: Db ;
15+ use buzz_datastore_tracing:: datastore_span;
1416
1517/// Namespace for the per-community push-gate advisory lock. Must match the
1618/// key built inside the `enqueue_push_match_job` trigger (migration 0023):
@@ -1278,6 +1280,177 @@ fn row_to_claimed_wake(row: sqlx::postgres::PgRow) -> Result<ClaimedWake> {
12781280 } )
12791281}
12801282
1283+ impl Db {
1284+ /// Exclusively claim a batch of due matcher jobs from one community.
1285+ #[ datastore_span( name = "claim_due_push_match_batch" , system = "postgresql" ) ]
1286+ pub async fn claim_due_push_match_batch (
1287+ & self ,
1288+ limit : i64 ,
1289+ lease_until : DateTime < Utc > ,
1290+ ) -> Result < Option < crate :: push:: ClaimedMatchBatch > > {
1291+ crate :: push:: claim_due_match_batch ( & self . pool , limit, lease_until) . await
1292+ }
1293+
1294+ /// Load active endpoint-enabled leases eligible for push matching.
1295+ #[ datastore_span( name = "active_push_match_leases" , system = "postgresql" ) ]
1296+ pub async fn active_push_match_leases (
1297+ & self ,
1298+ community : CommunityId ,
1299+ ) -> Result < Vec < crate :: push:: MatchLease > > {
1300+ crate :: push:: active_match_leases ( & self . pool , community) . await
1301+ }
1302+
1303+ /// Complete matcher jobs from one claimed batch while the fence holds.
1304+ #[ datastore_span( name = "complete_push_match_batch" , system = "postgresql" ) ]
1305+ pub async fn complete_push_match_batch (
1306+ & self ,
1307+ community : CommunityId ,
1308+ claim_id : uuid:: Uuid ,
1309+ event_ids : & [ Vec < u8 > ] ,
1310+ ) -> Result < u64 > {
1311+ crate :: push:: complete_match_batch ( & self . pool , community, claim_id, event_ids) . await
1312+ }
1313+
1314+ /// Release fenced matcher claims from one batch for retry.
1315+ #[ datastore_span( name = "retry_push_match_batch" , system = "postgresql" ) ]
1316+ pub async fn retry_push_match_batch (
1317+ & self ,
1318+ community : CommunityId ,
1319+ claim_id : uuid:: Uuid ,
1320+ event_ids : & [ Vec < u8 > ] ,
1321+ next : DateTime < Utc > ,
1322+ ) -> Result < u64 > {
1323+ crate :: push:: retry_match_batch ( & self . pool , community, claim_id, event_ids, next) . await
1324+ }
1325+
1326+ /// Delete exhausted matcher jobs (periodic sweep, off the claim path).
1327+ #[ datastore_span( name = "reap_exhausted_push_matches" , system = "postgresql" ) ]
1328+ pub async fn reap_exhausted_push_matches ( & self ) -> Result < u64 > {
1329+ crate :: push:: reap_exhausted_matches ( & self . pool ) . await
1330+ }
1331+
1332+ /// Idempotently enqueue a wake for a matched lease and event.
1333+ #[ datastore_span( name = "enqueue_push_wake" , system = "postgresql" ) ]
1334+ pub async fn enqueue_push_wake (
1335+ & self ,
1336+ community : CommunityId ,
1337+ author : & [ u8 ] ,
1338+ installation_id : & str ,
1339+ wake : crate :: push:: NewWake < ' _ > ,
1340+ ) -> Result < crate :: push:: EnqueueWakeOutcome > {
1341+ crate :: push:: enqueue_wake ( & self . pool , community, author, installation_id, wake) . await
1342+ }
1343+
1344+ /// Set-wise [`Self::enqueue_push_wake`]: one transaction per batch.
1345+ #[ datastore_span( name = "enqueue_push_wakes" , system = "postgresql" ) ]
1346+ pub async fn enqueue_push_wakes (
1347+ & self ,
1348+ community : CommunityId ,
1349+ requests : & [ crate :: push:: WakeRequest ] ,
1350+ ) -> Result < Vec < crate :: push:: EnqueueWakeOutcome > > {
1351+ crate :: push:: enqueue_wakes ( & self . pool , community, requests) . await
1352+ }
1353+
1354+ /// Exclusively claim due wake jobs for one community.
1355+ #[ datastore_span( name = "claim_due_push_wakes" , system = "postgresql" ) ]
1356+ pub async fn claim_due_push_wakes (
1357+ & self ,
1358+ community : CommunityId ,
1359+ limit : i64 ,
1360+ lease_until : DateTime < Utc > ,
1361+ ) -> Result < Vec < crate :: push:: ClaimedWake > > {
1362+ crate :: push:: claim_due_wakes ( & self . pool , community, limit, lease_until) . await
1363+ }
1364+
1365+ /// Revalidate a wake's claim, source event, and current lease before send.
1366+ #[ datastore_span( name = "revalidate_push_wake" , system = "postgresql" ) ]
1367+ pub async fn revalidate_push_wake (
1368+ & self ,
1369+ community : CommunityId ,
1370+ id : Uuid ,
1371+ claim_id : Uuid ,
1372+ ) -> Result < crate :: push:: RevalidateWakeOutcome > {
1373+ crate :: push:: revalidate_wake_for_send ( & self . pool , community, id, claim_id) . await
1374+ }
1375+
1376+ /// Mark a fenced wake claim delivered.
1377+ #[ datastore_span( name = "complete_push_wake" , system = "postgresql" ) ]
1378+ pub async fn complete_push_wake (
1379+ & self ,
1380+ community : CommunityId ,
1381+ id : Uuid ,
1382+ claim_id : Uuid ,
1383+ ) -> Result < bool > {
1384+ crate :: push:: complete_wake ( & self . pool , community, id, claim_id) . await
1385+ }
1386+
1387+ /// Release a fenced wake claim for retry at the supplied time.
1388+ #[ datastore_span( name = "retry_push_wake" , system = "postgresql" ) ]
1389+ pub async fn retry_push_wake (
1390+ & self ,
1391+ community : CommunityId ,
1392+ id : Uuid ,
1393+ claim_id : Uuid ,
1394+ next : DateTime < Utc > ,
1395+ ) -> Result < bool > {
1396+ crate :: push:: retry_wake ( & self . pool , community, id, claim_id, next) . await
1397+ }
1398+
1399+ /// Mark a fenced wake claim terminally failed.
1400+ #[ datastore_span( name = "fail_push_wake" , system = "postgresql" ) ]
1401+ pub async fn fail_push_wake (
1402+ & self ,
1403+ community : CommunityId ,
1404+ id : Uuid ,
1405+ claim_id : Uuid ,
1406+ ) -> Result < bool > {
1407+ crate :: push:: fail_wake ( & self . pool , community, id, claim_id) . await
1408+ }
1409+
1410+ /// Disable an endpoint only if the specified lease generation is current.
1411+ #[ datastore_span( name = "disable_push_endpoint" , system = "postgresql" ) ]
1412+ pub async fn disable_push_endpoint (
1413+ & self ,
1414+ community : CommunityId ,
1415+ author : & [ u8 ] ,
1416+ installation_id : & str ,
1417+ generation : i64 ,
1418+ ) -> Result < bool > {
1419+ crate :: push:: disable_endpoint_generation (
1420+ & self . pool ,
1421+ community,
1422+ author,
1423+ installation_id,
1424+ generation,
1425+ )
1426+ . await
1427+ }
1428+
1429+ /// Atomically persist a validated kind:30350 event and its effective lease.
1430+ #[ allow( clippy:: too_many_arguments) ]
1431+ #[ datastore_span( name = "accept_push_lease_event" , system = "postgresql" ) ]
1432+ pub async fn accept_push_lease_event (
1433+ & self ,
1434+ community : CommunityId ,
1435+ event : & nostr:: Event ,
1436+ installation_id : & str ,
1437+ version : crate :: push:: LeaseVersion < ' _ > ,
1438+ active : Option < crate :: push:: ActiveLease < ' _ > > ,
1439+ max_active_leases : i64 ,
1440+ ) -> Result < crate :: push:: AcceptLeaseOutcome > {
1441+ crate :: push:: accept_lease_event (
1442+ & self . pool ,
1443+ community,
1444+ event,
1445+ installation_id,
1446+ version,
1447+ active,
1448+ max_active_leases,
1449+ )
1450+ . await
1451+ }
1452+ }
1453+
12811454#[ cfg( test) ]
12821455mod tests {
12831456 use super :: * ;
0 commit comments