gitoria

kunibert

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Main branchmainbd3591c7conductor: gates run on the live day44 binarymremain/lib/queue.hl

12.4 KB

  1. import { tryReadFile, writeFile, mkDir } from 'hl:fs'
  2. import { now, timestamp } from 'hl:time'
  3. import RunJob from './run-job.hl'
  4. \\ ONE process owns all jobs: no cross-process lock needed. State = <dataDir>/queue.json
  5. \\ and <dataDir>/deliveries.json (delivery id -> epoch ms, pruned after 7 days).
  6. \\ queue.json: jobs (one per PR key "<repo.id>#<number>"), closed (PR key -> { time, state, removeSession }),
  7. \\ pendingThreads (PR key -> thread updates waiting for the next run), lastHead (PR key -> head of the last run), seq.
  8. \\ A Hybrid copies on assignment: every change reads a job (find), changes it and writes it back (put).
  9. \\ job.status: queued | running | failed.
  10. String dataDir = ''
  11. Number maxTries = 3
  12. Number maxParallel = 3
  13. Number backoffSeconds = 30
  14. Number keepDeliveryDays = 7
  15. Hybrid repos = null
  16. String hybriel = 'hybriel'
  17. Number timeoutSeconds = 900
  18. String runnerData = ''
  19. String ticketsBase = ''
  20. String ticketsTokenFile = ''
  21. String reviewUser = ''
  22. String codexBin = 'codex'
  23. String workDir = ''
  24. busy = false
  25. jobs = []
  26. closed = {}
  27. pendingThreads = {}
  28. lastHead = {}
  29. seq = 0
  30. seen = {}
  31. running = []
  32. lastError = null
  33. runner = null
  34. load = () => {
  35. if (maxParallel < 1) { maxParallel = 1 }
  36. running = []
  37. mkDir(dataDir)
  38. runner = new RunJob(dataDir = dataDir, =repos, =hybriel, =timeoutSeconds, runnerData = runnerData == '' ? dataDir + '/runner' : runnerData, =ticketsBase, =ticketsTokenFile, =reviewUser, =codexBin, =workDir)
  39. q = tryReadFile(dataDir + '/queue.json')
  40. if (q != null) {
  41. state = JSON.parse(q)
  42. jobs = state.jobs
  43. closed = state.closed
  44. pendingThreads = state.pendingThreads
  45. lastHead = state.lastHead
  46. seq = state.seq
  47. \\ a job that was running when the process died is queued again
  48. for (let i = 0; i < jobs.length; i = i + 1) {
  49. if (jobs[i].status == 'running') {
  50. let j = jobs[i]
  51. j.status = 'queued'
  52. if (j.next != null) {
  53. n = j.next
  54. j = fold(j, n)
  55. if (!n.threadReplyOnly) { j.threadReplyOnly = false }
  56. j.next = null
  57. }
  58. jobs[i] = j
  59. }
  60. }
  61. }
  62. d = tryReadFile(dataDir + '/deliveries.json')
  63. if (d != null) { seen = JSON.parse(d) }
  64. saveQueue()
  65. pruneDeliveries()
  66. }
  67. saveQueue = () => {
  68. writeFile(dataDir + '/queue.json', JSON.stringify({ =jobs, =closed, =pendingThreads, =lastHead, =seq }))
  69. }
  70. saveSeen = () => {
  71. writeFile(dataDir + '/deliveries.json', JSON.stringify(seen))
  72. }
  73. wasSeen = (String id) => {
  74. return seen[id] != null
  75. }
  76. markSeen = (String id) => {
  77. seen[id] = now()
  78. saveSeen()
  79. }
  80. pruneDeliveries = () => {
  81. limit = now() - keepDeliveryDays * 86400 * 1000
  82. let kept = {}
  83. let dropped = 0
  84. for (id in seen) {
  85. if (seen[id] >= limit) { kept[id] = seen[id] } else { dropped = dropped + 1 }
  86. }
  87. if (dropped > 0) {
  88. seen = kept
  89. saveSeen()
  90. }
  91. return dropped
  92. }
  93. indexOf = (String key) => {
  94. for (let i = 0; i < jobs.length; i = i + 1) { if (jobs[i].key == key) { return i } }
  95. return -1
  96. }
  97. remove = (String key) => {
  98. let kept = []
  99. for (j of jobs) { if (j.key != key) { kept.push(j) } }
  100. jobs = kept
  101. }
  102. \\ fold a change into a job; returns the new job. Newest head wins by time; flags and lists merge.
  103. \\ A job stays threadReplyOnly only while every change in it is a reply without a new head.
  104. fold = (Hybrid job, Hybrid c) => {
  105. let j = job
  106. if (j.time == null || c.time == null || c.time >= j.time) {
  107. j.head = c.head
  108. j.time = c.time
  109. j.pull = c.pull
  110. j.repo = c.repo
  111. }
  112. if (c.force) { j.force = true }
  113. if (c.reset) { j.reset = true }
  114. for (t of c.threads) { j.threads.push(t) }
  115. for (n of c.notes) { j.notes.push(n) }
  116. j.events.push(c.event)
  117. j.updated = timestamp()
  118. return j
  119. }
  120. newJob = (Hybrid c, String key) => {
  121. seq = seq + 1
  122. let threads = []
  123. if (pendingThreads[key] != null) {
  124. threads = pendingThreads[key]
  125. delete pendingThreads[key]
  126. }
  127. return { =key, =seq, status = 'queued', tries = 0, notBefore = 0, repo = c.repo, number = c.number, head = null, time = null, pull = null, force = false, reset = false, threadReplyOnly = true, =threads, notes = [], events = [], created = timestamp(), updated = timestamp(), next = null, lastError = null }
  128. }
  129. \\ the head a thread-reply change is compared with: the job's own head, else the last head that was run
  130. baselineHead = (String key, Number i) => {
  131. if (i >= 0) { return jobs[i].head }
  132. return lastHead[key]
  133. }
  134. \\ a review change -> { action: 'queued'|'updated'|'follow-up'|'ignored', key, threadReplyOnly }
  135. enqueue = (Hybrid c) => {
  136. key = c.repoId + '#' + c.number
  137. mark = closed[key]
  138. if (mark != null) {
  139. if (c.event == 'pull_request.reopened' || c.event == 'pull_request.opened') {
  140. if (c.time == null || c.time >= mark.time) { delete closed[key] }
  141. }
  142. if (closed[key] != null) {
  143. saveQueue()
  144. return { action = 'ignored', reason = 'pr closed', =key }
  145. }
  146. }
  147. i = indexOf(key)
  148. \\ replies bring no new head when the head equals the baseline; everything else is a full review
  149. replyOnly = c.replyCandidate && c.head == baselineHead(key, i)
  150. let j = null
  151. if (i < 0) { j = newJob(c, key) } else { j = jobs[i] }
  152. if (i >= 0 && j.status == 'running') {
  153. if (j.next == null) { j.next = newJob(c, key) }
  154. n = fold(j.next, c)
  155. if (!replyOnly) { n.threadReplyOnly = false }
  156. j.next = n
  157. jobs[i] = j
  158. saveQueue()
  159. return { action = 'follow-up', =key, threadReplyOnly = n.threadReplyOnly }
  160. }
  161. j = fold(j, c)
  162. if (!replyOnly) { j.threadReplyOnly = false }
  163. if (j.status == 'failed') {
  164. j.status = 'queued'
  165. j.tries = 0
  166. j.notBefore = 0
  167. }
  168. if (i < 0) { jobs.push(j) } else { jobs[i] = j }
  169. saveQueue()
  170. let act = 'queued'
  171. if (i >= 0) { act = 'updated' }
  172. return { action = act, =key, threadReplyOnly = j.threadReplyOnly }
  173. }
  174. \\ review_thread resolved/unresolved: NO review. Merge the flip into the queued job, else store it for the next run.
  175. recordThread = (Hybrid c) => {
  176. key = c.repoId + '#' + c.number
  177. if (closed[key] != null) { return { action = 'ignored', reason = 'pr closed', =key } }
  178. i = indexOf(key)
  179. if (i >= 0 && jobs[i].status != 'running') {
  180. let j = jobs[i]
  181. for (t of c.threads) { j.threads.push(t) }
  182. j.updated = timestamp()
  183. jobs[i] = j
  184. saveQueue()
  185. return { action = 'thread-merged', =key }
  186. }
  187. let list = []
  188. if (pendingThreads[key] != null) { list = pendingThreads[key] }
  189. for (t of c.threads) { list.push(t) }
  190. pendingThreads[key] = list
  191. saveQueue()
  192. return { action = 'thread-stored', =key }
  193. }
  194. \\ closed/merged: drop the queued job and any stored thread flips, mark the PR closed and ITS SESSION FOR REMOVAL
  195. \\ (the runner acts on closed[key].removeSession). A running job is dropped when it ends.
  196. close = (Hybrid c) => {
  197. key = c.repoId + '#' + c.number
  198. closed[key] = { time = c.time, state = c.pull.state, removeSession = true, repo = c.repo }
  199. delete pendingThreads[key]
  200. delete lastHead[key]
  201. let dropped = false
  202. let kept = []
  203. for (j of jobs) {
  204. if (j.key != key) {
  205. kept.push(j)
  206. } else if (j.status == 'running') {
  207. let r = j
  208. r.next = null
  209. r.dropWhenDone = true
  210. kept.push(r)
  211. } else { dropped = true }
  212. }
  213. jobs = kept
  214. saveQueue()
  215. return { action = 'closed', =key, =dropped }
  216. }
  217. \\ Repo ids serialize reviews; identical clone URLs also serialize because they share cache/session paths.
  218. repoBusy = (Hybrid repo) => {
  219. for (key of running) {
  220. i = indexOf(key)
  221. if (i >= 0 && ('' + jobs[i].repo.id == '' + repo.id || (repo.clone != null && jobs[i].repo.clone == repo.clone))) { return true }
  222. }
  223. return false
  224. }
  225. \\ the oldest startable job: skip busy repos and jobs in backoff; respect the global cap
  226. nextJob = () => {
  227. if (running.length >= maxParallel) { return null }
  228. t = now()
  229. let best = null
  230. for (j of jobs) {
  231. if (j.status != 'queued' || j.notBefore > t || repoBusy(j.repo)) { continue }
  232. if (best == null || j.seq < best.seq) { best = j }
  233. }
  234. return best
  235. }
  236. \\ settle a job after runJob: success (remove, or the follow-up takes its place), or failure (retry/backoff, then failed)
  237. settle = (Hybrid job, r) => {
  238. let j = job
  239. i = indexOf(j.key)
  240. if (i < 0) { return null }
  241. if (j.dropWhenDone) {
  242. remove(j.key)
  243. console.log(timestamp() + ' worker: ' + j.key + ' closed while running, dropped')
  244. return null
  245. }
  246. if (r != null && r.ok) {
  247. console.log(timestamp() + ' worker: done ' + j.key)
  248. lastHead[j.key] = j.head
  249. if (j.next != null) {
  250. let n = j.next
  251. n.status = 'queued'
  252. n.seq = j.seq
  253. jobs[i] = n
  254. console.log(timestamp() + ' worker: follow-up for ' + j.key + ' queued: head=' + n.head)
  255. } else { remove(j.key) }
  256. return null
  257. }
  258. j.tries = j.tries + 1
  259. let err = 'runJob failed'
  260. if (r != null && r.error != null) { err = r.error }
  261. j.lastError = err
  262. lastError = j.key + ': ' + err
  263. if (j.next != null) {
  264. j = fold(j, j.next)
  265. j.next = null
  266. }
  267. if (j.tries >= maxTries) {
  268. j.status = 'failed'
  269. console.log(timestamp() + ' OPERATOR: job ' + j.key + ' FAILED after ' + j.tries + ' tries: ' + err)
  270. } else {
  271. j.status = 'queued'
  272. let wait = backoffSeconds * 1000
  273. for (let k = 1; k < j.tries; k = k + 1) { wait = wait * 2 }
  274. j.notBefore = now() + wait
  275. console.log(timestamp() + ' worker: ' + j.key + ' failed (' + err + '), try ' + j.tries + '/' + maxTries + ', retry in ' + (wait / 1000) + 's')
  276. }
  277. jobs[i] = j
  278. return null
  279. }
  280. \\ the job's child ended (or was killed): settle the stored copy of the job. This is the completion call.
  281. done = (String key, Hybrid res) => {
  282. let kept = []
  283. for (k of running) { if (k != key) { kept.push(k) } }
  284. running = kept
  285. i = indexOf(key)
  286. if (i >= 0) {
  287. \\ the job may have changed while it ran (follow-up, close): settle on the stored copy
  288. settle(jobs[i], res)
  289. }
  290. saveQueue()
  291. return key
  292. }
  293. \\ a closed PR whose session is still to be removed and which has no job any more
  294. dropCandidate = () => {
  295. for (k in closed) {
  296. if (closed[k].removeSession && indexOf(k) < 0 && !repoBusy(closed[k].repo)) { return k }
  297. }
  298. return null
  299. }
  300. \\ one timer tick: start ONE thing and return at once: the session removal of a closed PR, else the oldest job.
  301. \\ The result arrives later through done() / the drop callback.
  302. tick = () => {
  303. if (busy) { return null }
  304. let dk = null
  305. if (repos != null) { dk = dropCandidate() }
  306. if (dk != null) {
  307. busy = true
  308. parts = dk.split('#')
  309. num = parts[parts.length - 1]
  310. console.log(timestamp() + ' worker: removing the session of closed ' + dk)
  311. runner.dropSession(closed[dk], num, (r) => {
  312. busy = false
  313. if (r.ok) {
  314. let m = closed[dk]
  315. m.removeSession = false
  316. m.removed = timestamp()
  317. closed[dk] = m
  318. console.log(timestamp() + ' worker: session of ' + dk + ' removed, mark cleared')
  319. } else { console.log(timestamp() + ' worker: removing the session of ' + dk + ' failed: ' + r.error) }
  320. saveQueue()
  321. return null
  322. })
  323. return dk
  324. }
  325. j = nextJob()
  326. if (j == null) { return null }
  327. let r = j
  328. r.status = 'running'
  329. r.next = null
  330. jobs[indexOf(r.key)] = r
  331. running.push(r.key)
  332. saveQueue()
  333. console.log(timestamp() + ' worker: start ' + r.key + ' head=' + r.head + ' try=' + (r.tries + 1))
  334. k = r.key
  335. runner.runJob(r, (res) => { return done(k, res) })
  336. return r.key
  337. }
  338. countSeen = () => {
  339. let n = 0
  340. for (id in seen) { n = n + 1 }
  341. return n
  342. }
  343. health = () => {
  344. let queued = 0
  345. let failed = 0
  346. for (j of jobs) {
  347. if (j.status == 'failed') { failed = failed + 1 } else { queued = queued + 1 }
  348. }
  349. let pending = 0
  350. for (k in pendingThreads) { pending = pending + 1 }
  351. let toRemove = []
  352. for (k in closed) { if (closed[k].removeSession) { toRemove.push(k) } }
  353. return { jobs = jobs.length, =queued, =failed, =running, =lastError, deliveries = countSeen(), pendingThreadPrs = pending, sessionsToRemove = toRemove }
  354. }

Branches

Latest commits

  • bd3591c7conductor: gates run on the live day44 binarymre
  • 47f747a7records: jukebox #9 fix livemre
  • 0a3169fdMerge branch 'vendored-size' into mainKunibert
  • e447bb09Vendored size fix: keep renames with an app side whole; type the filter parameter (L10)mre
  • 99ccc742Vendored size fix: no renames in the app diff, filter the resumed delta toomre
  • 1a52e100|||PR Diff size limit counts only app files when vendored paths are setmre
  • 50a1867estatus: jukebox #9mre
  • c57484e6log: jukebox #9 diff sizemre
  • 795a20carecords: guard fix live (PR 8)mre
  • 6981d96fMerge branch 'fresh-on-nocmd' into mainKunibert
  • 37c47840runner: one shared prompt for both fresh-session fallbacks (L16)mre
  • f945d2e1|||PR runner: a resumed run with no command on a new head gets one fresh session instead of a tool errormre
  • 10b2a49estatus: PR 314, guard fix proposedmre
  • e73995a3log: hycon PR 314 approved after fresh reviewmre
  • f60427falog: hycon PR 314 false tool errormre
  • 481974f7log: Hermes accepts PR 259 findingmre
  • bb0976acrecords: hycon PR 259 design findingmre
  • 5c637c24records: hycon PR 259 false tool errormre
  • a344c406log: routger 132 reviewedmre
  • 56e70dcbrecords: review time limit 1800 s (routger 132)mre