update
This commit is contained in:
@@ -21,7 +21,7 @@ logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class BidQueue:
|
||||
def __init__(self, jobs=review_jobs, action=bid_step, turn=model_turn, rule=None):
|
||||
def __init__(self, jobs=review_jobs, action=bid_step, turn=model_turn, rule=None, worker_count=None):
|
||||
self.jobs, self.action, self.turn = jobs, action, turn
|
||||
self.rule = rule or (lambda: RULE_VERSION + ':' + routing_revision())
|
||||
self.stop = threading.Event()
|
||||
@@ -29,6 +29,11 @@ class BidQueue:
|
||||
self.lock = threading.Lock()
|
||||
self.worker = None
|
||||
self.reconcile_worker = None
|
||||
if worker_count is None:
|
||||
from app.core.config import get_settings
|
||||
worker_count = get_settings().work_assistant_bid_workers
|
||||
self.worker_count = max(1, min(16, worker_count))
|
||||
self.workers = []
|
||||
|
||||
def connect(self):
|
||||
db = self.jobs.connect()
|
||||
@@ -129,6 +134,23 @@ class BidQueue:
|
||||
db.execute("INSERT OR IGNORE INTO review_job(job_key,status,updated_at) VALUES(?,'queued',?)", (key, now))
|
||||
row = db.execute('SELECT j.*, q.created_at AS queued_at FROM review_job j JOIN bid_queue q USING(job_key) WHERE j.job_key=?', (key,)).fetchone()
|
||||
control = db.execute('SELECT paused FROM bid_control WHERE task_id=?', (task_id,)).fetchone()
|
||||
# One-time migration of pre-existing section-recovery failures.
|
||||
# Keep the immutable request key and saved units; never reopen a
|
||||
# paused task, a live lease, or unrelated validation failures.
|
||||
if (request.workbench.get('version') == 2 and request.workbench.get('phase') == 'writing'
|
||||
and row['status'] == 'suspended' and row['reason'] == 'validation_blocked'
|
||||
and not (control and control['paused']) and (row['lease_until'] or 0) <= now):
|
||||
checkpoint = json.loads(row['checkpoint']) if row['checkpoint'] else {}
|
||||
recovery = checkpoint.get('studio_writing_recovery', {})
|
||||
exhausted = (any(count >= 2 for count in recovery.get('failures', {}).values())
|
||||
or recovery.get('expansion_round', 0) >= 3)
|
||||
if exhausted and not checkpoint.get('studio_partial_recovery_v1'):
|
||||
checkpoint['studio_partial_recovery_v1'] = True
|
||||
db.execute("UPDATE review_job SET status='queued',reason='review_continuation',retry_at=0,"
|
||||
"checkpoint=?,updated_at=? WHERE job_key=?",
|
||||
(json.dumps(checkpoint, ensure_ascii=False), now, key))
|
||||
row = db.execute('SELECT j.*,q.created_at AS queued_at FROM review_job j '
|
||||
'JOIN bid_queue q USING(job_key) WHERE j.job_key=?', (key,)).fetchone()
|
||||
service = db.execute('SELECT * FROM review_service WHERE id=1').fetchone()
|
||||
db.commit()
|
||||
finally:
|
||||
@@ -140,7 +162,7 @@ class BidQueue:
|
||||
if row['status'] == 'ready' and not paused:
|
||||
return json.loads(row['result'])
|
||||
if row['status'] == 'suspended' and not paused:
|
||||
if request.workbench.get('version') == 1:
|
||||
if request.workbench.get('version') in {1, 2}:
|
||||
checkpoint = json.loads(row['checkpoint']) if row['checkpoint'] else {}
|
||||
answer = {'rule_version': RULE_VERSION, 'job_status': 'blocked',
|
||||
'message': checkpoint.get('workbench_failure', '本步骤尚未通过内容校验,请核对要求后重试。')[:1500]}
|
||||
@@ -193,7 +215,7 @@ class BidQueue:
|
||||
row = db.execute("SELECT q.*,COALESCE(c.revision,0) AS control_revision FROM bid_queue q JOIN review_job j ON j.job_key=q.job_key "
|
||||
"LEFT JOIN bid_control c ON c.task_id=q.task_id "
|
||||
"WHERE q.rule=? AND j.status NOT IN ('ready','suspended') "
|
||||
"AND (?=0 OR json_extract(q.payload,'$.action') IN ('reconcile','analyze','outline') OR json_extract(q.payload,'$.workbench.version')=1) "
|
||||
"AND (?=0 OR json_extract(q.payload,'$.action') IN ('reconcile','analyze','outline') OR json_extract(q.payload,'$.workbench.version') IN (1,2)) "
|
||||
"AND COALESCE(c.paused,0)=0 "
|
||||
"AND COALESCE(j.lease_until,0)<=? "
|
||||
"AND (MIN(j.retry_at,COALESCE(j.updated_at,0)+?)<=? OR j.updated_at<?) "
|
||||
@@ -218,8 +240,8 @@ class BidQueue:
|
||||
with execution_lock(self.jobs.path, row['job_key']) as acquired:
|
||||
if not acquired:
|
||||
return False
|
||||
# Independent comparisons share the same two-call admission as
|
||||
# extraction. Drafting and other actions keep their reservation.
|
||||
# Independent steps share configured admission/token limits.
|
||||
# Worker count does not bypass model capacity or execution leases.
|
||||
payload = json.loads(row['payload'])
|
||||
turn = parallel_model_turn if self.turn is model_turn and (payload['action'] in {'extract', 'reconcile', 'analyze', 'outline'} or payload.get('workbench')) else self.turn
|
||||
with turn():
|
||||
@@ -248,15 +270,18 @@ class BidQueue:
|
||||
def start(self):
|
||||
with self.lock:
|
||||
self.stop.clear()
|
||||
if not self.worker or not self.worker.is_alive():
|
||||
self.worker = threading.Thread(target=self._loop, name='bid-model-queue', daemon=True)
|
||||
self.worker.start()
|
||||
# The second worker also accepts independent workspace jobs. PMS
|
||||
# enforces ordering between continuation segments of one chapter.
|
||||
if not self.reconcile_worker or not self.reconcile_worker.is_alive():
|
||||
self.reconcile_worker = threading.Thread(target=self._loop, kwargs={'reconcile_only': True},
|
||||
name='bid-reconcile-queue', daemon=True)
|
||||
self.reconcile_worker.start()
|
||||
for index in range(self.worker_count):
|
||||
if index < len(self.workers) and self.workers[index].is_alive():
|
||||
continue
|
||||
worker = threading.Thread(target=self._loop, kwargs={'reconcile_only': index > 0},
|
||||
name=f'bid-model-queue-{index + 1}', daemon=True)
|
||||
if index < len(self.workers):
|
||||
self.workers[index] = worker
|
||||
else:
|
||||
self.workers.append(worker)
|
||||
worker.start()
|
||||
self.worker = self.workers[0]
|
||||
self.reconcile_worker = self.workers[1] if len(self.workers) > 1 else None
|
||||
|
||||
def _loop(self, *, reconcile_only=False):
|
||||
while not self.stop.is_set():
|
||||
@@ -271,7 +296,7 @@ class BidQueue:
|
||||
def close(self):
|
||||
self.stop.set()
|
||||
self.wake.set()
|
||||
for worker in (self.worker, self.reconcile_worker):
|
||||
for worker in self.workers:
|
||||
if worker:
|
||||
worker.join(timeout=2)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user