Repository navigation
fix(queue): stop failing jobs from retrying forever #361
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
Show all changes
5 commits
Select commit
Hold shift + click to select a range
43c0be3
fix(queue): stop failing jobs from retrying forever
JohnRDOrazio 4a7dc35
fix(translations): fall back to WP-Cron when enqueue returns false
JohnRDOrazio b6af016
fix(queue): keep a retried job's record when its re-enqueue fails
JohnRDOrazio e465022
chore(queue): mark the enqueued hook's job id as unused
JohnRDOrazio 9c9097b
Merge remote-tracking branch 'origin/main' into fix/queue-retry-loop
JohnRDOrazio 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
68 changes: 68 additions & 0 deletions
68
wordpress/plugins/cdcf-redis-translations/includes/retry-cleanup.php
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,68 @@ | ||
| <?php | ||
| /** | ||
| * Removes the job record a redis-queue retry leaves behind. | ||
| * | ||
| * redis-queue retries a failed job by enqueueing it again as a new job | ||
| * with its own record, then sets the old record back to 'queued'. The | ||
| * old job is no longer in Redis, so nothing processes it and the record | ||
| * stays 'queued' on the dashboard for good. The new record carries the | ||
| * job on, so the old one can go. | ||
| * | ||
| * The retry fires `redis_queue_job_retried` whether or not the new | ||
| * enqueue succeeded, and does not pass its result. enqueue() fires | ||
| * `redis_queue_job_enqueued` with the same job object only on success, | ||
| * so the old record is deleted only when that happened for this job. | ||
| */ | ||
|
|
||
| defined('ABSPATH') || exit; | ||
|
|
||
| /** | ||
| * Runs on `redis_queue_job_enqueued`, and on `redis_queue_job_retried` | ||
| * to read and clear what was recorded. | ||
| * | ||
| * @param object|null $job The job just enqueued; null to take the one recorded. | ||
| * @return object|null The job recorded before this call when taking it. | ||
| */ | ||
| function cdcf_last_enqueued_job($job = null): ?object { | ||
| static $last = null; | ||
| if ($job !== null) { | ||
| $last = $job; | ||
| return null; | ||
| } | ||
| $taken = $last; | ||
| $last = null; | ||
| return $taken; | ||
| } | ||
|
|
||
| /** | ||
| * Runs on `redis_queue_job_enqueued`. | ||
| * | ||
| * @param string $job_id The new job's id. | ||
| * @param object $job The job enqueued. | ||
| */ | ||
| function cdcf_remember_enqueued_job($job_id, $job): void { | ||
| unset($job_id); // unused — the action passes it before the job | ||
| if (is_object($job)) { | ||
| cdcf_last_enqueued_job($job); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Runs on `redis_queue_job_retried`, after the old record is set back to | ||
| * 'queued'. | ||
| * | ||
| * @param string $job_id The retried job's old id. | ||
| * @param object $job The job the retry enqueued again. | ||
| */ | ||
| function cdcf_delete_superseded_job_record($job_id, $job = null): void { | ||
| if ($job === null || cdcf_last_enqueued_job() !== $job) { | ||
| // The replacement was not enqueued: keep the old record. | ||
| return; | ||
| } | ||
| global $wpdb; | ||
| $wpdb->delete( | ||
| $wpdb->prefix . 'redis_queue_jobs', | ||
| ['job_id' => (string) $job_id, 'status' => 'queued'], | ||
| ['%s', '%s'] | ||
| ); | ||
| } | ||
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
wordpress/plugins/cdcf-redis-translations/tests/RetryCleanupTest.php
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 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use Brain\Monkey; | ||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| require_once __DIR__ . '/../includes/retry-cleanup.php'; | ||
|
|
||
| /** | ||
| * Tests for cdcf_delete_superseded_job_record(), which removes the job | ||
| * record a redis-queue retry replaces with a new one. | ||
| */ | ||
| final class RetryCleanupTest extends TestCase | ||
| { | ||
| protected function setUp(): void | ||
| { | ||
| parent::setUp(); | ||
| Monkey\setUp(); | ||
| } | ||
|
|
||
| protected function tearDown(): void | ||
| { | ||
| unset($GLOBALS['wpdb']); | ||
| Monkey\tearDown(); | ||
| Mockery::close(); | ||
| parent::tearDown(); | ||
| } | ||
|
|
||
| public function test_deletes_the_superseded_record_only_while_it_is_still_queued(): void | ||
| { | ||
| $wpdb = Mockery::mock(); | ||
| $wpdb->prefix = 'wp_'; | ||
| $wpdb->shouldReceive('delete') | ||
| ->once() | ||
| ->with('wp_redis_queue_jobs', ['job_id' => 'job_abc', 'status' => 'queued'], ['%s', '%s']) | ||
| ->andReturn(1); | ||
| $GLOBALS['wpdb'] = $wpdb; | ||
|
|
||
| $job = new stdClass(); | ||
| cdcf_remember_enqueued_job('job_new', $job); | ||
| cdcf_delete_superseded_job_record('job_abc', $job); | ||
|
|
||
| $this->addToAssertionCount(1); | ||
| } | ||
|
|
||
| public function test_keeps_the_record_when_the_replacement_was_not_enqueued(): void | ||
| { | ||
| $wpdb = Mockery::mock(); | ||
| $wpdb->prefix = 'wp_'; | ||
| $wpdb->shouldNotReceive('delete'); | ||
| $GLOBALS['wpdb'] = $wpdb; | ||
|
|
||
| // An earlier, unrelated job was enqueued; the retry's enqueue failed. | ||
| cdcf_remember_enqueued_job('job_other', new stdClass()); | ||
| cdcf_delete_superseded_job_record('job_abc', new stdClass()); | ||
|
|
||
| $this->addToAssertionCount(1); | ||
| } | ||
|
|
||
| public function test_a_successful_enqueue_is_used_once(): void | ||
| { | ||
| $wpdb = Mockery::mock(); | ||
| $wpdb->prefix = 'wp_'; | ||
| $wpdb->shouldReceive('delete')->once()->andReturn(1); | ||
| $GLOBALS['wpdb'] = $wpdb; | ||
|
|
||
| $job = new stdClass(); | ||
| cdcf_remember_enqueued_job('job_new', $job); | ||
| cdcf_delete_superseded_job_record('job_abc', $job); | ||
| // A second retry of the same job whose enqueue failed. | ||
| cdcf_delete_superseded_job_record('job_new', $job); | ||
|
|
||
| $this->addToAssertionCount(1); | ||
| } | ||
|
|
||
| public function test_keeps_the_record_without_the_job(): void | ||
| { | ||
| $wpdb = Mockery::mock(); | ||
| $wpdb->prefix = 'wp_'; | ||
| $wpdb->shouldNotReceive('delete'); | ||
| $GLOBALS['wpdb'] = $wpdb; | ||
|
|
||
| cdcf_delete_superseded_job_record('job_abc'); | ||
|
|
||
| $this->addToAssertionCount(1); | ||
| } | ||
| } |
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
Oops, something went wrong.
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.