gitoria

tickets

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit54bc5fe154bc5fe1Harden delta relevance, overflow caps and retry verificationmre54bc5fe1/lib/subscription-wakes.hl

2.9 KB

  1. // Content-free asynchronous wakes. Only the main process dispatches (standby is explicitly disabled).
  2. import { env } from 'hl:proc'
  3. import { now, timestamp } from 'hl:time'
  4. import { fetchStart } from 'hl:fetch'
  5. import { hkdfExtract, randomBytes } from 'hl:crypto'
  6. import { subscriptionsTable, subscriptionRecords, matchesSubscription, hasDelta } from './subscriptions.hl'
  7. 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' }
  8. static signature = (secret, body) => { return 'sha256=' + hkdfExtract(toBytes(secret), toBytes(body)).hex() }
  9. static retryMinutes = [1, 5, 30, 120]
  10. static sendWake = (s) => {
  11. if (!enabled()) { return null }
  12. if (s.delivery == null || s.delivery == '') {
  13. s.delivery = randomBytes(16).slice(0, 8) + '-' + randomBytes(2) + '-4' + randomBytes(2).slice(1) + '-8' + randomBytes(2).slice(1) + '-' + randomBytes(6)
  14. s.wakeBody = JSON.stringify({ subscription = s.id at = timestamp() delivery = s.delivery })
  15. s.wakeAttempts = 0
  16. }
  17. s.wakeAttempts = s.wakeAttempts + 1
  18. s.wakeStatus = 'sending'
  19. s.nextWake = now() + 11000
  20. subscriptionsTable.update(s.id, s)
  21. 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 })
  22. return null
  23. }
  24. static wakeForRow = (r) => {
  25. if (!enabled()) { return null }
  26. for (s of subscriptionRecords()) {
  27. if (!s.wakePending && r.n > s.cursor && matchesSubscription(s, r)) {
  28. s.wakePending = true
  29. s.delivery = ''
  30. sendWake(s)
  31. }
  32. }
  33. return null
  34. }
  35. static wakeCompleted = (ev) => {
  36. if (ev.tag == null || !ev.tag.startsWith('tickets-wake:')) { return null }
  37. parts = ev.tag.split(':')
  38. if (parts.length != 3) { return null }
  39. s = subscriptionsTable.fetch(parts[1])
  40. if (s == null || !s.wakePending || s.delivery != parts[2]) { return null }
  41. if (ev.status >= 200 && ev.status < 300) { s.wakeStatus = 'sent' } else {
  42. if (s.wakeAttempts > retryMinutes.length) { s.wakeStatus = 'failed' } else {
  43. s.wakeStatus = 'retry'
  44. s.nextWake = now() + retryMinutes[s.wakeAttempts - 1] * 60000
  45. }
  46. }
  47. subscriptionsTable.update(s.id, s)
  48. return null
  49. }
  50. // Poll only local numbered rows: also catches writes made while standby was serving requests.
  51. // Startup retries use the SAME delivery/body; the waker durably de-duplicates successful deliveries.
  52. static sweepWakes = (startup) => {
  53. if (!enabled()) { return null }
  54. for (s of subscriptionRecords()) {
  55. if (hasDelta(s)) {
  56. if (startup || !s.wakePending) {
  57. s.wakePending = true
  58. if (startup) { s.wakeAttempts = 0 }
  59. sendWake(s)
  60. } else if ((s.wakeStatus == 'retry' || s.wakeStatus == 'sending') && s.nextWake <= now()) { sendWake(s) }
  61. }
  62. }
  63. return null
  64. }

Branches

Latest commits

  • 54bc5fe1Harden delta relevance, overflow caps and retry verificationmre
  • 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