"""Single bounded approval worker with its own database connection.""" import asyncio from concurrent.futures import ThreadPoolExecutor import time from .approval_store import ApprovalError, ApprovalStore class ApprovalWorker: def __init__(self,settings,keyring): self.settings,self.keyring,self.clock=settings,keyring,time.time self.executor=ThreadPoolExecutor(max_workers=1,thread_name_prefix='work-approval') self.slots=asyncio.Semaphore(8) self.store=None def _start(self): from .store import Store from .binding_state import BindingState self.connection=Store(self.settings.data_dir/'metadata.sqlite',self.settings.database_url) if self.settings.database_url: self.connection.db.execute("SET lock_timeout TO '500ms'") self.connection.db.execute("SET statement_timeout TO '800ms'") else:self.connection.db.execute('PRAGMA busy_timeout=500') self.store=ApprovalStore(BindingState(self.connection,self.settings.environment, self.settings.feishu_app_id),self.keyring,enforce_execution_state=True) async def start(self): await asyncio.get_running_loop().run_in_executor(self.executor,self._start) def _invoke(self,deadline,method,args): if time.monotonic()>deadline or self.store is None: raise ApprovalError('approval_temporarily_unavailable') try: # Postgres may reconnect after a lost connection. Reapply this # worker's limits before opening a new approval transaction. if self.settings.database_url: db=self.connection.db if db.conn.closed or db.conn.broken: db.execute("SET lock_timeout TO '500ms'") db.execute("SET statement_timeout TO '800ms'") return getattr(self.store,method)(*args) except ApprovalError:raise except Exception:raise ApprovalError('approval_temporarily_unavailable')from None async def call(self,method,*args): allowed={'create','status','consume','cancel','decide','card_material','claim_card', 'bind_card','card_send_unknown','close_unavailable','closed_card_receipt', 'mark_card_closed','cards_to_send','cards_to_close','maintain_retention'} if method not in allowed:raise ApprovalError('unsupported_approval_operation') try:await asyncio.wait_for(self.slots.acquire(),timeout=.05) except asyncio.TimeoutError:raise ApprovalError('approval_temporarily_unavailable')from None future=asyncio.get_running_loop().run_in_executor(self.executor,self._invoke, time.monotonic()+1.2,method,args) # Keep the queue slot until the worker really stops, even when a caller # times out. A timeout is not proof that a database commit did not occur. def settled(completed): self.slots.release() if not completed.cancelled():completed.exception() future.add_done_callback(settled) try:return await asyncio.wait_for(asyncio.shield(future),timeout=1.5) except asyncio.TimeoutError:raise ApprovalError('approval_result_unconfirmed')from None async def close(self): def finish(): if self.store is not None:self.connection.close() self.store=None try:await asyncio.get_running_loop().run_in_executor(self.executor,finish) finally:self.executor.shutdown(wait=False,cancel_futures=True)