-
Notifications
You must be signed in to change notification settings - Fork 28
feat: add max_retry_backoff option to job runner #53
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
Open
sirewix
wants to merge
1
commit into
Diggsey:master
Choose a base branch
from
famedly:sirewix/add-max-backoff
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| -- Main entry-point for job runner: pulls a batch of messages from the queue. | ||
| CREATE OR REPLACE FUNCTION mq_poll(channel_names TEXT[], batch_size INT DEFAULT 1) | ||
| RETURNS TABLE( | ||
| id UUID, | ||
| is_committed BOOLEAN, | ||
| name TEXT, | ||
| payload_json TEXT, | ||
| payload_bytes BYTEA, | ||
| retry_backoff INTERVAL, | ||
| wait_time INTERVAL | ||
| ) AS $$ | ||
| BEGIN | ||
| RETURN QUERY UPDATE mq_msgs | ||
| SET | ||
| attempt_at = CASE WHEN mq_msgs.attempts = 1 THEN NULL ELSE NOW() + mq_msgs.retry_backoff END, | ||
| attempts = mq_msgs.attempts - 1, | ||
| retry_backoff = mq_msgs.retry_backoff * 2 | ||
| FROM ( | ||
| SELECT | ||
| msgs.id | ||
| FROM mq_active_channels(channel_names, batch_size) AS active_channels | ||
| INNER JOIN LATERAL ( | ||
| SELECT mq_msgs.id FROM mq_msgs | ||
| WHERE mq_msgs.id != uuid_nil() | ||
| AND mq_msgs.attempt_at <= NOW() | ||
| AND mq_msgs.channel_name = active_channels.name | ||
| AND mq_msgs.channel_args = active_channels.args | ||
| AND NOT mq_uuid_exists(mq_msgs.after_message_id) | ||
| ORDER BY mq_msgs.attempt_at ASC | ||
| LIMIT batch_size | ||
| ) AS msgs ON TRUE | ||
| LIMIT batch_size | ||
| ) AS messages_to_update | ||
| LEFT JOIN mq_payloads ON mq_payloads.id = messages_to_update.id | ||
| WHERE mq_msgs.id = messages_to_update.id | ||
| AND mq_msgs.attempt_at <= NOW() | ||
| RETURNING | ||
| mq_msgs.id, | ||
| mq_msgs.commit_interval IS NULL, | ||
| mq_payloads.name, | ||
| mq_payloads.payload_json::TEXT, | ||
| mq_payloads.payload_bytes, | ||
| mq_msgs.retry_backoff / 2, | ||
| interval '0' AS wait_time; | ||
|
|
||
| IF NOT FOUND THEN | ||
| RETURN QUERY SELECT | ||
| NULL::UUID, | ||
| NULL::BOOLEAN, | ||
| NULL::TEXT, | ||
| NULL::TEXT, | ||
| NULL::BYTEA, | ||
| NULL::INTERVAL, | ||
| MIN(mq_msgs.attempt_at) - NOW() | ||
| FROM mq_msgs | ||
| WHERE mq_msgs.id != uuid_nil() | ||
| AND NOT mq_uuid_exists(mq_msgs.after_message_id) | ||
| AND (channel_names IS NULL OR mq_msgs.channel_name = ANY(channel_names)); | ||
| END IF; | ||
| END; | ||
| $$ LANGUAGE plpgsql; |
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,61 @@ | ||
| -- Main entry-point for job runner: pulls a batch of messages from the queue. | ||
| CREATE OR REPLACE FUNCTION mq_poll(channel_names TEXT[], batch_size INT DEFAULT 1, max_retry_text INTERVAL DEFAULT NULL) | ||
| RETURNS TABLE( | ||
| id UUID, | ||
| is_committed BOOLEAN, | ||
| name TEXT, | ||
| payload_json TEXT, | ||
| payload_bytes BYTEA, | ||
| retry_backoff INTERVAL, | ||
| wait_time INTERVAL | ||
| ) AS $$ | ||
| BEGIN | ||
| RETURN QUERY UPDATE mq_msgs | ||
| SET | ||
| attempt_at = CASE WHEN mq_msgs.attempts = 1 THEN NULL ELSE NOW() + mq_msgs.retry_backoff END, | ||
| attempts = mq_msgs.attempts - 1, | ||
| retry_backoff = CASE WHEN max_retry_text IS NULL THEN mq_msgs.retry_backoff * 2 ELSE LEAST(mq_msgs.retry_backoff * 2, max_retry_text) END | ||
| FROM ( | ||
| SELECT | ||
| msgs.id | ||
| FROM mq_active_channels(channel_names, batch_size) AS active_channels | ||
| INNER JOIN LATERAL ( | ||
| SELECT mq_msgs.id FROM mq_msgs | ||
| WHERE mq_msgs.id != uuid_nil() | ||
| AND mq_msgs.attempt_at <= NOW() | ||
| AND mq_msgs.channel_name = active_channels.name | ||
| AND mq_msgs.channel_args = active_channels.args | ||
| AND NOT mq_uuid_exists(mq_msgs.after_message_id) | ||
| ORDER BY mq_msgs.attempt_at ASC | ||
| LIMIT batch_size | ||
| ) AS msgs ON TRUE | ||
| LIMIT batch_size | ||
| ) AS messages_to_update | ||
| LEFT JOIN mq_payloads ON mq_payloads.id = messages_to_update.id | ||
| WHERE mq_msgs.id = messages_to_update.id | ||
| AND mq_msgs.attempt_at <= NOW() | ||
| RETURNING | ||
| mq_msgs.id, | ||
| mq_msgs.commit_interval IS NULL, | ||
| mq_payloads.name, | ||
| mq_payloads.payload_json::TEXT, | ||
| mq_payloads.payload_bytes, | ||
| mq_msgs.retry_backoff / 2, | ||
| interval '0' AS wait_time; | ||
|
|
||
| IF NOT FOUND THEN | ||
| RETURN QUERY SELECT | ||
| NULL::UUID, | ||
| NULL::BOOLEAN, | ||
| NULL::TEXT, | ||
| NULL::TEXT, | ||
| NULL::BYTEA, | ||
| NULL::INTERVAL, | ||
| MIN(mq_msgs.attempt_at) - NOW() | ||
| FROM mq_msgs | ||
| WHERE mq_msgs.id != uuid_nil() | ||
| AND NOT mq_uuid_exists(mq_msgs.after_message_id) | ||
| AND (channel_names IS NULL OR mq_msgs.channel_name = ANY(channel_names)); | ||
| END IF; | ||
| END; | ||
| $$ LANGUAGE plpgsql; | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this condition is redundant, it can just be:
I think this should also be called
max_retry_intervalrather thanmax_retry_text