Skip to content

Commit

Permalink
Make attempts count state a little more logical.
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Dec 29, 2016
1 parent e5bd041 commit 5505728
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 7 deletions.
5 changes: 4 additions & 1 deletion src/Illuminate/Queue/Jobs/RedisJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ class RedisJob extends Job implements JobContract
*/
public function __construct(Container $container, RedisQueue $redis, $job, $reserved, $queue)
{
// The $job variable is the original job JSON as it existed in the ready queue while
// the $reserved variable is the raw JSON in the reserved queue. The exact format
// of the reserved job is requird in order for us to properly delete its value.
$this->job = $job;
$this->redis = $redis;
$this->queue = $queue;
Expand Down Expand Up @@ -98,7 +101,7 @@ public function release($delay = 0)
*/
public function attempts()
{
return Arr::get($this->decoded, 'attempts');
return Arr::get($this->decoded, 'attempts') + 1;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Queue/RedisQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ protected function createPayloadArray($job, $data = '', $queue = null)
{
return array_merge(parent::createPayloadArray($job, $data, $queue), [
'id' => $this->getRandomId(),
'attempts' => 1,
'attempts' => 0,
]);
}

Expand Down
6 changes: 3 additions & 3 deletions tests/Queue/QueueRedisQueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function testPushProperlyPushesJobOntoRedis()
$queue = $this->getMockBuilder('Illuminate\Queue\RedisQueue')->setMethods(['getRandomId'])->setConstructorArgs([$redis = m::mock('Illuminate\Contracts\Redis\Factory'), 'default'])->getMock();
$queue->expects($this->once())->method('getRandomId')->will($this->returnValue('foo'));
$redis->shouldReceive('connection')->once()->andReturn($redis);
$redis->shouldReceive('rpush')->once()->with('queues:default', json_encode(['job' => 'foo', 'data' => ['data'], 'id' => 'foo', 'attempts' => 1]));
$redis->shouldReceive('rpush')->once()->with('queues:default', json_encode(['job' => 'foo', 'data' => ['data'], 'id' => 'foo', 'attempts' => 0]));

$id = $queue->push('foo', ['data']);
$this->assertEquals('foo', $id);
Expand All @@ -30,7 +30,7 @@ public function testDelayedPushProperlyPushesJobOntoRedis()
$redis->shouldReceive('zadd')->once()->with(
'queues:default:delayed',
2,
json_encode(['job' => 'foo', 'data' => ['data'], 'id' => 'foo', 'attempts' => 1])
json_encode(['job' => 'foo', 'data' => ['data'], 'id' => 'foo', 'attempts' => 0])
);

$id = $queue->later(1, 'foo', ['data']);
Expand All @@ -48,7 +48,7 @@ public function testDelayedPushWithDateTimeProperlyPushesJobOntoRedis()
$redis->shouldReceive('zadd')->once()->with(
'queues:default:delayed',
2,
json_encode(['job' => 'foo', 'data' => ['data'], 'id' => 'foo', 'attempts' => 1])
json_encode(['job' => 'foo', 'data' => ['data'], 'id' => 'foo', 'attempts' => 0])
);

$queue->later($date, 'foo', ['data']);
Expand Down
4 changes: 2 additions & 2 deletions tests/Queue/RedisQueueIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function testPopProperlyPopsJobOffOfRedis()
$this->assertEquals($job, unserialize(json_decode($redisJob->getRawBody())->data->command));
$this->assertEquals(1, $redisJob->attempts());
$this->assertEquals($job, unserialize(json_decode($redisJob->getReservedJob())->data->command));
$this->assertEquals(2, json_decode($redisJob->getReservedJob())->attempts);
$this->assertEquals(1, json_decode($redisJob->getReservedJob())->attempts);
$this->assertEquals($redisJob->getJobId(), json_decode($redisJob->getReservedJob())->id);

// Check reserved queue
Expand Down Expand Up @@ -216,7 +216,7 @@ public function testRelease()

$decoded = json_decode($payload);

$this->assertEquals(2, $decoded->attempts);
$this->assertEquals(1, $decoded->attempts);
$this->assertEquals($job, unserialize($decoded->data->command));

//check if the queue has no ready item yet
Expand Down

0 comments on commit 5505728

Please sign in to comment.