tickets
All repositories: gitoria
3.4 KB
// Content-free asynchronous wakes. Only the main process dispatches (standby is explicitly disabled).import { env } from 'hl:proc'import { now } from 'hl:time'import { fetchStart } from 'hl:fetch'import { hkdfExtract, randomBytes } from 'hl:crypto'import { publicUrl } from './users.hl'import { pushUrlError, pushSecret } from './subscription-push.hl'import { subscriptionsTable, subscriptionRecords, matchesSubscription, hasDelta } from './subscriptions.hl'static enabled = () => { return env('TICKETS_WAKES_DISABLED') != '1' }static pushEnabled = (s) => { return s.pushUrl != null && s.pushUrl != '' && pushUrlError(s.pushUrl) == null }static signature = (secret, body) => { return 'sha256=' + hkdfExtract(toBytes(secret), toBytes(body)).hex() }static retryMinutes = [1, 5, 30, 120]static sendWake = (s) => {if (!enabled() || !pushEnabled(s)) { return null }secret = pushSecret(s)if (secret == null) {s.wakeStatus = 'failed'subscriptionsTable.update(s.id, s)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)text = "tickets: new changes for you. Read them: curl -s -A '<you>' -H 'Authorization: Bearer <your tickets token>' -H 'Accept: text/markdown' " + publicUrl + '/api/subscriptions/' + s.id + '/delta'if (text.length > 500) { s.wakeStatus = 'failed' subscriptionsTable.update(s.id, s) return null }s.wakeBody = JSON.stringify({ source = 'tickets' delivery = s.delivery text = text })s.wakeAttempts = 0}s.wakeAttempts = s.wakeAttempts + 1s.wakeStatus = 'sending's.nextWake = now() + 11000subscriptionsTable.update(s.id, s)fetchStart(s.pushUrl, { method = 'POST' body = s.wakeBody headers = { 'Content-Type' = 'application/json' 'X-Push-Delivery' = s.delivery 'X-Push-Signature-256' = signature(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 (pushEnabled(s) && !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 != null && ev.status >= 200 && ev.status < 300) || ev.status == 409) { 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; HC returns 409 for deliveries it already accepted.static sweepWakes = (startup) => {if (!enabled()) { return null }for (s of subscriptionRecords()) {if (pushEnabled(s) && 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
- e843ff7cOne-time purge of miserit-anthar#94 (#94, identity rule): idempotent step in lib/migrate.hl, fixture gate tests/purge94.mjsmre
- 506a4efdmission 028: #94, identity rulemre
- 78c7f48eRefine mpackdb holdmre
- 3d7d89c8Record mpackdb hold until day38mre
- f1bf7195#51 done; my subscription idmre
- 9870b945status: #51 proof greenmre
- e92173c6log: #51 live push proofmre
- 72d87041log: Hermes push livemre
- 1d69bf7dTickets conductor: park until Hermes pushmre
- 80d8cfd1Tickets conductor: #51 livemre
- 61641dd9Complete subscriptions deployment evidencemre
- c980856cRecord successful subscriptions deployment and live checksmre
- 4b4710adMerge branch 's51-subscriptions'mre
- a731ef7eTickets conductor: S51 reviewed, deploy stepmre
- b487b05aRecord verified direct push protocol handoffmre
- 74e34cc3Verify encrypted push secrets and direct push retry semanticsmre
- a1ac5cd0Push subscription notices directly to signed HyperConductor chat endpointsmre
- d662ad27Remove superseded waker service for direct chat push protocolmre
- c0ac927aTickets conductor: S51 redo for push protocolmre
- ff2e8701Finalize subscription build verification and handoff reportmre