-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.py
More file actions
213 lines (171 loc) · 6.24 KB
/
Copy pathworker.py
File metadata and controls
213 lines (171 loc) · 6.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
"""Standalone worker process for document ingestion.
Usage:
python -m minikb.worker
This runs as a separate process from the web server, consuming
ingest jobs from the database queue.
"""
from __future__ import annotations
import asyncio
import logging
import signal
import sys
import time
import uuid
from datetime import datetime, timedelta
from typing import Any
from sqlalchemy import select, update
from minikb.config.settings import get_settings
from minikb.db import (
Document,
DocumentStatus,
IngestJob,
JobStatus,
close_db,
get_session,
)
from minikb.ingest.workers import IngestPipeline
logger = logging.getLogger("minikb.worker")
# Graceful shutdown flag
_shutdown = False
def _signal_handler(signum: int, frame: Any) -> None:
global _shutdown
logger.info("Received signal %s, shutting down gracefully...", signum)
_shutdown = True
async def poll_jobs(limit: int = 5) -> list[dict[str, Any]]:
"""Poll for queued ingest jobs."""
async for session in get_session():
stmt = (
select(IngestJob)
.where(IngestJob.status == JobStatus.QUEUED)
.order_by(IngestJob.created_at.asc())
.limit(limit)
)
result = await session.execute(stmt)
jobs = result.scalars().all()
if not jobs:
return []
job_ids = [j.id for j in jobs]
# Atomically mark as running (optimistic lock)
update_stmt = (
update(IngestJob)
.where(
IngestJob.id.in_(job_ids),
IngestJob.status == JobStatus.QUEUED,
)
.values(status=JobStatus.RUNNING, started_at=datetime.utcnow())
.returning(IngestJob.id, IngestJob.document_id)
)
update_result = await session.execute(update_stmt)
await session.commit()
return [
{"job_id": row.id, "document_id": row.document_id}
for row in update_result
]
async def process_job(job_id: uuid.UUID, document_id: uuid.UUID) -> bool:
"""Process a single ingest job. Returns True on success."""
try:
pipeline = IngestPipeline()
await pipeline.process_document(document_id, job_id)
return True
except Exception as e:
logger.exception("Job %s failed: %s", job_id, e)
# Mark job as failed
async for session in get_session():
stmt = select(IngestJob).where(IngestJob.id == job_id)
result = await session.execute(stmt)
job = result.scalar_one_or_none()
if job:
job.status = JobStatus.FAILED
job.last_error = str(e)[:1000]
job.ended_at = datetime.utcnow()
job.attempts = (job.attempts or 0) + 1
await session.commit()
# Check if we should retry
async for session in get_session():
stmt = select(IngestJob).where(IngestJob.id == job_id)
result = await session.execute(stmt)
job = result.scalar_one_or_none()
if job and job.attempts < 3:
# Re-queue with backoff
job.status = JobStatus.QUEUED
job.started_at = None
job.ended_at = None
await session.commit()
logger.info("Job %s re-queued (attempt %d)", job_id, job.attempts + 1)
return False
async def retry_stale_jobs(stale_minutes: int = 10) -> int:
"""Re-queue jobs that have been running for too long (likely crashed worker)."""
cutoff = datetime.utcnow() - timedelta(minutes=stale_minutes)
count = 0
async for session in get_session():
stmt = (
update(IngestJob)
.where(
IngestJob.status == JobStatus.RUNNING,
IngestJob.started_at < cutoff,
)
.values(status=JobStatus.QUEUED, started_at=None)
)
result = await session.execute(stmt)
count = result.rowcount
await session.commit()
if count:
logger.info("Re-queued %d stale jobs", count)
return count
async def worker_loop(
poll_interval: float = 2.0,
concurrency: int = 4,
) -> None:
"""Main worker loop: poll → process → repeat."""
logger.info("Worker started (concurrency=%d, poll_interval=%.1fs)", concurrency, poll_interval)
semaphore = asyncio.Semaphore(concurrency)
tasks: set[asyncio.Task] = set()
while not _shutdown:
try:
jobs = await poll_jobs(limit=concurrency)
for job_info in jobs:
async def _process(jid: uuid.UUID, did: uuid.UUID) -> None:
async with semaphore:
await process_job(jid, did)
task = asyncio.create_task(
_process(job_info["job_id"], job_info["document_id"])
)
tasks.add(task)
task.add_done_callback(tasks.discard)
if not jobs:
# Periodic stale job check
await retry_stale_jobs()
await asyncio.sleep(poll_interval)
else:
# Brief pause before next poll
await asyncio.sleep(0.5)
except Exception as e:
logger.exception("Worker loop error: %s", e)
await asyncio.sleep(poll_interval)
# Wait for in-flight tasks
if tasks:
logger.info("Waiting for %d in-flight tasks...", len(tasks))
await asyncio.gather(*tasks, return_exceptions=True)
logger.info("Worker stopped")
async def worker_health() -> dict:
"""Simple health check for the worker."""
return {"status": "ok", "service": "minikb-worker"}
def main() -> None:
"""Entry point for the worker process."""
settings = get_settings()
logging.basicConfig(
level=settings.log_level.upper(),
format="%(asctime)s %(levelname)s %(name)s | %(message)s",
)
# Register signal handlers
signal.signal(signal.SIGINT, _signal_handler)
signal.signal(signal.SIGTERM, _signal_handler)
concurrency = int(getattr(settings, "ingest_concurrency", 4))
try:
asyncio.run(worker_loop(concurrency=concurrency))
except KeyboardInterrupt:
pass
finally:
asyncio.run(close_db())
if __name__ == "__main__":
main()