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

Commit 9b7634b

Browse files
committed
Implement QueueController to receive jobs
1 parent 900263f commit 9b7634b

File tree

13 files changed

+354
-24
lines changed

13 files changed

+354
-24
lines changed

README.md

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Remote Queue
22

3-
Submit jobs to other Laravel or Lumen instances.
3+
Submit jobs to and receive jobs from other Laravel or Lumen instances.
44

55
## Installation
66

@@ -19,11 +19,49 @@ Add `$app->register(Biigle\RemoteQueue\RemoteQueueServiceProvider::class);` to `
1919

2020
## Configuration
2121

22-
todo
22+
You can override any of these configuration options of the `remote-queue` config either directly or via environment variables:
23+
24+
### remote-queue.listen
25+
26+
Default: `false`
27+
Environment: `REMOTE_QUEUE_LISTEN`
28+
29+
Accept and process jobs sent to this application instance.
30+
31+
### remote-queue.endpoint
32+
33+
Default: `api/v1/remote-queue`
34+
Environment: `REMOTE_QUEUE_ENDPOINT`
35+
36+
API endpoint to receive new jobs.
37+
38+
### remote-queue.connection
39+
40+
Default: `null`
41+
Environment: `REMOTE_QUEUE_CONNECTION`
42+
43+
Use this queue connection to process received jobs. If `null`, the default connection is used.
44+
45+
### remote-queue.accept-tokens
46+
47+
Default: `[]`
48+
Environment: `REMOTE_QUEUE_ACCEPT_TOKENS`
49+
50+
Accept jobs only if they provide one of these tokens. Specify tokens as comma separated list if you use the environment variable. One way to generate tokens is this command: `head -c 32 /dev/urandom | base64`.
2351

2452
## Usage
2553

26-
To use the remote queue, configure a queue connection in the `queue.connections` config to use the `remote` driver:
54+
This package can be used to submit queued jobs to another Laravel or Lumen application, receive jobs from another application or both.
55+
56+
### Receive jobs
57+
58+
By default, this package does not allow receiving of jobs from another application. To allow this, set the `remote-queue.listen` config to `true`. Tokens are used to authenticate incoming requests. A token is just some long random string in this case. Configure `remote-queue.accept-tokens` with all tokens that are accepted. All received jobs which are successfully authenticated are pushed to a "regular" queue of this application and processed by the queue worker.
59+
60+
**Important:** Make sure that the job class exists in both the submitting and receiving application.
61+
62+
### Submit jobs
63+
64+
Jobs pushed to the remote queue are transmitted via HTTP and processed on another application. To use the remote queue, configure a queue connection in the `queue.connections` config to use the `remote`. Example:
2765

2866
```php
2967
[
@@ -32,20 +70,20 @@ To use the remote queue, configure a queue connection in the `queue.connections`
3270
'queue' => 'default',
3371
// The remote queue API endpoint of the remote host.
3472
'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' => '',
73+
// Token to use for authentication on the remote host.
74+
'token' => 'IoO2l7UKZfso5zQloF2XvAShEbAR5a9M8u+WBfg0HgI=',
3975
]
4076
```
4177

78+
You can now dispatch jobs to the remote queue connection just like any other connection.
79+
4280
```php
4381
use App\Jobs\MyJob;
4482

4583
MyJob::dispatch($data)->onConnection('remote');
4684
```
4785

48-
## Submit/Response Pattern
86+
### Submit/Response Pattern
4987

5088
We developed this package to be able to process jobs on a remote machine with GPU. To return the computed results, we applied what we call the "submit/response" pattern.
5189

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"guzzlehttp/guzzle": "~6.0",
1313
"illuminate/queue": "^5.5",
1414
"illuminate/support": "^5.5",
15-
"illuminate/contracts": "^5.5"
15+
"illuminate/contracts": "^5.5",
16+
"illuminate/routing": "^5.5"
1617
},
1718
"autoload": {
1819
"psr-4": {
@@ -36,6 +37,7 @@
3637
},
3738
"require-dev": {
3839
"laravel/laravel": "^5.5",
39-
"phpunit/phpunit": "^7.4"
40+
"phpunit/phpunit": "^7.4",
41+
"mockery/mockery": "^1.2"
4042
}
4143
}

composer.lock

Lines changed: 114 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

phpunit.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,11 @@
1818
<directory suffix=".php">src</directory>
1919
</whitelist>
2020
</filter>
21+
22+
<php>
23+
<env name="APP_DEBUG" value="true"/>
24+
<env name="APP_ENV" value="testing"/>
25+
<env name="CACHE_DRIVER" value="array"/>
26+
<env name="REMOTE_QUEUE_LISTEN" value="true"/>
27+
</php>
2128
</phpunit>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Biigle\RemoteQueue\Http\Controllers;
4+
5+
use Illuminate\Http\Request;
6+
use Illuminate\Http\Response;
7+
use Illuminate\Routing\Controller;
8+
use Illuminate\Queue\QueueManager;
9+
10+
class QueueController extends Controller
11+
{
12+
/**
13+
* Get the size of a queue
14+
*
15+
* @param QueueManager $manager
16+
* @param string $queue
17+
*
18+
* @return int
19+
*/
20+
public function show(QueueManager $manager, $queue)
21+
{
22+
return $manager->connection()->size($queue);
23+
}
24+
25+
/**
26+
* Accept a new job and push it to the local queue.
27+
*
28+
* @param Request $request
29+
* @param QueueManager $manager
30+
* @param string $queue
31+
*/
32+
public function store(Request $request, QueueManager $manager, $queue)
33+
{
34+
$payload = $request->getContent();
35+
36+
if (!$payload) {
37+
return new Response('Job payload is required', 422);
38+
}
39+
40+
$manager->connection(config('remote-queue.connection'))
41+
->pushRaw($payload, $queue);
42+
43+
}
44+
}

src/Http/Middleware/Authenticate.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Biigle\RemoteQueue\Http\Middleware;
4+
5+
use Closure;
6+
7+
class Authenticate
8+
{
9+
/**
10+
* Handle an incoming request.
11+
*
12+
* @param \Illuminate\Http\Request $request
13+
* @param \Closure $next
14+
* @param string|null $guard
15+
* @return mixed
16+
*/
17+
public function handle($request, Closure $next, $guard = null)
18+
{
19+
$token = $request->bearerToken();
20+
if ($token && in_array($token, config('remote-queue.accept-tokens'))) {
21+
return $next($request);
22+
}
23+
24+
return response('Unauthorized.', 401);
25+
}
26+
}

src/RemoteConnector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function connect(array $config)
1717
{
1818
$client = new Client([
1919
'base_uri' => $config['url'],
20-
'auth' => [$config['username'], $config['password']],
20+
'headers' => ['Authorization' => "Bearer {$config['token']}"]
2121
]);
2222

2323
return new RemoteQueue($client, $config['queue']);

src/RemoteQueue.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function __construct(Client $client, $default = 'default')
4444
public function size($queue = null)
4545
{
4646
$queue = $this->getQueue($queue);
47-
$response = $this->client->get("queue/{$queue}/size");
47+
$response = $this->client->get("{$queue}/size");
4848

4949
return (int) $response->getBody()->getContents();
5050
}
@@ -74,7 +74,7 @@ public function pushRaw($payload, $queue = null, array $options = [])
7474
{
7575
$queue = $this->getQueue($queue);
7676

77-
return $this->client->post("queue/{$queue}", ['body' => $payload]);
77+
return $this->client->post("{$queue}", ['body' => $payload]);
7878
}
7979

8080
/**

src/RemoteQueueServiceProvider.php

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

33
namespace Biigle\RemoteQueue;
44

5+
use Illuminate\Routing\Router;
56
use Illuminate\Queue\QueueManager;
67
use Illuminate\Support\ServiceProvider;
8+
use Biigle\RemoteQueue\Http\Middleware\Authenticate;
79

810
class RemoteQueueServiceProvider extends ServiceProvider
911
{
@@ -12,11 +14,23 @@ class RemoteQueueServiceProvider extends ServiceProvider
1214
*
1315
* @return void
1416
*/
15-
public function boot(QueueManager $manager)
17+
public function boot(QueueManager $manager, Router $router)
1618
{
1719
$manager->addConnector('remote', function() {
1820
return new RemoteConnector;
1921
});
22+
23+
if (config('remote-queue.listen')) {
24+
$router->group([
25+
'prefix' => config('remote-queue.endpoint'),
26+
'namespace' => 'Biigle\RemoteQueue\Http\Controllers',
27+
'middleware' => Authenticate::class,
28+
], function ($router) {
29+
$router->post('{queue}', 'QueueController@store');
30+
$router->get('{queue}/size', 'QueueController@show');
31+
});
32+
}
33+
2034
}
2135

2236
/**
@@ -26,6 +40,6 @@ public function boot(QueueManager $manager)
2640
*/
2741
public function register()
2842
{
29-
//
43+
$this->mergeConfigFrom(__DIR__.'/config/remote-queue.php', 'remote-queue');
3044
}
3145
}

0 commit comments

Comments
 (0)