Skip to content

Commit b63175a

Browse files
committed
Fix invalid webhook handling
1 parent 5033741 commit b63175a

File tree

6 files changed

+20
-11
lines changed

6 files changed

+20
-11
lines changed

packages/examples/cvat/exchange-oracle/src/crons/webhooks/_common.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ def handle_webhook(
1818
session: Session,
1919
webhook: Webhook,
2020
*,
21+
queue: webhook_service.OracleWebhookQueue,
2122
on_fail: Callable[[Session, Webhook, Exception], None] = lambda _s, _w, _e: None,
2223
):
2324
logger.debug(
@@ -39,9 +40,9 @@ def handle_webhook(
3940
savepoint.rollback()
4041
raise
4142
finally:
42-
webhook_service.outbox.handle_webhook_fail(session, webhook.id)
43+
queue.handle_webhook_fail(session, webhook.id)
4344
else:
44-
webhook_service.outbox.handle_webhook_success(session, webhook.id)
45+
queue.handle_webhook_success(session, webhook.id)
4546
logger.debug("Webhook handled successfully")
4647

4748

@@ -80,6 +81,6 @@ def process_outgoing_webhooks(
8081
for_update=ForUpdateParams(skip_locked=True),
8182
)
8283
for webhook in webhooks:
83-
with handle_webhook(logger, session, webhook):
84+
with handle_webhook(logger, session, webhook, queue=webhook_service.outbox):
8485
webhook_url = url_getter(webhook.chain_id, webhook.escrow_address)
8586
_send_webhook(webhook_url, webhook, with_timestamp=with_timestamp)

packages/examples/cvat/exchange-oracle/src/crons/webhooks/job_launcher.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ def process_incoming_job_launcher_webhooks(logger: logging.Logger, session: Sess
5353
)
5454

5555
for webhook in webhooks:
56-
with handle_webhook(logger, session, webhook, on_fail=handle_failure):
56+
with handle_webhook(
57+
logger, session, webhook, on_fail=handle_failure, queue=oracle_db_service.inbox
58+
):
5759
handle_job_launcher_event(webhook, db_session=session, logger=logger)
5860

5961

packages/examples/cvat/exchange-oracle/src/crons/webhooks/recording_oracle.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def process_incoming_recording_oracle_webhooks(logger: logging.Logger, session:
3535
)
3636

3737
for webhook in webhooks:
38-
with handle_webhook(logger, session, webhook):
38+
with handle_webhook(logger, session, webhook, queue=oracle_db_service.inbox):
3939
handle_recording_oracle_event(webhook, db_session=session, logger=logger)
4040

4141

packages/examples/cvat/exchange-oracle/src/crons/webhooks/reputation_oracle.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def process_incoming_reputation_oracle_webhooks(logger: logging.Logger, session:
2323
for_update=ForUpdateParams(skip_locked=True),
2424
)
2525
for webhook in webhooks:
26-
with handle_webhook(logger, session, webhook):
26+
with handle_webhook(logger, session, webhook, queue=oracle_db_service.inbox):
2727
match webhook.event_type:
2828
case ReputationOracleEventTypes.escrow_completed:
2929
projects = db_service.get_projects_by_escrow_address(

packages/examples/cvat/recording-oracle/src/crons/_utils.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,13 @@ def wrapper():
6969

7070

7171
@contextmanager
72-
def handle_webhook(logger: logging.Logger, session: Session, webhook: Webhook):
72+
def handle_webhook(
73+
logger: logging.Logger,
74+
session: Session,
75+
webhook: Webhook,
76+
*,
77+
queue: webhook_service.OracleWebhookQueue,
78+
):
7379
savepoint = session.begin_nested()
7480
logger.debug(
7581
"Processing webhook "
@@ -83,9 +89,9 @@ def handle_webhook(logger: logging.Logger, session: Session, webhook: Webhook):
8389
# TODO: should we rollback on any errors or just on database errors?
8490
savepoint.rollback()
8591
logger.exception(f"Webhook {webhook.id} sending failed: {e}")
86-
webhook_service.outbox.handle_webhook_fail(session, webhook.id)
92+
queue.handle_webhook_fail(session, webhook.id)
8793
else:
88-
webhook_service.outbox.handle_webhook_success(session, webhook.id)
94+
queue.handle_webhook_success(session, webhook.id)
8995
logger.debug("Webhook handled successfully")
9096

9197

@@ -124,6 +130,6 @@ def process_outgoing_webhooks(
124130
for_update=ForUpdateParams(skip_locked=True),
125131
)
126132
for webhook in webhooks:
127-
with handle_webhook(logger, session, webhook):
133+
with handle_webhook(logger, session, webhook, queue=oracle_db_service.outbox):
128134
webhook_url = url_getter(webhook.chain_id, webhook.escrow_address)
129135
send_webhook(webhook_url, webhook, with_timestamp=with_timestamp)

packages/examples/cvat/recording-oracle/src/crons/process_exchange_oracle_webhooks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def process_incoming_exchange_oracle_webhooks(logger: logging.Logger, session: S
2626
)
2727

2828
for webhook in webhooks:
29-
with handle_webhook(logger, session, webhook):
29+
with handle_webhook(logger, session, webhook, queue=oracle_db_service.inbox):
3030
handle_exchange_oracle_event(webhook, db_session=session)
3131

3232

0 commit comments

Comments
 (0)