tickets
All repositories: gitoria
2.9 KB
// Content-free asynchronous wakes. Only the main process dispatches (standby is explicitly disabled).import { env } from 'hl:proc'import { now, timestamp } from 'hl:time'import { fetchStart } from 'hl:fetch'import { hkdfExtract, randomBytes } from 'hl:crypto'import { subscriptionsTable, subscriptionRecords, matchesSubscription, hasDelta } from './subscriptions.hl'static enabled = () => { return env('TICKETS_WAKER_URL') != null && env('TICKETS_WAKER_URL') != '' && env('TICKETS_WAKER_SECRET') != null && env('TICKETS_WAKER_SECRET') != '' && env('TICKETS_WAKES_DISABLED') != '1' }static signature = (secret, body) => { return 'sha256=' + hkdfExtract(toBytes(secret), toBytes(body)).hex() }static retryMinutes = [1, 5, 30, 120]static sendWake = (s) => {if (!enabled()) { return null }if (s.delivery == null || s.delivery == '') {s.delivery = randomBytes(16).slice(0, 8) + '-' + randomBytes(2) + '-4' + randomBytes(2).slice(1) + '-8' + randomBytes(2).slice(1) + '-' + randomBytes(6)s.wakeBody = JSON.stringify({ subscription = s.id at = timestamp() delivery = s.delivery })s.wakeAttempts = 0}s.wakeAttempts = s.wakeAttempts + 1s.wakeStatus = 'sending's.nextWake = now() + 11000subscriptionsTable.update(s.id, s)fetchStart(env('TICKETS_WAKER_URL'), { method = 'POST' body = s.wakeBody headers = { 'Content-Type' = 'application/json' 'X-WorldAPI-Signature-256' = signature(env('TICKETS_WAKER_SECRET'), s.wakeBody) } timeoutMs = 10000 maxRedirects = 0 tag = 'tickets-wake:' + s.id + ':' + s.delivery })return null}static wakeForRow = (r) => {if (!enabled()) { return null }for (s of subscriptionRecords()) {if (!s.wakePending && r.n > s.cursor && matchesSubscription(s, r)) {s.wakePending = trues.delivery = ''sendWake(s)}}return null}static wakeCompleted = (ev) => {if (ev.tag == null || !ev.tag.startsWith('tickets-wake:')) { return null }parts = ev.tag.split(':')if (parts.length != 3) { return null }s = subscriptionsTable.fetch(parts[1])if (s == null || !s.wakePending || s.delivery != parts[2]) { return null }if (ev.status >= 200 && ev.status < 300) { s.wakeStatus = 'sent' } else {if (s.wakeAttempts > retryMinutes.length) { s.wakeStatus = 'failed' } else {s.wakeStatus = 'retry's.nextWake = now() + retryMinutes[s.wakeAttempts - 1] * 60000}}subscriptionsTable.update(s.id, s)return null}// Poll only local numbered rows: also catches writes made while standby was serving requests.// Startup retries use the SAME delivery/body; the waker durably de-duplicates successful deliveries.static sweepWakes = (startup) => {if (!enabled()) { return null }for (s of subscriptionRecords()) {if (hasDelta(s)) {if (startup || !s.wakePending) {s.wakePending = trueif (startup) { s.wakeAttempts = 0 }sendWake(s)} else if ((s.wakeStatus == 'retry' || s.wakeStatus == 'sending') && s.nextWake <= now()) { sendWake(s) }}}return null}
Branches
Latest commits
- 8d8e2053Document subscription contract and exercise delta deliverymre
- fd23726aAdd subscription delta API and signed wake bridgemre
- c5d28fb5Tickets conductor: S51 startedmre
- fd6ca1e6Tickets conductor: brief 027 waker design, push-test projectmre
- e448e656Tickets conductor: STATUS #51mre
- 1c10857bTickets conductor: P51 eval, brief 027mre
- e725969dTickets conductor: #51 directionmre
- 1b5c63ddTickets conductor: P51 startedmre
- 9f13fbdaTickets conductor: brief 026 prioritymre
- acc477d9Tickets conductor: brief 026 + Vinzenz recommendationsmre
- 9233a734Tickets conductor: brief 026 completemre
- 5da589fdTickets conductor: brief 026 + Gitoria's answermre
- a3cd5329Tickets conductor: tickets#51 opened, brief 026 draftmre
- 6f6e5797Tickets conductor: STATUS wordingmre
- 957de51fTickets conductor: T36 live, #36 review, #50 donemre
- bc2fde32Merge branch 'main' into t36-stylemre
- b7ca093atests: wait for the new row and normalize editor marker spacesmre
- 41da3525Tickets conductor: flake cause, T36 deploy stepmre
- 64c477e6Tickets conductor: deploy waits for Arachnemre
- 467d7286Tickets conductor: T36 done, deploy questionmre