Skip to content

Speakr is a personal, self-hosted web application designed for transcribing audio recordings

Python

Add periodic trial-fire path so auto-paused webhooks resume on a successful delivery (WEBHOOK_TRIAL_INTERVAL_SECONDS, default 3600); the dispatcher attempts one trial per paused webhook per interval, with later queued deliveries pushed forward so the receiver isn't flooded while in recovery

Murtaza Nasir399c83ff4590
diff --git a/src/services/webhook_dispatch.py b/src/services/webhook_dispatch.py
index 10cdfe9b..793d4782 100644
--- a/src/services/webhook_dispatch.py
+++ b/src/services/webhook_dispatch.py
@@ -74,6 +74,20 @@ def _autopause_failures() -> int:
return 10
+def _trial_interval_seconds() -> int:
+ """How often an auto-paused webhook gets a trial-fire delivery.
+
+ Defaults to one hour: long enough that a flapping endpoint doesn't
+ flood the receiver, short enough that a recovered endpoint resumes
+ within a working day. Set 0 to disable trial-fires entirely (the
+ webhook then stays paused until the user manually re-enables).
+ """
+ try:
+ return max(0, int(os.environ.get('WEBHOOK_TRIAL_INTERVAL_SECONDS', '3600')))
+ except (TypeError, ValueError):
+ return 3600
+
+
def _dispatcher_interval() -> int:
try:
return max(1, int(os.environ.get('WEBHOOK_DISPATCHER_INTERVAL_SECONDS', '5')))
@@ -376,14 +390,32 @@ def run_dispatcher_pass(app=None, limit: int = 100) -> dict:
attempted = 0
outcomes = {'success': 0, 'failed': 0, 'permanent_failure': 0}
autopause_thresh = _autopause_failures()
+ trial_interval_s = _trial_interval_seconds()
+ # Track which auto-paused webhooks have already been trialed
+ # this pass so we attempt at most one delivery per webhook per
+ # interval (the rest of their queue waits for the trial result).
+ trialed_this_pass = set()
for delivery in due:
webhook = delivery.webhook
- if webhook is None or not webhook.enabled:
- # Parent disabled while we were queued. Mark permanent so
- # we stop re-evaluating this row.
+ if webhook is None:
delivery.status = 'permanent_failure'
delivery.next_retry_at = None
continue
+ if not webhook.enabled:
+ if not webhook.auto_paused:
+ # User explicitly disabled; abandon queued rows.
+ delivery.status = 'permanent_failure'
+ delivery.next_retry_at = None
+ continue
+ # Auto-paused webhook. Try at most ONE delivery per
+ # pass as a trial. Subsequent queued deliveries for the
+ # same webhook are pushed past the trial interval so
+ # we recheck them after the next trial window.
+ if webhook.id in trialed_this_pass:
+ delivery.next_retry_at = now + timedelta(seconds=trial_interval_s)
+ delivery.status = 'failed'
+ continue
+ trialed_this_pass.add(webhook.id)
status_code, body_preview, error = _post_delivery(delivery, webhook)
outcome = _apply_attempt_result(delivery, status_code, body_preview, error)
attempted += 1
@@ -392,16 +424,30 @@ def run_dispatcher_pass(app=None, limit: int = 100) -> dict:
# Update the parent webhook's health counters.
webhook.last_delivery_at = datetime.utcnow()
if outcome == 'success':
+ # Successful delivery resets failure counter AND
+ # unpauses an auto-paused webhook so its remaining
+ # queue can flush on subsequent passes.
webhook.consecutive_failures = 0
- webhook.auto_paused = False
+ if webhook.auto_paused:
+ webhook.enabled = True
+ webhook.auto_paused = False
else:
if outcome == 'permanent_failure':
webhook.consecutive_failures = (webhook.consecutive_failures or 0) + 1
- if webhook.consecutive_failures >= autopause_thresh:
+ if webhook.consecutive_failures >= autopause_thresh and not webhook.auto_paused:
+ # First time crossing the threshold: pause and
+ # schedule the next trial 1h out (configurable
+ # via WEBHOOK_TRIAL_INTERVAL_SECONDS).
webhook.enabled = False
webhook.auto_paused = True
-
- if attempted:
+ delivery.status = 'failed'
+ delivery.next_retry_at = now + timedelta(seconds=trial_interval_s)
+ elif webhook.auto_paused:
+ # Failed trial of an already-paused webhook: push
+ # next trial forward by the interval.
+ delivery.next_retry_at = now + timedelta(seconds=trial_interval_s)
+
+ if attempted or trialed_this_pass:
db.session.commit()
return {'attempted': attempted, **outcomes}
diff --git a/tests/test_webhooks.py b/tests/test_webhooks.py
index cc8dc155..136a34e9 100644
--- a/tests/test_webhooks.py
+++ b/tests/test_webhooks.py
@@ -638,6 +638,59 @@ def test_is_url_safe_unanchored_allowlist_no_longer_skips_ip_check():
assert 'private/loopback' in reason
+def test_autopaused_webhook_resumes_on_successful_trial_delivery():
+ """Regression: previously auto-pause was terminal — once a webhook
+ crossed the failure threshold the only recovery was manual
+ re-enable in the UI. Now the dispatcher fires one trial delivery
+ per WEBHOOK_TRIAL_INTERVAL_SECONDS (default 1h); a success unpauses
+ the webhook and the rest of its queue flushes on subsequent passes."""
+ from unittest.mock import patch, MagicMock
+ with app.app_context():
+ user = _make_user('autopause_resume')
+ wh = Webhook(
+ user_id=user.id, name='paused', url='https://example.com/w', enabled=True,
+ )
+ wh.event_list = ['recording.created']
+ # Simulate an already auto-paused webhook with a queued delivery
+ # whose retry has come due (trial moment).
+ wh.enabled = False
+ wh.auto_paused = True
+ wh.consecutive_failures = 10
+ db.session.add(wh)
+ db.session.commit()
+
+ # Seed a pending delivery due now (the "trial").
+ envelope = '{"id":"x","type":"recording.created","timestamp":"now","user_id":0,"data":{}}'
+ d = WebhookDelivery(
+ webhook_id=wh.id,
+ event_id='trial-1',
+ event_type='recording.created',
+ payload=envelope,
+ status='pending',
+ next_retry_at=datetime.utcnow() - timedelta(seconds=1),
+ )
+ db.session.add(d)
+ db.session.commit()
+ d_id = d.id
+
+ # Mock the outbound POST to succeed.
+ with patch('src.services.webhook_dispatch.requests.post') as mock_post:
+ mock_post.return_value = MagicMock(status_code=204, text='')
+ run_dispatcher_pass()
+
+ db.session.expire_all()
+ wh_after = db.session.get(Webhook, wh.id)
+ assert wh_after.enabled is True, 'Successful trial should re-enable webhook'
+ assert wh_after.auto_paused is False, 'Successful trial should clear auto_paused'
+ assert wh_after.consecutive_failures == 0
+ assert db.session.get(WebhookDelivery, d_id).status == 'success'
+
+ WebhookDelivery.query.filter_by(webhook_id=wh.id).delete()
+ db.session.delete(wh_after)
+ db.session.delete(user)
+ db.session.commit()
+
+
def test_is_url_safe_inspects_all_resolved_addresses():
"""Sanity: even when the hostname is fine, every resolved address
must be inspected. Tested by mocking getaddrinfo to return mixed