| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- """Durable booking results, retried by workers and the sweeper after restart.
- The directory must be persistent. Docker services share it through /app/data.
- A Redis marker also protects results when the sweeper runs on another host.
- """
- import json
- import os
- import sqlite3
- import time
- from contextlib import closing
- from pathlib import Path
- class BookingOutbox:
- def __init__(self, path=None):
- self.path = path or os.getenv('BOOKING_OUTBOX_PATH') or str(
- Path(__file__).resolve().parent.parent / 'data' / 'booking_outbox.sqlite3')
- def _connect(self):
- Path(self.path).parent.mkdir(parents=True, exist_ok=True)
- db = sqlite3.connect(self.path, timeout=10)
- db.execute('''CREATE TABLE IF NOT EXISTS booking_results (
- task_id TEXT PRIMARY KEY, tracker TEXT NOT NULL, payload TEXT NOT NULL,
- lease_until REAL NOT NULL DEFAULT 0, cloud_synced INTEGER NOT NULL DEFAULT 0)''')
- return db
- @staticmethod
- def marker(task_id):
- return f'vs:booking:success:{task_id}'
- def put(self, task_id, tracker, payload):
- with closing(self._connect()) as db, db:
- db.execute('BEGIN IMMEDIATE')
- existing = db.execute('SELECT payload FROM booking_results WHERE task_id=?', (str(task_id),)).fetchone()
- if existing and payload.get('status') == 'grabbed' and json.loads(existing[0]).get('status') != 'grabbed':
- db.execute('UPDATE booking_results SET tracker=?,payload=?,cloud_synced=0,lease_until=0 WHERE task_id=?',
- (tracker, json.dumps(payload), str(task_id)))
- return
- db.execute('INSERT INTO booking_results(task_id, tracker, payload) VALUES (?, ?, ?) '
- 'ON CONFLICT(task_id) DO NOTHING',
- (str(task_id), tracker, json.dumps(payload)))
- def get(self, task_id):
- with closing(self._connect()) as db:
- row = db.execute('SELECT payload FROM booking_results WHERE task_id=?',
- (str(task_id),)).fetchone()
- return json.loads(row[0]) if row else None
- def flush(self, cloud, redis_client, logger, tracker=None):
- with closing(self._connect()) as db:
- rows = db.execute('SELECT task_id, tracker, payload, cloud_synced FROM booking_results '
- 'WHERE lease_until < ?' + (' AND tracker=?' if tracker else '') +
- ' ORDER BY lease_until LIMIT 20',
- (time.time(), tracker) if tracker else (time.time(),)).fetchall()
- for task_id, tracker_key, raw, cloud_synced in rows:
- # Separate processes sharing data must not send the same result concurrently.
- with closing(self._connect()) as db, db:
- claimed = db.execute('UPDATE booking_results SET lease_until=? '
- 'WHERE task_id=? AND lease_until < ?',
- (time.time() + 60, task_id, time.time())).rowcount
- if claimed:
- cloud_synced = db.execute('SELECT cloud_synced FROM booking_results WHERE task_id=?',
- (task_id,)).fetchone()[0]
- if not claimed:
- continue
- try:
- redis_client.set(self.marker(task_id), raw)
- if not cloud_synced:
- cloud.update_vas_task(task_id, json.loads(raw))
- with closing(self._connect()) as db, db:
- db.execute('UPDATE booking_results SET cloud_synced=1 WHERE task_id=?', (task_id,))
- payload = json.loads(raw)
- if payload.get('status') == 'grabbed':
- from core_types import BookResult
- from toolkit.notifier import notify_book_result
- info = payload.get('grabbed_history', {})
- receipt = BookResult(success=True, account=info.get('account', ''),
- session_id=info.get('session_id', ''), urn=info.get('urn', ''),
- book_date=info.get('slot_date', ''), book_time=info.get('slot_time', ''),
- payment_link=info.get('payment_link', ''))
- if not notify_book_result(receipt, order_id=info.get('order_id'), logger=logger,
- event_id='booking:order:' + str(task_id)):
- raise RuntimeError('Booking notification could not be queued')
- # Keep the journal if Redis is unavailable; cleanup is retryable too.
- redis_client.zrem(tracker_key, task_id)
- redis_client.delete(self.marker(task_id))
- with closing(self._connect()) as db, db:
- db.execute('DELETE FROM booking_results WHERE task_id=?', (task_id,))
- except Exception as exc:
- logger(f'Booking result sync pending for task={task_id}: {exc}')
- with closing(self._connect()) as db, db:
- db.execute('UPDATE booking_results SET lease_until=? WHERE task_id=?',
- (time.time() + 5, task_id))
|