Skip to content

Commit

Permalink
Merge pull request #1 from intouchinsight/support-laravel-11
Browse files Browse the repository at this point in the history
Support laravel 11
  • Loading branch information
craig-roy authored Oct 9, 2024
2 parents bd49b17 + 9a0fe23 commit 924434c
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 58 deletions.
12 changes: 4 additions & 8 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,7 @@ jobs:
uses: sudo-bot/action-scrutinizer@latest
with:
cli-args: "--format=php-clover tests/reports/clover.xml --revision=${{ github.event.pull_request.head.sha || github.sha }}"
phpcs:
name: phpcs
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: php-actions/composer@v6
- name: Check PSR-12 Codestyle
run: composer check-style
pint:
uses: intouchinsight/github-workflows/.github/workflows/laravel-pint.yml@main
with:
php_version: '8.3'
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1 @@
php 8.1.13
php 8.3.3
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,19 @@
}
},
"require": {
"illuminate/support": "^8",
"illuminate/support": "^10.0|^11.0",
"aws/aws-sdk-php": "^3.20.6"
},
"require-dev": {
"mockery/mockery": "^1.5",
"laravel/framework": "^8",
"laravel/framework": "^10.0|^11.0",
"phpunit/phpunit": "^9.5",
"squizlabs/php_codesniffer": "^3.5",
"scrutinizer/ocular": "^1.3",
"phpstan/phpstan": "^1.9",
"nunomaduro/larastan": "^1|^2",
"orchestra/testbench": "^6|^7"
"orchestra/testbench": "^8.0|^9.0",
"laravel/pint": "^1.16"
},
"scripts": {
"test": "phpunit",
Expand Down
9 changes: 9 additions & 0 deletions pint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"preset": "laravel",
"rules": {
"not_operator_with_successor_space": false,
"concat_space": {
"spacing": "one"
}
}
}
3 changes: 1 addition & 2 deletions src/BatchQueueServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ public function boot()
/**
* Register the Batch queue connector.
*
* @param \Illuminate\Queue\QueueManager $manager
*
* @param \Illuminate\Queue\QueueManager $manager
* @return void
*/
protected function registerBatchConnector($manager)
Expand Down
3 changes: 1 addition & 2 deletions src/Connectors/BatchConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class BatchConnector extends DatabaseConnector
/**
* Establish a queue connection.
*
* @param array $config
*
* @return \Illuminate\Contracts\Queue\Queue
*/
Expand All @@ -35,7 +34,7 @@ public function connect(array $config)
Arr::get($config, 'expire', 60),
$config['jobDefinition'],
new BatchClient([
'region' => $config['region'],
'region' => $config['region'],
'version' => '2016-08-10',
])
);
Expand Down
35 changes: 19 additions & 16 deletions src/Console/QueueWorkBatchCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,43 +15,47 @@
use Illuminate\Console\Command;
use Illuminate\Contracts\Cache\Repository as Cache;
use Illuminate\Foundation\Exceptions\Handler;
use Illuminate\Queue\Console\WorkCommand;
use Illuminate\Queue\QueueManager;
use Illuminate\Queue\Worker;
use Illuminate\Queue\WorkerOptions;
use LukeWaite\LaravelQueueAwsBatch\Exceptions\JobNotFoundException;
use LukeWaite\LaravelQueueAwsBatch\Exceptions\UnsupportedException;
use LukeWaite\LaravelQueueAwsBatch\Queues\BatchQueue;
use Symfony\Component\Debug\Exception\FatalThrowableError;

class QueueWorkBatchCommand extends WorkCommand
class QueueWorkBatchCommand extends Command
{
protected $name = 'queue:work-batch';

protected $description = 'Run a Job for the AWS Batch queue';

protected $signature = 'queue:work-batch
{connection : The name of the queue connection to work}
{job_id : The job id in the database}
{connection? : The name of the queue connection to work}
{--memory=128 : The memory limit in megabytes}
{--timeout=60 : The number of seconds a child process can run}
{--tries=0 : Number of times to attempt a job before logging it failed}';

{--force : Force the worker to run even in maintenance mode}
{--tries= : Number of times to attempt a job before logging it failed}';

protected $manager;

protected $exceptions;

protected $worker;

protected $cache;

public function __construct(QueueManager $manager, Worker $worker, Handler $exceptions, Cache $cache)
{
parent::__construct($worker, $cache);
parent::__construct();

$this->manager = $manager;
$this->worker = $worker;
$this->exceptions = $exceptions;
$this->cache = $cache;
}

public function fire()
public function handle()
{
$this->listenForEvents();

try {
$this->runJob();
} catch (\Throwable $e) {
Expand Down Expand Up @@ -83,6 +87,7 @@ protected function runJob()
$job,
$this->gatherWorkerOptions()
);

return;
}

Expand All @@ -98,12 +103,10 @@ protected function runJob()
protected function gatherWorkerOptions()
{
return new WorkerOptions(
0,
$this->option('memory'),
$this->option('timeout'),
0,
$this->option('tries'),
false
name: $this->argument('connection'),
memory: $this->option('memory'),
timeout: $this->option('timeout'),
maxTries: $this->option('tries'),
);
}
}
4 changes: 2 additions & 2 deletions src/Jobs/BatchJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ class BatchJob extends DatabaseJob
*
* Here we need to retain the same jobId, so Batch can retry it, so we need to override the parent.
*
* @param int $delay
*
* @param int $delay
* @return void
*
* @throws UnsupportedException
*/
public function release($delay = 0)
Expand Down
31 changes: 15 additions & 16 deletions src/Queues/BatchQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function __construct(
public function push($job, $data = '', $queue = null)
{
$payload = $this->createPayload($job, $data);

return $this->pushToBatch($queue, $payload, $this->getBatchDisplayName($job));
}

Expand All @@ -65,7 +66,7 @@ protected function getBatchDisplayName($job)
{
if (is_object($job)) {
return method_exists($job, 'displayName')
? $job->displayName() : str_replace('\\', '_', (string)get_class($job));
? $job->displayName() : str_replace('\\', '_', (string) get_class($job));
} else {
return is_string($job) ? explode('@', $job)[0] : null;
}
Expand All @@ -74,10 +75,9 @@ protected function getBatchDisplayName($job)
/**
* Push a raw payload to the database, then to AWS Batch, with a given delay.
*
* @param string|null $queue
* @param string $payload
* @param string $jobName
*
* @param string|null $queue
* @param string $payload
* @param string $jobName
* @return int
*/
protected function pushToBatch($queue, $payload, $jobName)
Expand All @@ -86,11 +86,11 @@ protected function pushToBatch($queue, $payload, $jobName)

$this->batch->submitJob([
'jobDefinition' => $this->jobDefinition,
'jobName' => $jobName,
'jobQueue' => $this->getQueue($queue),
'parameters' => [
'jobName' => $jobName,
'jobQueue' => $this->getQueue($queue),
'parameters' => [
'jobId' => $jobId,
]
],
]);

return $jobId;
Expand All @@ -105,7 +105,6 @@ public function getJobById($id)
->where('id', $id)
->first();


if (!isset($job)) {
throw new JobNotFoundException('Could not find the job');
}
Expand Down Expand Up @@ -133,11 +132,11 @@ protected function marshalJob($queue, $job)
/**
* Release the job, without deleting first from the Queue
*
* @param string $queue
* @param \StdClass $job
* @param int $delay
*
* @param string $queue
* @param \StdClass $job
* @param int $delay
* @return int
*
* @throws UnsupportedException
*/
public function release($queue, $job, $delay)
Expand All @@ -147,8 +146,8 @@ public function release($queue, $job, $delay)
}

return $this->database->table($this->table)->where('id', $job->id)->update([
'attempts' => $job->attempts,
'reserved_at' => null
'attempts' => $job->attempts,
'reserved_at' => null,
]);
}

Expand Down
6 changes: 3 additions & 3 deletions tests/BatchJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
namespace LukeWaite\LaravelQueueAwsBatch\Tests;

use LukeWaite\LaravelQueueAwsBatch\Exceptions\UnsupportedException;
use Mockery as m;
use Mockery\Adapter\Phpunit\MockeryTestCase as TestCase;
use Mockery as m;

class BatchJobTest extends TestCase
{
public function setUp() : void
public function setUp(): void
{
$this->job = new \stdClass();
$this->job->payload = '{"job":"foo","data":["data"]}';
Expand All @@ -22,7 +22,7 @@ public function setUp() : void
$this->batchQueue = m::mock('LukeWaite\LaravelQueueAwsBatch\Queues\BatchQueue'),
$this->job,
'testConnection',
'defaultQueue'
'defaultQueue',
])->getMock();
}

Expand Down
10 changes: 5 additions & 5 deletions tests/BatchQueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@

use Carbon\Carbon;
use LukeWaite\LaravelQueueAwsBatch\Exceptions\UnsupportedException;
use Mockery as m;
use Mockery\Adapter\Phpunit\MockeryTestCase as TestCase;
use Mockery as m;

class BatchQueueTest extends TestCase
{
public function setUp() : void
public function setUp(): void
{
$this->queue = $this->getMockBuilder('LukeWaite\LaravelQueueAwsBatch\Queues\BatchQueue')->setMethods(null)->setConstructorArgs([
$this->database = m::mock('Illuminate\Database\Connection'),
'table',
'default',
'60',
'jobdefinition',
$this->batch = m::mock('Aws\Batch\BatchClient')
$this->batch = m::mock('Aws\Batch\BatchClient'),
])->getMock();

$this->queue->setContainer(m::mock('Illuminate\Container\Container'));
Expand Down Expand Up @@ -80,7 +80,7 @@ public function testGetJobById()
$queryResult->id = 1;

$table->shouldReceive('where')->once()->with('id', 1)->andReturn($reserved = m::mock('StdClass'));
$reserved->shouldReceive('update')->with(['reserved_at'=> 1473004800, 'attempts'=> 1])->once()->andReturn($job = m::mock('StdClass'));
$reserved->shouldReceive('update')->with(['reserved_at' => 1473004800, 'attempts' => 1])->once()->andReturn($job = m::mock('StdClass'));

$this->database->shouldReceive('commit')->once();

Expand All @@ -94,7 +94,7 @@ public function testRelease()
$this->database->shouldReceive('table')->once()->with('table')->andReturn($table = m::mock('StdClass'));
$table->shouldReceive('where')->once()->with('id', 4)->andReturn($query = m::mock('StdClass'));
$query->shouldReceive('update')->once()->with([
'attempts' => 1,
'attempts' => 1,
'reserved_at' => null,
])->andReturn(4);

Expand Down

0 comments on commit 924434c

Please sign in to comment.