gitoria

kunibert

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitc9b48bfec9b48bfeVendor Hybriel plugins; deploy ships bin/ from zig-outKunibertc9b48bfe/lib/run-job.hl

5.6 KB

  1. import { appendFile, exists, writeFile, mkDir, tryReadFile } from 'hl:fs'
  2. import { timestamp } from 'hl:time'
  3. import { run, spawnArgs, dir } from 'hl:proc'
  4. import { after } from 'hl:time'
  5. \\ THE ONE FUNCTION the queue calls: runJob(job, done). It starts the review CLI (tools/runner.hl review-pr ...) as a CHILD with
  6. \\ spawnArgs and returns at once; `done(result)` is called later, from the child's exit event (or the timeout), with
  7. \\ { ok, error, out }. Nothing here blocks the web routes while the model runs.
  8. \\ Legacy test mode (no repos configured, K5 phases): the job is only recorded in <dataDir>/runjobs.jsonl; hooks
  9. \\ <dataDir>/fail-<key, # as _> (fails) and <dataDir>/slow-<key> (3 s, blocking) as before.
  10. String dataDir = ''
  11. Hybrid repos = null \\ repo.id -> { api, clone, keyFile }; null = legacy test mode
  12. String hybriel = 'hybriel' \\ the runtime binary
  13. Number timeoutSeconds = 900 \\ hard limit per job: past it the child is killed, the job failed
  14. String runnerData = '' \\ the CLI's own state dir (sessions, git cache)
  15. String ticketsBase = '' \\ tickets service base URL; '' = linked tickets are not read
  16. String ticketsTokenFile = '' \\ read token file of Kunibert for it (mode 600); only the PATH goes into the argv
  17. \\ the argv of the review child; the key file path is the only secret-related thing in it
  18. argvFor = (Hybrid job, Hybrid cfg) => {
  19. tool = dir() + '/../tools/runner.hl'
  20. let a = [hybriel, tool, 'review-pr', runnerData, '--api', cfg.api, '--key-file', cfg.keyFile, '--clone', cfg.clone]
  21. if (ticketsBase != '' && ticketsTokenFile != '') { a.push('--tickets-base') a.push(ticketsBase) a.push('--tickets-token-file') a.push(ticketsTokenFile) }
  22. if (job.force || job.reset || job.notes.length > 0) { a.push('--force') }
  23. if (job.reset) { a.push('--reset') }
  24. if (job.notes.length > 0) {
  25. mkDir(dataDir + '/notes')
  26. nf = dataDir + '/notes/' + job.key.replace('#', '_') + '.json'
  27. writeFile(nf, JSON.stringify(job.notes))
  28. a.push('--note-file')
  29. a.push(nf)
  30. }
  31. a.push('' + job.number)
  32. return a
  33. }
  34. cut = (String t) => {
  35. if (t.length <= 300) { return t }
  36. return t.slice(0, 300) + '…'
  37. }
  38. \\ run a child to its end or its timeout; cb({ exit, killed, out, err })
  39. runChild = (Hybrid argv, Number seconds, cb) => {
  40. let outLines = []
  41. let errLines = []
  42. let killed = false
  43. child = spawnArgs(argv)
  44. timer = after(seconds)
  45. on child.line(e) { if (e.stream == 'stdout') { outLines.push(e.line) } else { errLines.push(e.line) } }
  46. on child.exit(e) {
  47. timer.stop()
  48. cb({ exit = e.exit, =killed, out = outLines, err = errLines })
  49. }
  50. on timer.tick() {
  51. killed = true
  52. child.kill()
  53. }
  54. return child.pid
  55. }
  56. \\ the JSON the CLI printed last (stdout), or null
  57. lastJson = (Hybrid lines) => {
  58. for (let i = lines.length - 1; i >= 0; i = i - 1) {
  59. if (lines[i].trim() != '') {
  60. if (!lines[i].trim().startsWith('{')) { return null }
  61. return JSON.parse(lines[i])
  62. }
  63. }
  64. return null
  65. }
  66. runJob = (Hybrid job, done) => {
  67. if (repos == null) { return legacy(job, done) }
  68. cfg = repos['' + job.repo.id]
  69. if (cfg == null) { return done({ ok = false, error = 'no config for repo ' + job.repo.id }) }
  70. argv = argvFor(job, cfg)
  71. console.log(timestamp() + ' runjob: ' + job.key + ' start timeout=' + timeoutSeconds + 's force=' + job.force + ' reset=' + job.reset + ' notes=' + job.notes.length)
  72. runChild(argv, timeoutSeconds, (r) => {
  73. if (r.killed) {
  74. \\ the killed CLI cannot tell gitoria: do it now (short child), then report the failure
  75. msg = 'Review timed out after ' + timeoutSeconds + 's'
  76. runChild([hybriel, dir() + '/../tools/runner.hl', 'status-error', '--api', cfg.api, '--key-file', cfg.keyFile, '--head', job.head, '--message', msg], 60, (s) => {
  77. console.log(timestamp() + ' runjob: ' + job.key + ' KILLED at timeout, error status exit=' + s.exit)
  78. done({ ok = false, error = msg })
  79. })
  80. return null
  81. }
  82. j = lastJson(r.out)
  83. if (r.exit == 0 && (j == null || j.error == null)) {
  84. done({ ok = true, out = j })
  85. return null
  86. }
  87. let err = 'review child exit ' + r.exit
  88. if (j != null && j.error != null) { err = '' + j.error }
  89. else if (r.err.length > 0) { err = err + ': ' + r.err[r.err.length - 1] }
  90. done({ ok = false, error = cut(err) })
  91. return null
  92. })
  93. return null
  94. }
  95. \\ closed/merged PR: remove its session (state) via the CLI; done({ ok })
  96. dropSession = (Hybrid mark, String number, done) => {
  97. let cfg = null
  98. if (repos != null) { cfg = repos['' + mark.repo.id] }
  99. if (cfg == null) { return done({ ok = false, error = 'no config for repo ' + mark.repo.id }) }
  100. runChild([hybriel, dir() + '/../tools/runner.hl', 'drop', runnerData, cfg.clone, number], 60, (r) => {
  101. done({ ok = r.exit == 0 && !r.killed, error = r.err.join(' ') })
  102. return null
  103. })
  104. return null
  105. }
  106. legacy = (Hybrid job, done) => {
  107. appendFile(dataDir + '/runjobs.jsonl', JSON.stringify({ at = timestamp(), key = job.key, head = job.head, force = job.force, reset = job.reset, threadReplyOnly = job.threadReplyOnly, threads = job.threads.length, notes = job.notes, events = job.events, tries = job.tries }) + '
  108. ')
  109. slowFile = dataDir + '/slow-' + job.key.replace('#', '_')
  110. if (exists(slowFile)) { run('sleep 3') }
  111. failFile = dataDir + '/fail-' + job.key.replace('#', '_')
  112. if (exists(failFile)) { return done({ ok = false, error = 'test failure hook' }) }
  113. return done({ ok = true })
  114. }

Branches

Latest commits

  • c9b48bfeVendor Hybriel plugins; deploy ships bin/ from zig-outKunibert
  • a41ba15fKunibert: event intake, git cache, review runner, posting, linked tickets, deploy filesKunibert