Skip to content

Commit

Permalink
+ 5.3 support
Browse files Browse the repository at this point in the history
  • Loading branch information
dusterio committed Sep 14, 2016
1 parent 34fd76c commit 010165e
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 5 deletions.
12 changes: 8 additions & 4 deletions src/Controllers/WorkerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Dusterio\AwsWorker\Exceptions\FailedJobException;
use Dusterio\AwsWorker\Exceptions\MalformedRequestException;
use Dusterio\AwsWorker\Jobs\AwsJob;
use Dusterio\AwsWorker\Wrappers\WorkerInterface;
use Illuminate\Contracts\Container\Container;
use Illuminate\Http\Request;
use Illuminate\Queue\Worker;
Expand Down Expand Up @@ -57,12 +58,12 @@ public function schedule(Container $laravel, Kernel $kernel, Schedule $schedule)

/**
* @param Request $request
* @param Worker $worker
* @param WorkerInterface $worker
* @param Container $laravel
* @return array
* @return Response
* @throws FailedJobException
*/
public function queue(Request $request, Worker $worker, Container $laravel)
public function queue(Request $request, WorkerInterface $worker, Container $laravel)
{
$this->validateHeaders($request);
$body = $this->validateBody($request, $laravel);
Expand All @@ -78,7 +79,10 @@ public function queue(Request $request, Worker $worker, Container $laravel)

try {
$worker->process(
$request->header('X-Aws-Sqsd-Queue'), $job, 0, 0
$request->header('X-Aws-Sqsd-Queue'), $job, [
'maxTries' => 0,
'delay' => 0
]
);
} catch (\Exception $e) {
throw new FailedJobException('Worker failed executing the job', 0, $e);
Expand Down
42 changes: 42 additions & 0 deletions src/Integrations/BindsWorker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Dusterio\AwsWorker\Integrations;

use Dusterio\AwsWorker\Wrappers\WorkerInterface;
use Dusterio\AwsWorker\Wrappers\DefaultWorker;
use Dusterio\AwsWorker\Wrappers\Laravel53Worker;

/**
* Class BindsWorker
* @package Dusterio\AwsWorker\Integrations
*/
trait BindsWorker
{
/**
* @var array
*/
protected $workerImplementations = [
'5\.3.*' => Laravel53Worker::class
];

/**
* @param $version
* @return mixed
*/
protected function findWorkerClass($version)
{
foreach ($this->workerImplementations as $regexp => $class) {
if (preg_match('/' . $regexp . '/', $version)) return $class;
}

return DefaultWorker::class;
}

/**
* @return void
*/
protected function bindWorker()
{
$this->app->bind(WorkerInterface::class, $this->findWorkerClass($this->app->version()));
}
}
6 changes: 6 additions & 0 deletions src/Integrations/LaravelServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Dusterio\AwsWorker\Integrations;

use Dusterio\AwsWorker\Wrappers\DefaultWorker;
use Dusterio\AwsWorker\Wrappers\Laravel53Worker;
use Dusterio\AwsWorker\Wrappers\WorkerInterface;
use Dusterio\PlainSqs\Sqs\Connector;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Queue;
Expand All @@ -14,13 +17,16 @@
*/
class LaravelServiceProvider extends ServiceProvider
{
use BindsWorker;

/**
* @return void
*/
public function register()
{
if ($this->app->environment() == 'production') return;

$this->bindWorker();
$this->addRoutes();
}

Expand Down
3 changes: 3 additions & 0 deletions src/Integrations/LumenServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@
*/
class LumenServiceProvider extends ServiceProvider
{
use BindsWorker;

/**
* @return void
*/
public function register()
{
if ($this->app->environment() == 'production') return;

$this->bindWorker();
$this->addRoutes();
}

Expand Down
7 changes: 6 additions & 1 deletion src/Jobs/AwsJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ public function __construct(Container $container,
*/
public function fire()
{
$this->resolveAndFire(json_decode($this->getRawBody(), true));
if (method_exists($this, 'resolveAndFire')) {
$this->resolveAndFire(json_decode($this->getRawBody(), true));
return;
}

parent::fire();
}

/**
Expand Down
34 changes: 34 additions & 0 deletions src/Wrappers/DefaultWorker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Dusterio\AwsWorker\Wrappers;

use Illuminate\Queue\Worker;

/**
* Class DefaultWorker
* @package Dusterio\AwsWorker\Wrappers
*/
class DefaultWorker implements WorkerInterface
{
/**
* DefaultWorker constructor.
* @param Worker $worker
*/
public function __construct(Worker $worker)
{
$this->worker = $worker;
}

/**
* @param $queue
* @param $job
* @param array $options
* @return void
*/
public function process($queue, $job, array $options)
{
$this->worker->process(
$queue, $job, $options['maxTries'], $options['delay']
);
}
}
37 changes: 37 additions & 0 deletions src/Wrappers/Laravel53Worker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Dusterio\AwsWorker\Wrappers;

use Illuminate\Queue\Worker;
use Illuminate\Queue\WorkerOptions;

/**
* Class Laravel53Worker
* @package Dusterio\AwsWorker\Wrappers
*/
class Laravel53Worker implements WorkerInterface
{
/**
* DefaultWorker constructor.
* @param Worker $worker
*/
public function __construct(Worker $worker)
{
$this->worker = $worker;
}

/**
* @param $queue
* @param $job
* @param array $options
* @return void
*/
public function process($queue, $job, array $options)
{
$workerOptions = new WorkerOptions($options['delay'], 128, 60, 3, $options['maxTries']);

$this->worker->process(
$queue, $job, $workerOptions
);
}
}
18 changes: 18 additions & 0 deletions src/Wrappers/WorkerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Dusterio\AwsWorker\Wrappers;

/**
* Interface WorkerInterface
* @package Dusterio\AwsWorker\Wrappers
*/
interface WorkerInterface
{
/**
* @param $queue
* @param $job
* @param array $options
* @return mixed
*/
public function process($queue, $job, array $options);
}

0 comments on commit 010165e

Please sign in to comment.