feat(work): add signed task progress and durable status cards
This commit is contained in:
@@ -17,12 +17,20 @@ class ResultRequest(TaskRequest):
|
||||
result: str = Field(max_length=16000, repr=False)
|
||||
|
||||
|
||||
class StateRequest(TaskRequest):
|
||||
execution_state: str = Field(pattern=r'^(queued|running|waiting_for_approval|completed|failed|cancelled|interrupted|unknown)$')
|
||||
status_sequence: int = Field(ge=1, le=2147483647, strict=True)
|
||||
|
||||
|
||||
class TaskDelivery:
|
||||
def __init__(self, auth, state, broker, platform):
|
||||
self.auth,self.state,self.broker,self.platform=auth,state,broker,platform
|
||||
self.db=state.db
|
||||
self.keyring=auth.broker.store.keyring
|
||||
with self.db:
|
||||
self.db.execute('''CREATE TABLE IF NOT EXISTS work_execution_state_v3(
|
||||
task_id TEXT PRIMARY KEY,execution_state TEXT NOT NULL,
|
||||
status_sequence BIGINT NOT NULL,updated_at DOUBLE PRECISION NOT NULL)''')
|
||||
self.db.execute("""CREATE TABLE IF NOT EXISTS work_result_outbox_v2(
|
||||
task_id TEXT PRIMARY KEY,environment TEXT NOT NULL,app_id TEXT NOT NULL,
|
||||
owner TEXT NOT NULL,host_id TEXT NOT NULL,binding_generation BIGINT NOT NULL,
|
||||
@@ -30,7 +38,12 @@ class TaskDelivery:
|
||||
status TEXT NOT NULL,updated_at DOUBLE PRECISION NOT NULL)""")
|
||||
self.router=APIRouter()
|
||||
self.router.add_api_route('/work/v2/tasks/state',self.status,methods=['POST'])
|
||||
self.router.add_api_route('/work/v2/tasks/progress',self.progress,methods=['POST'])
|
||||
self.router.add_api_route('/work/v2/tasks/result',self.submit,methods=['POST'])
|
||||
self.cards=None
|
||||
if auth.s.work_task_cards_enabled:
|
||||
from .task_cards import TaskCards
|
||||
self.cards=TaskCards(state,platform,broker.worker_id)
|
||||
|
||||
def current(self, body):
|
||||
self.auth.require_binding(body.host_id,body.binding_owner,body.binding_generation)
|
||||
@@ -49,7 +62,10 @@ class TaskDelivery:
|
||||
def receipt(self,task):
|
||||
row=self.db.execute('SELECT status,digest FROM work_result_outbox_v2 WHERE task_id=?',
|
||||
(task['turn_id'],)).fetchone()
|
||||
execution=self.db.execute('SELECT execution_state,status_sequence FROM work_execution_state_v3 WHERE task_id=?',(task['turn_id'],)).fetchone()
|
||||
return {'task_id':task['turn_id'],'status':row['status'] if row else task['status'],
|
||||
'execution_state':execution['execution_state'] if execution else 'unknown',
|
||||
'status_sequence':execution['status_sequence'] if execution else 0,
|
||||
'result_received':bool(row),'result_digest':row['digest'] if row else None,
|
||||
'deadline':task['deadline']}
|
||||
|
||||
@@ -57,6 +73,28 @@ class TaskDelivery:
|
||||
await self.auth.authenticate(body,request)
|
||||
task=self.current(body)
|
||||
pending=self.broker.pending.get(body.task_id)
|
||||
if pending and pending.host==body.host_id:pending.progress.set()
|
||||
return no_cache(JSONResponse(self.receipt(task)))
|
||||
|
||||
async def progress(self,body:StateRequest,request:Request):
|
||||
await self.auth.authenticate(body,request)
|
||||
with self.db:
|
||||
self.state._owner_lock(body.binding_owner)
|
||||
task=self.current(body)
|
||||
if body.execution_state is not None:
|
||||
old=self.db.execute('SELECT * FROM work_execution_state_v3 WHERE task_id=?',(body.task_id,)).fetchone()
|
||||
terminal={'completed','failed','cancelled','interrupted'}
|
||||
if old and body.status_sequence==old['status_sequence'] and body.execution_state!=old['execution_state']:
|
||||
raise HTTPException(409,'execution_state_conflict')
|
||||
if not old:
|
||||
self.db.execute('INSERT INTO work_execution_state_v3 VALUES(?,?,?,?)',
|
||||
(body.task_id,body.execution_state,body.status_sequence,time.time()))
|
||||
elif body.status_sequence>old['status_sequence']:
|
||||
if old['execution_state'] in terminal and body.execution_state!=old['execution_state']:
|
||||
raise HTTPException(409,'execution_already_terminal')
|
||||
self.db.execute('UPDATE work_execution_state_v3 SET execution_state=?,status_sequence=?,updated_at=? WHERE task_id=?',
|
||||
(body.execution_state,body.status_sequence,time.time(),body.task_id))
|
||||
pending=self.broker.pending.get(body.task_id)
|
||||
if pending and pending.host==body.host_id:
|
||||
pending.progress.set()
|
||||
return no_cache(JSONResponse(self.receipt(task)))
|
||||
|
||||
Reference in New Issue
Block a user