Skip to content
This repository was archived by the owner on Nov 9, 2020. It is now read-only.

Commit 4f0ae61

Browse files
committed
Imeplement RemoteQueue class to push jobs
1 parent 3a673dc commit 4f0ae61

File tree

4 files changed

+157
-7
lines changed

4 files changed

+157
-7
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,22 @@ todo
2323

2424
## Usage
2525

26+
To use the remote queue, configure a queue connection in the `queue.connections` config to use the `remote` driver:
27+
28+
```php
29+
[
30+
'driver' => 'remote',
31+
// Default queue of the remote host to push jobs to.
32+
'queue' => 'default',
33+
// The remote queue API endpoint of the remote host.
34+
'url' => 'http://192.168.100.100/api/v1/remote-queue',
35+
// Basic auth username to use for authentication on the remote host.
36+
'username' => '',
37+
// Basic auth password to use for authentication on the remote host.
38+
'password' => '',
39+
]
40+
```
41+
2642
```php
2743
use App\Jobs\MyJob;
2844

src/RemoteConnector.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Biigle\RemoteQueue;
44

5+
use GuzzleHttp\Client;
56
use Illuminate\Queue\Connectors\ConnectorInterface;
67

78
class RemoteConnector implements ConnectorInterface
@@ -14,6 +15,11 @@ class RemoteConnector implements ConnectorInterface
1415
*/
1516
public function connect(array $config)
1617
{
17-
return new RemoteQueue;
18+
$client = new Client([
19+
'base_uri' => $config['url'],
20+
'auth' => [$config['username'], $config['password']],
21+
]);
22+
23+
return new RemoteQueue($client, $config['queue']);
1824
}
1925
}

src/RemoteQueue.php

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,39 @@
22

33
namespace Biigle\RemoteQueue;
44

5+
use GuzzleHttp\Client;
56
use Illuminate\Queue\Queue;
67
use Illuminate\Queue\Connectors\ConnectorInterface;
78
use Illuminate\Contracts\Queue\Queue as QueueContract;
89

910
class RemoteQueue extends Queue implements QueueContract
1011
{
12+
/**
13+
* Guzzle client for the configured remote entpoint
14+
*
15+
* @var Client
16+
*/
17+
protected $client;
18+
19+
/**
20+
* Default queue to push jobs on the remote end
21+
*
22+
* @var string
23+
*/
24+
protected $default;
25+
26+
/**
27+
* Create a new remote queue instance.
28+
*
29+
* @param Client $client Guzzle client for the configured remote entpoint
30+
* @param string $default Default queue to push jobs on the remote end
31+
*/
32+
public function __construct(Client $client, $default = 'default')
33+
{
34+
$this->client = $client;
35+
$this->default = $default;
36+
}
37+
1138
/**
1239
* Get the size of the queue.
1340
*
@@ -16,7 +43,10 @@ class RemoteQueue extends Queue implements QueueContract
1643
*/
1744
public function size($queue = null)
1845
{
19-
return 0;
46+
$queue = $this->getQueue($queue);
47+
$response = $this->client->get("queue/{$queue}/size");
48+
49+
return (int) $response->getBody()->getContents();
2050
}
2151

2252
/**
@@ -29,7 +59,7 @@ public function size($queue = null)
2959
*/
3060
public function push($job, $data = '', $queue = null)
3161
{
32-
//
62+
return $this->pushRaw($this->createPayload($job, $data), $queue);
3363
}
3464

3565
/**
@@ -42,7 +72,9 @@ public function push($job, $data = '', $queue = null)
4272
*/
4373
public function pushRaw($payload, $queue = null, array $options = [])
4474
{
45-
//
75+
$queue = $this->getQueue($queue);
76+
77+
return $this->client->post("queue/{$queue}", ['body' => $payload]);
4678
}
4779

4880
/**
@@ -56,7 +88,8 @@ public function pushRaw($payload, $queue = null, array $options = [])
5688
*/
5789
public function later($delay, $job, $data = '', $queue = null)
5890
{
59-
//
91+
// A delay is unsupported for now.
92+
$this->pushRaw($this->createPayload($job, $data), $queue);
6093
}
6194

6295
/**
@@ -69,4 +102,15 @@ public function pop($queue = null)
69102
{
70103
//
71104
}
105+
106+
/**
107+
* Get the queue or return the default.
108+
*
109+
* @param string|null $queue
110+
* @return string
111+
*/
112+
protected function getQueue($queue)
113+
{
114+
return $queue ?: $this->default;
115+
}
72116
}

tests/RemoteQueueTest.php

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,94 @@
22

33
namespace Biigle\RemoteQueue\Tests;
44

5+
use Queue;
6+
use GuzzleHttp\Client;
7+
use GuzzleHttp\Middleware;
8+
use GuzzleHttp\HandlerStack;
9+
use GuzzleHttp\Psr7\Response;
10+
use Illuminate\Bus\Queueable;
11+
use GuzzleHttp\Handler\MockHandler;
12+
use Biigle\RemoteQueue\RemoteQueue;
13+
514
class RemoteQueueTest extends TestCase
615
{
7-
public function testTest()
16+
public function setUp()
17+
{
18+
parent::setUp();
19+
config(['queue.connections.remote' => [
20+
'driver' => 'remote',
21+
'queue' => 'default',
22+
'url' => 'http://localhost/api/remote-queue',
23+
'username' => 'test',
24+
'password' => 'test',
25+
]]);
26+
}
27+
28+
public function testDriver()
29+
{
30+
$queue = Queue::connection('remote');
31+
$this->assertInstanceOf(RemoteQueue::class, $queue);
32+
}
33+
34+
public function testSize()
35+
{
36+
$mock = new MockHandler([new Response(200, [], 123)]);
37+
$handler = HandlerStack::create($mock);
38+
$container = [];
39+
$handler->push(Middleware::history($container));
40+
$client = new Client(['handler' => $handler]);
41+
$queue = new RemoteQueue($client);
42+
43+
$size = $queue->size();
44+
$this->assertEquals(123, $size);
45+
$this->assertEquals('queue/default/size', $container[0]['request']->getUri());
46+
}
47+
48+
public function testSizeQueue()
849
{
9-
//
50+
$mock = new MockHandler([new Response(200, [], 123)]);
51+
$handler = HandlerStack::create($mock);
52+
$container = [];
53+
$handler->push(Middleware::history($container));
54+
$client = new Client(['handler' => $handler]);
55+
$queue = new RemoteQueue($client);
56+
57+
$size = $queue->size('gpu');
58+
$this->assertEquals(123, $size);
59+
$this->assertEquals('queue/gpu/size', $container[0]['request']->getUri());
1060
}
61+
62+
public function testPush()
63+
{
64+
$mock = new MockHandler([new Response(200)]);
65+
$handler = HandlerStack::create($mock);
66+
$container = [];
67+
$handler->push(Middleware::history($container));
68+
$client = new Client(['handler' => $handler]);
69+
$queue = new RemoteQueue($client);
70+
71+
$queue->push(new TestJob);
72+
$request = $container[0]['request'];
73+
$this->assertEquals('POST', $request->getMethod());
74+
$this->assertEquals('queue/default', $request->getUri());
75+
$this->assertContains('"commandName":"Biigle\\\RemoteQueue\\\Tests\\\TestJob"', $request->getBody()->getContents());
76+
}
77+
78+
public function testPushQueue()
79+
{
80+
$mock = new MockHandler([new Response(200)]);
81+
$handler = HandlerStack::create($mock);
82+
$container = [];
83+
$handler->push(Middleware::history($container));
84+
$client = new Client(['handler' => $handler]);
85+
$queue = new RemoteQueue($client);
86+
87+
$queue->push(new TestJob, '', 'gpu');
88+
$this->assertEquals('queue/gpu', $container[0]['request']->getUri());
89+
}
90+
}
91+
92+
class TestJob
93+
{
94+
use Queueable;
1195
}

0 commit comments

Comments
 (0)