booking_outbox.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. """Durable booking results, retried by workers and the sweeper after restart.
  2. The directory must be persistent. Docker services share it through /app/data.
  3. A Redis marker also protects results when the sweeper runs on another host.
  4. """
  5. import json
  6. import os
  7. import sqlite3
  8. import time
  9. from contextlib import closing
  10. from pathlib import Path
  11. class BookingOutbox:
  12. def __init__(self, path=None):
  13. self.path = path or os.getenv('BOOKING_OUTBOX_PATH') or str(
  14. Path(__file__).resolve().parent.parent / 'data' / 'booking_outbox.sqlite3')
  15. def _connect(self):
  16. Path(self.path).parent.mkdir(parents=True, exist_ok=True)
  17. db = sqlite3.connect(self.path, timeout=10)
  18. db.execute('''CREATE TABLE IF NOT EXISTS booking_results (
  19. task_id TEXT PRIMARY KEY, tracker TEXT NOT NULL, payload TEXT NOT NULL,
  20. lease_until REAL NOT NULL DEFAULT 0, cloud_synced INTEGER NOT NULL DEFAULT 0)''')
  21. return db
  22. @staticmethod
  23. def marker(task_id):
  24. return f'vs:booking:success:{task_id}'
  25. def put(self, task_id, tracker, payload):
  26. with closing(self._connect()) as db, db:
  27. db.execute('BEGIN IMMEDIATE')
  28. existing = db.execute('SELECT payload FROM booking_results WHERE task_id=?', (str(task_id),)).fetchone()
  29. if existing and payload.get('status') == 'grabbed' and json.loads(existing[0]).get('status') != 'grabbed':
  30. db.execute('UPDATE booking_results SET tracker=?,payload=?,cloud_synced=0,lease_until=0 WHERE task_id=?',
  31. (tracker, json.dumps(payload), str(task_id)))
  32. return
  33. db.execute('INSERT INTO booking_results(task_id, tracker, payload) VALUES (?, ?, ?) '
  34. 'ON CONFLICT(task_id) DO NOTHING',
  35. (str(task_id), tracker, json.dumps(payload)))
  36. def get(self, task_id):
  37. with closing(self._connect()) as db:
  38. row = db.execute('SELECT payload FROM booking_results WHERE task_id=?',
  39. (str(task_id),)).fetchone()
  40. return json.loads(row[0]) if row else None
  41. def flush(self, cloud, redis_client, logger, tracker=None):
  42. with closing(self._connect()) as db:
  43. rows = db.execute('SELECT task_id, tracker, payload, cloud_synced FROM booking_results '
  44. 'WHERE lease_until < ?' + (' AND tracker=?' if tracker else '') +
  45. ' ORDER BY lease_until LIMIT 20',
  46. (time.time(), tracker) if tracker else (time.time(),)).fetchall()
  47. for task_id, tracker_key, raw, cloud_synced in rows:
  48. # Separate processes sharing data must not send the same result concurrently.
  49. with closing(self._connect()) as db, db:
  50. claimed = db.execute('UPDATE booking_results SET lease_until=? '
  51. 'WHERE task_id=? AND lease_until < ?',
  52. (time.time() + 60, task_id, time.time())).rowcount
  53. if claimed:
  54. cloud_synced = db.execute('SELECT cloud_synced FROM booking_results WHERE task_id=?',
  55. (task_id,)).fetchone()[0]
  56. if not claimed:
  57. continue
  58. try:
  59. redis_client.set(self.marker(task_id), raw)
  60. if not cloud_synced:
  61. cloud.update_vas_task(task_id, json.loads(raw))
  62. with closing(self._connect()) as db, db:
  63. db.execute('UPDATE booking_results SET cloud_synced=1 WHERE task_id=?', (task_id,))
  64. payload = json.loads(raw)
  65. if payload.get('status') == 'grabbed':
  66. from core_types import BookResult
  67. from toolkit.notifier import notify_book_result
  68. info = payload.get('grabbed_history', {})
  69. receipt = BookResult(success=True, account=info.get('account', ''),
  70. session_id=info.get('session_id', ''), urn=info.get('urn', ''),
  71. book_date=info.get('slot_date', ''), book_time=info.get('slot_time', ''),
  72. payment_link=info.get('payment_link', ''))
  73. if not notify_book_result(receipt, order_id=info.get('order_id'), logger=logger,
  74. event_id='booking:order:' + str(task_id)):
  75. raise RuntimeError('Booking notification could not be queued')
  76. # Keep the journal if Redis is unavailable; cleanup is retryable too.
  77. redis_client.zrem(tracker_key, task_id)
  78. redis_client.delete(self.marker(task_id))
  79. with closing(self._connect()) as db, db:
  80. db.execute('DELETE FROM booking_results WHERE task_id=?', (task_id,))
  81. except Exception as exc:
  82. logger(f'Booking result sync pending for task={task_id}: {exc}')
  83. with closing(self._connect()) as db, db:
  84. db.execute('UPDATE booking_results SET lease_until=? WHERE task_id=?',
  85. (time.time() + 5, task_id))