gitoria

tickets

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Branchl33-thinecd7e226|||PR Consolidate login and move handler logic into topic libraries (partial)mrel33-thin/lib/subscription-wakes.hl

3.4 KB

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

Branches

Latest commits

  • ecd7e226|||PR Consolidate login and move handler logic into topic libraries (partial)mre
  • 6dea1c60Merge branch 'r35-day72' into mainTickets
  • 7bcfdc22|||PR drop worker edits to conductor recordsmre
  • 2617b6ee|||PR dialogs: host flag cleared on close, dialog gate (tickets#40)mre
  • 6e701371|||PR record partial day72 dialog validationmre
  • b9fb9a91|||PR bind ticket dialog visibility to member flagsmre
  • 0788e65f|||PR re-vendor Hybriel day72mre
  • e2a6bdcfMerge branch 'records-t35' into mainTickets
  • d8d54df1|||PR records: R35 day72 + #40mre
  • 8a7f475aMerge branch 'records-t33' into mainTickets
  • e685d0c2|||PR records: PROCEDURES merge callmre
  • b42aff2b|||PR records: GO 2026-11-23, workers L33 O34, PROCEDURES.mdmre
  • ed091d44Merge branch 'records' into mainTickets
  • 180c0cdb|||PR records: ticket move live (M32), hybriel#439mre
  • 497cae7bMerge branch 'm32-move' into mainTickets
  • 772e0696|||PR Allow project admins to move tickets between their projectsmre
  • be92a560Merge branch 'records' into mainTickets
  • 0daf2ea8|||PR records: PR-only workflow, #50 donemre
  • 0185d0b6Log PR 1 deploymre
  • f5bf16e2Mission 031: deploy PR 1mre