Skip to content

Commit

Permalink
Set default value for job $payload['pushedAt'] when retrying (#1211)
Browse files Browse the repository at this point in the history
* Add failing test

* Use microtime for pushed at timestamp

* Update test

* CS fixes
  • Loading branch information
stevebauman authored Nov 4, 2022
1 parent be22d2c commit 96d8a1a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Jobs/RetryFailedJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,10 @@ protected function prepareNewTimeout($payload)
{
$retryUntil = $payload['retryUntil'] ?? $payload['timeoutAt'] ?? null;

$pushedAt = $payload['pushedAt'] ?? microtime(true);

return $retryUntil
? CarbonImmutable::now()->addSeconds(ceil($retryUntil - $payload['pushedAt']))->getTimestamp()
? CarbonImmutable::now()->addSeconds(ceil($retryUntil - $pushedAt))->getTimestamp()
: null;
}
}
26 changes: 26 additions & 0 deletions tests/Feature/RetryJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

namespace Laravel\Horizon\Tests\Feature;

use Exception;
use Illuminate\Support\Facades\Queue;
use Illuminate\Support\Facades\Redis;
use Laravel\Horizon\Contracts\JobRepository;
use Laravel\Horizon\JobPayload;
use Laravel\Horizon\Jobs\MonitorTag;
use Laravel\Horizon\Jobs\RetryFailedJob;
use Laravel\Horizon\Tests\IntegrationTest;
Expand Down Expand Up @@ -79,4 +82,27 @@ public function test_status_is_updated_for_double_failing_jobs()
// Test status is now failed on the retry...
$this->assertSame('failed', $retried[0]['status']);
}

public function test_retrying_failed_job_with_retry_until_and_without_pushed_at()
{
$repository = $this->app->make(JobRepository::class);

$payload = new JobPayload(
json_encode([
'id' => 1,
'displayName' => 'foo',
'retryUntil' => now()->addMinute()->timestamp,
])
);

$repository->failed(new Exception('Failed Job'), 'redis', 'default', $payload);

dispatch(new RetryFailedJob(1));
$this->work();

$retried = Redis::connection('horizon')->hget(1, 'retried_by');
$retried = json_decode($retried, true);

$this->assertSame('pending', $retried[0]['status']);
}
}

0 comments on commit 96d8a1a

Please sign in to comment.