baqueue 1.0.2__tar.gz → 1.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {baqueue-1.0.2/baqueue.egg-info → baqueue-1.1.0}/PKG-INFO +1 -1
- {baqueue-1.0.2 → baqueue-1.1.0}/baqueue/__init__.py +1 -1
- {baqueue-1.0.2 → baqueue-1.1.0}/baqueue/dashboard/api.py +8 -1
- {baqueue-1.0.2 → baqueue-1.1.0}/baqueue/dashboard/server.py +5 -0
- {baqueue-1.0.2 → baqueue-1.1.0}/baqueue/dashboard/static/app.js +38 -0
- {baqueue-1.0.2 → baqueue-1.1.0}/baqueue/dashboard/static/index.html +62 -18
- {baqueue-1.0.2 → baqueue-1.1.0}/baqueue/dashboard/static/style.css +17 -1
- {baqueue-1.0.2 → baqueue-1.1.0}/baqueue/drivers/base.py +14 -0
- {baqueue-1.0.2 → baqueue-1.1.0}/baqueue/drivers/memory_driver.py +15 -0
- {baqueue-1.0.2 → baqueue-1.1.0}/baqueue/drivers/postgres_driver.py +18 -0
- {baqueue-1.0.2 → baqueue-1.1.0}/baqueue/drivers/redis_driver.py +26 -0
- {baqueue-1.0.2 → baqueue-1.1.0}/baqueue/drivers/sqlite_driver.py +17 -0
- {baqueue-1.0.2 → baqueue-1.1.0}/baqueue/serializer.py +13 -3
- {baqueue-1.0.2 → baqueue-1.1.0}/baqueue/worker.py +42 -1
- {baqueue-1.0.2 → baqueue-1.1.0/baqueue.egg-info}/PKG-INFO +1 -1
- {baqueue-1.0.2 → baqueue-1.1.0}/LICENSE +0 -0
- {baqueue-1.0.2 → baqueue-1.1.0}/MANIFEST.in +0 -0
- {baqueue-1.0.2 → baqueue-1.1.0}/README.md +0 -0
- {baqueue-1.0.2 → baqueue-1.1.0}/baqueue/balancer.py +0 -0
- {baqueue-1.0.2 → baqueue-1.1.0}/baqueue/batch.py +0 -0
- {baqueue-1.0.2 → baqueue-1.1.0}/baqueue/cli.py +0 -0
- {baqueue-1.0.2 → baqueue-1.1.0}/baqueue/config.py +0 -0
- {baqueue-1.0.2 → baqueue-1.1.0}/baqueue/dashboard/__init__.py +0 -0
- {baqueue-1.0.2 → baqueue-1.1.0}/baqueue/drivers/__init__.py +0 -0
- {baqueue-1.0.2 → baqueue-1.1.0}/baqueue/events.py +0 -0
- {baqueue-1.0.2 → baqueue-1.1.0}/baqueue/job.py +0 -0
- {baqueue-1.0.2 → baqueue-1.1.0}/baqueue/pruner.py +0 -0
- {baqueue-1.0.2 → baqueue-1.1.0}/baqueue/queue.py +0 -0
- {baqueue-1.0.2 → baqueue-1.1.0}/baqueue/retry.py +0 -0
- {baqueue-1.0.2 → baqueue-1.1.0}/baqueue/scheduler.py +0 -0
- {baqueue-1.0.2 → baqueue-1.1.0}/baqueue/supervisor.py +0 -0
- {baqueue-1.0.2 → baqueue-1.1.0}/baqueue.egg-info/SOURCES.txt +0 -0
- {baqueue-1.0.2 → baqueue-1.1.0}/baqueue.egg-info/dependency_links.txt +0 -0
- {baqueue-1.0.2 → baqueue-1.1.0}/baqueue.egg-info/entry_points.txt +0 -0
- {baqueue-1.0.2 → baqueue-1.1.0}/baqueue.egg-info/requires.txt +0 -0
- {baqueue-1.0.2 → baqueue-1.1.0}/baqueue.egg-info/top_level.txt +0 -0
- {baqueue-1.0.2 → baqueue-1.1.0}/pyproject.toml +0 -0
- {baqueue-1.0.2 → baqueue-1.1.0}/setup.cfg +0 -0
|
@@ -117,7 +117,10 @@ class DashboardAPI:
|
|
|
117
117
|
created_from=created_from, created_to=created_to,
|
|
118
118
|
)
|
|
119
119
|
return {
|
|
120
|
-
|
|
120
|
+
# The list view never renders per-attempt history (the modal fetches
|
|
121
|
+
# job_detail for that), so omit it to keep the list and the live
|
|
122
|
+
# /ws/jobs push lean.
|
|
123
|
+
"jobs": [j.to_dict(include_history=False) for j in jobs],
|
|
121
124
|
"page": page,
|
|
122
125
|
"per_page": per_page,
|
|
123
126
|
"count": len(jobs),
|
|
@@ -128,6 +131,10 @@ class DashboardAPI:
|
|
|
128
131
|
job = await self.driver.get_job(job_id)
|
|
129
132
|
return job.to_dict() if job else None
|
|
130
133
|
|
|
134
|
+
async def promote_job(self, job_id: str) -> bool:
|
|
135
|
+
"""Make a scheduled/pending job runnable immediately. Returns True on success."""
|
|
136
|
+
return await self.driver.promote(job_id)
|
|
137
|
+
|
|
131
138
|
async def retry_job(self, job_id: str) -> bool:
|
|
132
139
|
job = await self.driver.get_job(job_id)
|
|
133
140
|
if not job or job.status != "failed":
|
|
@@ -150,6 +150,11 @@ def create_app(driver: BaseDriver, config: Optional[BaQueueConfig] = None) -> An
|
|
|
150
150
|
ok = await api.retry_job(job_id)
|
|
151
151
|
return JSONResponse({"success": ok})
|
|
152
152
|
|
|
153
|
+
@app.post("/api/jobs/{job_id}/execute")
|
|
154
|
+
async def execute_job(job_id: str):
|
|
155
|
+
ok = await api.promote_job(job_id)
|
|
156
|
+
return JSONResponse({"success": ok})
|
|
157
|
+
|
|
153
158
|
@app.delete("/api/jobs/{job_id}")
|
|
154
159
|
async def delete_job(job_id: str):
|
|
155
160
|
ok = await api.delete_job(job_id)
|
|
@@ -322,6 +322,14 @@ document.addEventListener("alpine:init", () => {
|
|
|
322
322
|
this.fetchOverview();
|
|
323
323
|
},
|
|
324
324
|
|
|
325
|
+
async executeJob(jobId) {
|
|
326
|
+
// Promote a scheduled/pending job so it runs immediately.
|
|
327
|
+
await fetch(`/api/jobs/${jobId}/execute`, { method: "POST" });
|
|
328
|
+
this.closeModal();
|
|
329
|
+
this.fetchJobs();
|
|
330
|
+
this.fetchOverview();
|
|
331
|
+
},
|
|
332
|
+
|
|
325
333
|
async retryAllFailed() {
|
|
326
334
|
const parts = [];
|
|
327
335
|
if (this.jobsFilter.queue) parts.push(`queue "${this.jobsFilter.queue}"`);
|
|
@@ -437,6 +445,36 @@ document.addEventListener("alpine:init", () => {
|
|
|
437
445
|
return Math.floor(diff / 60) + "m " + Math.floor(diff % 60) + "s";
|
|
438
446
|
},
|
|
439
447
|
|
|
448
|
+
// ── Per-attempt timeline ────────────────────────────────
|
|
449
|
+
|
|
450
|
+
attemptHistory(job) {
|
|
451
|
+
return job && Array.isArray(job.history) ? job.history : [];
|
|
452
|
+
},
|
|
453
|
+
|
|
454
|
+
hasHistory(job) {
|
|
455
|
+
return this.attemptHistory(job).length > 0;
|
|
456
|
+
},
|
|
457
|
+
|
|
458
|
+
// A job currently processing has an in-flight attempt that isn't recorded in
|
|
459
|
+
// history yet (entries are appended only when an attempt concludes).
|
|
460
|
+
inFlightAttempt(job) {
|
|
461
|
+
return !!(job && job.status === "processing" && job.started_at);
|
|
462
|
+
},
|
|
463
|
+
|
|
464
|
+
attemptDotClass(entry) {
|
|
465
|
+
return entry && entry.status === "completed" ? "completed" : "failed";
|
|
466
|
+
},
|
|
467
|
+
|
|
468
|
+
attemptDuration(entry) {
|
|
469
|
+
if (!entry || !entry.started_at || !entry.finished_at) return "";
|
|
470
|
+
const diff = entry.finished_at - entry.started_at;
|
|
471
|
+
if (diff < 0) return "";
|
|
472
|
+
if (diff < 0.001) return "<1ms";
|
|
473
|
+
if (diff < 1) return Math.round(diff * 1000) + "ms";
|
|
474
|
+
if (diff < 60) return diff.toFixed(1) + "s";
|
|
475
|
+
return Math.floor(diff / 60) + "m " + Math.floor(diff % 60) + "s";
|
|
476
|
+
},
|
|
477
|
+
|
|
440
478
|
shortId(id) {
|
|
441
479
|
return id ? id.substring(0, 12) : "-";
|
|
442
480
|
},
|
|
@@ -511,27 +511,67 @@
|
|
|
511
511
|
<span class="tl-time" x-text="formatTimeFull(selectedJob.delay_until)"></span>
|
|
512
512
|
</div>
|
|
513
513
|
</div>
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
514
|
+
<!-- Per-attempt history (jobs that ran at least once on a
|
|
515
|
+
driver that persists history). Each backoff retry is its
|
|
516
|
+
own entry. -->
|
|
517
|
+
<template x-for="(entry, idx) in attemptHistory(selectedJob)" :key="idx">
|
|
518
|
+
<div class="tl-item">
|
|
519
|
+
<div class="tl-dot" :class="attemptDotClass(entry)"></div>
|
|
520
|
+
<div class="tl-content">
|
|
521
|
+
<span class="tl-label">
|
|
522
|
+
Attempt <span x-text="entry.attempt"></span> ·
|
|
523
|
+
<span x-text="entry.status"></span>
|
|
524
|
+
<span class="tl-dur" x-show="attemptDuration(entry)" x-text="'(' + attemptDuration(entry) + ')'"></span>
|
|
525
|
+
</span>
|
|
526
|
+
<span class="tl-time" x-text="formatTimeFull(entry.started_at) + (entry.finished_at ? ' → ' + formatTimeFull(entry.finished_at) : '')"></span>
|
|
527
|
+
<span class="tl-retry" x-show="entry.will_retry">
|
|
528
|
+
Retry scheduled <span x-text="entry.next_retry_at ? scheduledIn(entry.next_retry_at) : ''"></span>
|
|
529
|
+
</span>
|
|
530
|
+
<pre class="tl-error" x-show="entry.error" x-text="entry.error"></pre>
|
|
531
|
+
</div>
|
|
519
532
|
</div>
|
|
520
|
-
</
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
<
|
|
533
|
+
</template>
|
|
534
|
+
<!-- The currently-running attempt is not recorded in history
|
|
535
|
+
until it concludes, so surface it live. -->
|
|
536
|
+
<template x-if="inFlightAttempt(selectedJob)">
|
|
537
|
+
<div class="tl-item">
|
|
538
|
+
<div class="tl-dot processing"></div>
|
|
539
|
+
<div class="tl-content">
|
|
540
|
+
<span class="tl-label">Attempt <span x-text="selectedJob.attempts"></span> · running…</span>
|
|
541
|
+
<span class="tl-time" x-text="formatTimeFull(selectedJob.started_at)"></span>
|
|
542
|
+
</div>
|
|
526
543
|
</div>
|
|
527
|
-
</
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
544
|
+
</template>
|
|
545
|
+
|
|
546
|
+
<!-- Legacy single-attempt timeline: jobs created before history
|
|
547
|
+
tracking, or on drivers that don't persist history. -->
|
|
548
|
+
<template x-if="!hasHistory(selectedJob) && !inFlightAttempt(selectedJob) && selectedJob.started_at">
|
|
549
|
+
<div class="tl-item">
|
|
550
|
+
<div class="tl-dot processing"></div>
|
|
551
|
+
<div class="tl-content">
|
|
552
|
+
<span class="tl-label">Started</span>
|
|
553
|
+
<span class="tl-time" x-text="formatTimeFull(selectedJob.started_at)"></span>
|
|
554
|
+
</div>
|
|
533
555
|
</div>
|
|
534
|
-
</
|
|
556
|
+
</template>
|
|
557
|
+
<template x-if="!hasHistory(selectedJob) && selectedJob.completed_at">
|
|
558
|
+
<div class="tl-item">
|
|
559
|
+
<div class="tl-dot completed"></div>
|
|
560
|
+
<div class="tl-content">
|
|
561
|
+
<span class="tl-label">Completed</span>
|
|
562
|
+
<span class="tl-time" x-text="formatTimeFull(selectedJob.completed_at)"></span>
|
|
563
|
+
</div>
|
|
564
|
+
</div>
|
|
565
|
+
</template>
|
|
566
|
+
<template x-if="!hasHistory(selectedJob) && selectedJob.failed_at">
|
|
567
|
+
<div class="tl-item">
|
|
568
|
+
<div class="tl-dot failed"></div>
|
|
569
|
+
<div class="tl-content">
|
|
570
|
+
<span class="tl-label">Failed</span>
|
|
571
|
+
<span class="tl-time" x-text="formatTimeFull(selectedJob.failed_at)"></span>
|
|
572
|
+
</div>
|
|
573
|
+
</div>
|
|
574
|
+
</template>
|
|
535
575
|
</div>
|
|
536
576
|
</div>
|
|
537
577
|
|
|
@@ -563,6 +603,10 @@
|
|
|
563
603
|
</div>
|
|
564
604
|
|
|
565
605
|
<div class="modal-actions">
|
|
606
|
+
<button class="btn-primary" x-show="isScheduled(selectedJob)" @click="executeJob(selectedJob.id)">
|
|
607
|
+
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="16" height="16"><polygon points="5 3 19 12 5 21 5 3"/></svg>
|
|
608
|
+
Execute Now
|
|
609
|
+
</button>
|
|
566
610
|
<button class="btn-primary" x-show="selectedJob.status === 'failed'" @click="retryJob(selectedJob.id)">
|
|
567
611
|
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="16" height="16"><polyline points="23 4 23 10 17 10"/><path d="M20.49 15a9 9 0 11-2.12-9.36L23 10"/></svg>
|
|
568
612
|
Retry Job
|
|
@@ -1267,8 +1267,24 @@ body {
|
|
|
1267
1267
|
.tl-dot.failed { border-color: var(--red); background: var(--red); }
|
|
1268
1268
|
|
|
1269
1269
|
.tl-content { display: flex; flex-direction: column; gap: 1px; }
|
|
1270
|
-
.tl-label { font-size: 13px; font-weight: 600; }
|
|
1270
|
+
.tl-label { font-size: 13px; font-weight: 600; text-transform: capitalize; }
|
|
1271
1271
|
.tl-time { font-size: 12px; color: var(--text-muted); font-family: 'JetBrains Mono', monospace; }
|
|
1272
|
+
.tl-dur { font-weight: 400; color: var(--text-muted); }
|
|
1273
|
+
.tl-retry { font-size: 12px; color: var(--amber); }
|
|
1274
|
+
.tl-error {
|
|
1275
|
+
margin: 4px 0 0;
|
|
1276
|
+
padding: 6px 8px;
|
|
1277
|
+
font-size: 11px;
|
|
1278
|
+
font-family: 'JetBrains Mono', monospace;
|
|
1279
|
+
color: var(--red);
|
|
1280
|
+
background: var(--bg-surface);
|
|
1281
|
+
border: 1px solid var(--border);
|
|
1282
|
+
border-radius: 6px;
|
|
1283
|
+
white-space: pre-wrap;
|
|
1284
|
+
word-break: break-word;
|
|
1285
|
+
max-height: 140px;
|
|
1286
|
+
overflow: auto;
|
|
1287
|
+
}
|
|
1272
1288
|
|
|
1273
1289
|
/* ── Tags ───────────────────────────────────────────────── */
|
|
1274
1290
|
|
|
@@ -115,6 +115,20 @@ class BaseDriver(ABC):
|
|
|
115
115
|
@abstractmethod
|
|
116
116
|
async def delete(self, job_id: str) -> None: ...
|
|
117
117
|
|
|
118
|
+
async def promote(self, job_id: str) -> bool:
|
|
119
|
+
"""Make a scheduled/pending job runnable immediately (clear its delay).
|
|
120
|
+
|
|
121
|
+
Returns True if the job was promoted, False if it does not exist or is not
|
|
122
|
+
in the ``pending`` state. Concrete (non-abstract) so existing third-party
|
|
123
|
+
drivers keep working; the built-in drivers override it with a race-safe,
|
|
124
|
+
index-aware version. The default relies on ``release(delay=0)`` to enqueue
|
|
125
|
+
the job for immediate processing."""
|
|
126
|
+
job = await self.get_job(job_id)
|
|
127
|
+
if job is None or job.status != "pending":
|
|
128
|
+
return False
|
|
129
|
+
await self.release(job, delay=0)
|
|
130
|
+
return True
|
|
131
|
+
|
|
118
132
|
# ── Query ───────────────────────────────────────────────────
|
|
119
133
|
|
|
120
134
|
@abstractmethod
|
|
@@ -116,6 +116,21 @@ class MemoryDriver(BaseDriver):
|
|
|
116
116
|
if job_id in self._delayed:
|
|
117
117
|
self._delayed.remove(job_id)
|
|
118
118
|
|
|
119
|
+
async def promote(self, job_id: str) -> bool:
|
|
120
|
+
async with self._lock:
|
|
121
|
+
payload = self._jobs.get(job_id)
|
|
122
|
+
if payload is None or payload.status != "pending":
|
|
123
|
+
return False
|
|
124
|
+
payload.delay_until = None
|
|
125
|
+
payload.updated_at = _now_ts()
|
|
126
|
+
if job_id in self._delayed:
|
|
127
|
+
self._delayed.remove(job_id)
|
|
128
|
+
# Only enqueue if it isn't already ready, so promoting a non-delayed
|
|
129
|
+
# pending job can never duplicate it in the ready list.
|
|
130
|
+
if job_id not in self._queues[payload.queue]:
|
|
131
|
+
self._queues[payload.queue].append(job_id)
|
|
132
|
+
return True
|
|
133
|
+
|
|
119
134
|
# ── Query ───────────────────────────────────────────────────
|
|
120
135
|
|
|
121
136
|
async def get_job(self, job_id: str) -> JobPayload | None:
|
|
@@ -324,6 +324,24 @@ class PostgresDriver(BaseDriver):
|
|
|
324
324
|
|
|
325
325
|
await self._with_disk_full_recovery(_do)
|
|
326
326
|
|
|
327
|
+
async def promote(self, job_id: str) -> bool:
|
|
328
|
+
now = _now_ts()
|
|
329
|
+
|
|
330
|
+
async def _do():
|
|
331
|
+
async with self._pool.acquire() as conn:
|
|
332
|
+
# Clearing delay_until is enough: pop() already accepts a pending
|
|
333
|
+
# row whose delay_until IS NULL or has elapsed.
|
|
334
|
+
return await conn.fetchrow(
|
|
335
|
+
f"""UPDATE {self._jobs_table}
|
|
336
|
+
SET delay_until=NULL, updated_at=$1
|
|
337
|
+
WHERE id=$2 AND status='pending'
|
|
338
|
+
RETURNING id""",
|
|
339
|
+
now, job_id,
|
|
340
|
+
)
|
|
341
|
+
|
|
342
|
+
row = await self._with_disk_full_recovery(_do)
|
|
343
|
+
return row is not None
|
|
344
|
+
|
|
327
345
|
# ── Query ───────────────────────────────────────────────────
|
|
328
346
|
|
|
329
347
|
async def get_job(self, job_id: str) -> JobPayload | None:
|
|
@@ -310,6 +310,32 @@ class RedisDriver(BaseDriver):
|
|
|
310
310
|
await pipe.execute()
|
|
311
311
|
await self._with_disk_full_recovery(_do)
|
|
312
312
|
|
|
313
|
+
async def promote(self, job_id: str) -> bool:
|
|
314
|
+
raw = await self._redis.hget(self._key("job", job_id), "data")
|
|
315
|
+
if not raw:
|
|
316
|
+
return False
|
|
317
|
+
payload = JobPayload.from_json(raw)
|
|
318
|
+
if payload.status != "pending":
|
|
319
|
+
return False
|
|
320
|
+
now = _now_ts()
|
|
321
|
+
# Only a job actually sitting in the delayed ZSET needs to be moved into
|
|
322
|
+
# its ready list. A pending job that is already ready (delay_until None or
|
|
323
|
+
# in the past) must NOT be re-pushed, or Redis pop — which does not
|
|
324
|
+
# re-check status — would process it twice.
|
|
325
|
+
was_scheduled = payload.delay_until is not None and payload.delay_until > now
|
|
326
|
+
payload.delay_until = None
|
|
327
|
+
payload.updated_at = now
|
|
328
|
+
|
|
329
|
+
async def _do():
|
|
330
|
+
pipe = self._redis.pipeline()
|
|
331
|
+
pipe.hset(self._key("job", job_id), mapping={"data": payload.to_json()})
|
|
332
|
+
if was_scheduled:
|
|
333
|
+
pipe.zrem(self._key("delayed"), job_id)
|
|
334
|
+
pipe.rpush(self._key("queue", payload.queue), job_id)
|
|
335
|
+
await pipe.execute()
|
|
336
|
+
await self._with_disk_full_recovery(_do)
|
|
337
|
+
return True
|
|
338
|
+
|
|
313
339
|
# ── Query ───────────────────────────────────────────────────
|
|
314
340
|
|
|
315
341
|
async def get_job(self, job_id: str) -> JobPayload | None:
|
|
@@ -377,6 +377,23 @@ class SqliteDriver(BaseDriver):
|
|
|
377
377
|
c.commit()
|
|
378
378
|
await self._execute_with_retry(_do)
|
|
379
379
|
|
|
380
|
+
async def promote(self, job_id: str) -> bool:
|
|
381
|
+
now = _now_ts()
|
|
382
|
+
async with self._lock:
|
|
383
|
+
result = [False]
|
|
384
|
+
def _do():
|
|
385
|
+
c = self._get_conn()
|
|
386
|
+
# Clearing delay_until is enough: pop() already accepts a pending
|
|
387
|
+
# row whose delay_until IS NULL or has elapsed.
|
|
388
|
+
cur = c.execute(
|
|
389
|
+
"UPDATE jobs SET delay_until=NULL, updated_at=? WHERE id=? AND status='pending'",
|
|
390
|
+
(now, job_id),
|
|
391
|
+
)
|
|
392
|
+
c.commit()
|
|
393
|
+
result[0] = cur.rowcount == 1
|
|
394
|
+
await self._execute_with_retry(_do)
|
|
395
|
+
return result[0]
|
|
396
|
+
|
|
380
397
|
# ── Query ───────────────────────────────────────────────────
|
|
381
398
|
|
|
382
399
|
async def get_job(self, job_id: str) -> JobPayload | None:
|
|
@@ -35,6 +35,7 @@ class JobPayload:
|
|
|
35
35
|
"failed_at",
|
|
36
36
|
"status",
|
|
37
37
|
"error",
|
|
38
|
+
"history",
|
|
38
39
|
)
|
|
39
40
|
|
|
40
41
|
def __init__(
|
|
@@ -58,6 +59,7 @@ class JobPayload:
|
|
|
58
59
|
failed_at: float | None = None,
|
|
59
60
|
status: str = "pending",
|
|
60
61
|
error: str | None = None,
|
|
62
|
+
history: list[dict[str, Any]] | None = None,
|
|
61
63
|
):
|
|
62
64
|
self.id = id or uuid4().hex
|
|
63
65
|
self.job_class = job_class
|
|
@@ -77,9 +79,14 @@ class JobPayload:
|
|
|
77
79
|
self.failed_at = failed_at
|
|
78
80
|
self.status = status
|
|
79
81
|
self.error = error
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
82
|
+
# Per-attempt execution history (one record per processing attempt).
|
|
83
|
+
# Bounded by the number of attempts; persisted only by drivers that store
|
|
84
|
+
# the full payload (memory, redis). Older payloads without this key load
|
|
85
|
+
# as an empty list, so the field is fully backward compatible.
|
|
86
|
+
self.history = history or []
|
|
87
|
+
|
|
88
|
+
def to_dict(self, *, include_history: bool = True) -> dict[str, Any]:
|
|
89
|
+
d = {
|
|
83
90
|
"id": self.id,
|
|
84
91
|
"job_class": self.job_class,
|
|
85
92
|
"data": self.data,
|
|
@@ -99,6 +106,9 @@ class JobPayload:
|
|
|
99
106
|
"status": self.status,
|
|
100
107
|
"error": self.error,
|
|
101
108
|
}
|
|
109
|
+
if include_history:
|
|
110
|
+
d["history"] = self.history
|
|
111
|
+
return d
|
|
102
112
|
|
|
103
113
|
def to_json(self) -> str:
|
|
104
114
|
return json.dumps(self.to_dict())
|
|
@@ -11,10 +11,16 @@ from baqueue.drivers.base import BaseDriver
|
|
|
11
11
|
from baqueue.events import EventBus
|
|
12
12
|
from baqueue.job import Job, FunctionJob
|
|
13
13
|
from baqueue.retry import compute_delay, should_retry
|
|
14
|
-
from baqueue.serializer import JobPayload, resolve_job_class
|
|
14
|
+
from baqueue.serializer import JobPayload, resolve_job_class, _now_ts
|
|
15
15
|
|
|
16
16
|
logger = logging.getLogger("baqueue.worker")
|
|
17
17
|
|
|
18
|
+
# Per-attempt errors stored in JobPayload.history are truncated to this many
|
|
19
|
+
# characters. The job's top-level `error` field keeps the full latest traceback;
|
|
20
|
+
# this bound keeps the history (and therefore the stored payload) from growing
|
|
21
|
+
# large across retries.
|
|
22
|
+
_HISTORY_ERROR_MAXLEN = 1000
|
|
23
|
+
|
|
18
24
|
|
|
19
25
|
class Worker:
|
|
20
26
|
"""Pulls and executes jobs from one or more queues."""
|
|
@@ -84,6 +90,33 @@ class Worker:
|
|
|
84
90
|
return job
|
|
85
91
|
return None
|
|
86
92
|
|
|
93
|
+
@staticmethod
|
|
94
|
+
def _record_attempt(
|
|
95
|
+
payload: JobPayload,
|
|
96
|
+
*,
|
|
97
|
+
status: str,
|
|
98
|
+
finished_at: float,
|
|
99
|
+
error: str | None = None,
|
|
100
|
+
will_retry: bool = False,
|
|
101
|
+
next_retry_at: float | None = None,
|
|
102
|
+
) -> None:
|
|
103
|
+
"""Append one bounded record describing the attempt that just concluded.
|
|
104
|
+
|
|
105
|
+
Called once per attempt, right before the driver persists the new state, so
|
|
106
|
+
drivers that store the whole payload (memory, redis) keep the full history.
|
|
107
|
+
The list is bounded by the number of attempts and the error is truncated."""
|
|
108
|
+
if error is not None and len(error) > _HISTORY_ERROR_MAXLEN:
|
|
109
|
+
error = error[:_HISTORY_ERROR_MAXLEN] + "…"
|
|
110
|
+
payload.history.append({
|
|
111
|
+
"attempt": payload.attempts,
|
|
112
|
+
"started_at": payload.started_at,
|
|
113
|
+
"finished_at": finished_at,
|
|
114
|
+
"status": status,
|
|
115
|
+
"error": error,
|
|
116
|
+
"will_retry": will_retry,
|
|
117
|
+
"next_retry_at": next_retry_at,
|
|
118
|
+
})
|
|
119
|
+
|
|
87
120
|
async def _process(self, payload: JobPayload) -> None:
|
|
88
121
|
self._current_job = payload
|
|
89
122
|
job_timeout = payload.timeout or self.timeout
|
|
@@ -99,6 +132,7 @@ class Worker:
|
|
|
99
132
|
timeout=job_timeout,
|
|
100
133
|
)
|
|
101
134
|
|
|
135
|
+
self._record_attempt(payload, status="completed", finished_at=_now_ts())
|
|
102
136
|
await self.driver.complete(payload)
|
|
103
137
|
await self.driver.record_metric(payload.queue, "completed", 1)
|
|
104
138
|
await self.events.emit("job.completed", payload=payload, result=result, worker=self.name)
|
|
@@ -118,9 +152,16 @@ class Worker:
|
|
|
118
152
|
|
|
119
153
|
if should_retry(payload.attempts, payload.max_attempts):
|
|
120
154
|
delay = compute_delay(payload.backoff, payload.attempts)
|
|
155
|
+
self._record_attempt(
|
|
156
|
+
payload, status="failed", finished_at=_now_ts(),
|
|
157
|
+
error=error_msg, will_retry=True, next_retry_at=_now_ts() + delay,
|
|
158
|
+
)
|
|
121
159
|
await self.driver.release(payload, delay=delay)
|
|
122
160
|
await self.events.emit("job.retrying", payload=payload, error=error_msg, delay=delay)
|
|
123
161
|
else:
|
|
162
|
+
self._record_attempt(
|
|
163
|
+
payload, status="failed", finished_at=_now_ts(), error=error_msg,
|
|
164
|
+
)
|
|
124
165
|
await self.driver.fail(payload, error_msg)
|
|
125
166
|
await self.driver.record_metric(payload.queue, "failed", 1)
|
|
126
167
|
await self.events.emit("job.failed", payload=payload, error=error_msg, worker=self.name)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|