Skip to content

Commit 7f2be57

Browse files
authored
Add Queue::touch() (#10)
1 parent bb20a96 commit 7f2be57

File tree

3 files changed

+68
-4
lines changed

3 files changed

+68
-4
lines changed

README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,13 @@ If a timeout value is supplied the call will wait `timeout` seconds until a `REA
127127
The method returns either a [Task](#tasks) object or `null`:
128128

129129
```php
130-
$task = $queue->take();
130+
$taskOrNull = $queue->take();
131131

132132
// wait 2 seconds
133-
$task = $queue->take(2);
133+
$taskOrNull = $queue->take(2);
134134

135135
// wait 100 milliseconds
136-
$task = $queue->take(.1);
136+
$taskOrNull = $queue->take(.1);
137137
```
138138

139139
After successful execution, a task can be marked as acknowledged (that will also delete the task from a queue):
@@ -151,7 +151,7 @@ Or put back into the queue in case it cannot be executed:
151151
```php
152152
$task = $queue->release($task->getId());
153153

154-
// for ttl-like queues you can specify a delay
154+
// for *ttl queues you can specify a delay
155155
$task = $queue->release($task->getId(), ['delay' => 30]);
156156
```
157157

@@ -173,6 +173,12 @@ To reset buried task(s) back to `READY` state:
173173
$count = $queue->kick(3); // kick 3 buried tasks
174174
```
175175

176+
To increase TTR and/or TTL of a running task (only for *ttl queues):
177+
178+
```php
179+
$taskOrNull = $queue->touch($takenTask->getId(), 5); // increase ttr/ttl to 5 seconds
180+
```
181+
176182
A task (in any state) can be deleted permanently with `delete()`:
177183

178184
```php

src/Queue.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,19 @@ public function take($timeout = null)
7474
return empty($result[0]) ? null : Task::createFromTuple($result[0]);
7575
}
7676

77+
/**
78+
* @param int $taskId
79+
* @param int|float $increment
80+
*
81+
* @return Task|null
82+
*/
83+
public function touch($taskId, $increment)
84+
{
85+
$result = $this->client->call("queue.tube.$this->name:touch", [$taskId, $increment]);
86+
87+
return empty($result[0]) ? null : Task::createFromTuple($result[0]);
88+
}
89+
7790
/**
7891
* @param int $taskId
7992
*

tests/Integration/Ttl.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,51 @@ public function testTimeToLive()
4040
$this->assertNull($task);
4141
}
4242

43+
/**
44+
* @eval queue.tube['%tube_name%']:put('touch_ttr_1', {ttr = 1})
45+
*/
46+
public function testTouchTimeToRun()
47+
{
48+
$task1 = $this->queue->take(.1);
49+
$task2 = $this->queue->touch($task1->getId(), 1);
50+
sleep(1);
51+
$task3 = $this->queue->take(.1);
52+
53+
$this->assertTaskInstance($task2);
54+
$this->assertSame('touch_ttr_1', $task2->getData());
55+
$this->assertEquals($task1, $task2);
56+
$this->assertNull($task3);
57+
}
58+
59+
/**
60+
* @eval queue.tube['%tube_name%']:put('touch_ttl_1', {ttl = 1})
61+
*/
62+
public function testTouchTimeToLive()
63+
{
64+
$task1 = $this->queue->take(.1);
65+
$task2 = $this->queue->touch($task1->getId(), 1);
66+
$this->queue->release($task1->getId());
67+
sleep(1);
68+
$task3 = $this->queue->take(.1);
69+
70+
$this->assertTaskInstance($task2);
71+
$this->assertSame('touch_ttl_1', $task2->getData());
72+
$this->assertEquals($task1, $task2);
73+
$this->assertEquals($task2, $task3);
74+
}
75+
76+
/**
77+
* @eval queue.tube['%tube_name%']:put('touch_invalid_interval')
78+
*/
79+
public function testTouchInvalidInterval()
80+
{
81+
$task = $this->queue->take(.1);
82+
83+
foreach ([0, -1] as $interval) {
84+
$this->assertNull($this->queue->touch($task->getId(), $interval));
85+
}
86+
}
87+
4388
/**
4489
* @eval queue.tube['%tube_name%']:put('pri_low', {pri = 2})
4590
* @eval queue.tube['%tube_name%']:put('pri_high', {pri = 1})

0 commit comments

Comments
 (0)