Skip to content

Commit 466d1c6

Browse files
author
Eugene Leonovich
committed
Add Queue::call()
1 parent 848ba64 commit 466d1c6

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,41 @@ $total = $queue->stats('tasks.total');
234234
```
235235

236236

237+
### Custom methods
238+
239+
Thanks to flexible nature of the [tarantool/queue](https://github.com/tarantool/queue/) module,
240+
you can easily create your own queue drivers or extend existing ones with an additional functionality.
241+
For example, let's say you want to add the `put_many` method to your `foobar` queue, which inserts
242+
multiple tasks in a transaction:
243+
244+
```lua
245+
-- queues.lua
246+
247+
...
248+
249+
queue.tube.foobar.put_many = function(self, items)
250+
local put = {}
251+
252+
box.begin()
253+
for k, item in pairs(items) do
254+
put[k] = tube:put(unpack(item))
255+
end
256+
box.commit()
257+
258+
return put
259+
end
260+
```
261+
262+
To call it, pass the method name and corresponding arguments to `Queue::call()`:
263+
264+
```php
265+
$result = $queue->call('put_many', [
266+
'foo' => ['foo', []],
267+
'bar' => ['bar', [Options::DELAY => 30]],
268+
]);
269+
```
270+
271+
237272
## Tests
238273

239274
The easiest way to run tests is with Docker. First, build an image using the [dockerfile.sh](dockerfile.sh) generator:

src/Queue.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,4 +191,15 @@ public function stats($path = null)
191191

192192
return $result;
193193
}
194+
195+
/**
196+
* @param string $method
197+
* @param array $args
198+
*
199+
* @return array
200+
*/
201+
public function call($method, array $args = [])
202+
{
203+
return $this->client->call("queue.tube.$this->name:$method", $args);
204+
}
194205
}

tests/Integration/QueueTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,16 @@ public function provideStatsInvalidPathData()
375375
];
376376
}
377377

378+
/**
379+
* @eval queue.tube['%tube_name%'].pow = function(self, base, exp) return math.pow(base, exp) end
380+
*/
381+
public function testCall()
382+
{
383+
$result = $this->queue->call('pow', [2, 8]);
384+
385+
$this->assertSame(256, $result[0][0]);
386+
}
387+
378388
/**
379389
* @dataProvider provideFailureCallbackData
380390
* @expectedException \Exception

0 commit comments

Comments
 (0)