-
-
Notifications
You must be signed in to change notification settings - Fork 134
fix(queue): enforce max 5 retries and make queues healthy #2765
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
supabase/migrations/20260726134739_queue_health_retry_budget_5.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| -- Align all queue retry budgets to 5, raise org-stats drain rate, and purge | ||
| -- already-stuck messages (read_ct > 5) so /queue_health can return healthy. | ||
|
|
||
| -- 1) org_stats_queue was batch 10 / every 5 minutes (~100 msgs/5m) and could not | ||
| -- keep up. Run every process_all_cron_tasks tick with a larger batch. | ||
| UPDATE public.cron_tasks | ||
| SET | ||
| batch_size = 100, | ||
| second_interval = 10, | ||
| minute_interval = NULL, | ||
| hour_interval = NULL, | ||
| run_at_hour = NULL, | ||
| updated_at = now() | ||
| WHERE name = 'org_stats_queue'; | ||
|
|
||
| -- 2) Webhook delivery application retries must also obey the global max of 5. | ||
| ALTER TABLE public.webhook_deliveries | ||
| ALTER COLUMN max_attempts SET DEFAULT 5; | ||
|
|
||
| UPDATE public.webhook_deliveries | ||
| SET max_attempts = 5 | ||
| WHERE max_attempts > 5; | ||
|
|
||
| -- 3) Immediate purge of poison messages that already exceeded the hard retry | ||
| -- budget. Bounded globally (batches + wall-clock) like cleanup_queue_messages. | ||
| DO $$ | ||
| DECLARE | ||
| queue_name text; | ||
| deleted_batch integer; | ||
| deleted_total bigint := 0; | ||
| batch_size integer := 5000; | ||
| max_batches_total integer := 40; | ||
| batches_used integer := 0; | ||
| max_runtime_ms integer := 30000; | ||
| started_at timestamptz := pg_catalog.clock_timestamp(); | ||
| BEGIN | ||
| IF pg_catalog.to_regclass('pgmq.meta') IS NULL THEN | ||
| RAISE NOTICE 'queue_health_retry_budget_5: pgmq.meta missing, skip stuck purge'; | ||
| RETURN; | ||
| END IF; | ||
|
|
||
| PERFORM pg_catalog.set_config('statement_timeout', '0', true); | ||
|
|
||
| FOR queue_name IN | ||
| SELECT q.queue_name | ||
| FROM pgmq.list_queues() q | ||
| ORDER BY q.queue_name | ||
| LOOP | ||
| EXIT WHEN batches_used >= max_batches_total; | ||
| EXIT WHEN (EXTRACT(EPOCH FROM (pg_catalog.clock_timestamp() - started_at)) * 1000) >= max_runtime_ms; | ||
|
|
||
| IF pg_catalog.to_regclass(pg_catalog.format('pgmq.q_%I', queue_name)) IS NULL THEN | ||
| CONTINUE; | ||
| END IF; | ||
|
|
||
| LOOP | ||
| EXIT WHEN batches_used >= max_batches_total; | ||
| EXIT WHEN (EXTRACT(EPOCH FROM (pg_catalog.clock_timestamp() - started_at)) * 1000) >= max_runtime_ms; | ||
|
|
||
| EXECUTE pg_catalog.format( | ||
| 'DELETE FROM pgmq.q_%I | ||
| WHERE ctid IN ( | ||
| SELECT ctid | ||
| FROM pgmq.q_%I | ||
| WHERE read_ct > 5 | ||
| LIMIT $1 | ||
| )', | ||
| queue_name, | ||
| queue_name | ||
| ) | ||
| USING batch_size; | ||
|
|
||
| GET DIAGNOSTICS deleted_batch = ROW_COUNT; | ||
| EXIT WHEN deleted_batch = 0; | ||
|
|
||
| batches_used := batches_used + 1; | ||
| deleted_total := deleted_total + deleted_batch; | ||
| END LOOP; | ||
| END LOOP; | ||
|
|
||
| RAISE NOTICE | ||
| 'queue_health_retry_budget_5: deleted_stuck_read_ct=% batches_used=%/% runtime_ms=%', | ||
| deleted_total, | ||
| batches_used, | ||
| max_batches_total, | ||
| (EXTRACT(EPOCH FROM (pg_catalog.clock_timestamp() - started_at)) * 1000)::bigint; | ||
| END; | ||
| $$; | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.