From 37edefdfcc6a1f5f64657212cdcd41311ab5e44e Mon Sep 17 00:00:00 2001 From: Luke Waite Date: Mon, 1 Apr 2024 13:39:50 -0400 Subject: [PATCH 1/3] Update Laravel supported versions --- .tool-versions | 2 +- composer.json | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.tool-versions b/.tool-versions index 18e9535..f0bbab3 100644 --- a/.tool-versions +++ b/.tool-versions @@ -1 +1 @@ -php 8.1.13 +php 8.3.3 diff --git a/composer.json b/composer.json index 87419b8..6954ad9 100644 --- a/composer.json +++ b/composer.json @@ -19,18 +19,18 @@ } }, "require": { - "illuminate/support": "^8", + "illuminate/support": "^10", "aws/aws-sdk-php": "^3.20.6" }, "require-dev": { "mockery/mockery": "^1.5", - "laravel/framework": "^8", + "laravel/framework": "^10", "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" }, "scripts": { "test": "phpunit", From 12cafbc5ace9db1fe360f6e45e291a54b1e109a8 Mon Sep 17 00:00:00 2001 From: Luke Waite Date: Mon, 1 Apr 2024 15:09:28 -0400 Subject: [PATCH 2/3] queue:work-batch for L10 --- src/Console/QueueWorkBatchCommand.php | 28 ++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/Console/QueueWorkBatchCommand.php b/src/Console/QueueWorkBatchCommand.php index 03dbe24..b46f82b 100644 --- a/src/Console/QueueWorkBatchCommand.php +++ b/src/Console/QueueWorkBatchCommand.php @@ -24,34 +24,38 @@ 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) { @@ -98,12 +102,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'), ); } } From 9a0fe2357569e6a226608ffa2e7d17e0189e8fb1 Mon Sep 17 00:00:00 2001 From: Grant Lemoine Date: Thu, 23 May 2024 15:40:55 -0400 Subject: [PATCH 3/3] Support Laravel 11 --- .github/workflows/tests.yml | 12 ++++------- composer.json | 7 +++--- pint.json | 9 ++++++++ src/BatchQueueServiceProvider.php | 3 +-- src/Connectors/BatchConnector.php | 3 +-- src/Console/QueueWorkBatchCommand.php | 7 +++--- src/Jobs/BatchJob.php | 4 ++-- src/Queues/BatchQueue.php | 31 +++++++++++++-------------- tests/BatchJobTest.php | 6 +++--- tests/BatchQueueTest.php | 10 ++++----- 10 files changed, 48 insertions(+), 44 deletions(-) create mode 100644 pint.json diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index a3c30f2..25418dc 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -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' diff --git a/composer.json b/composer.json index 6954ad9..f046eab 100644 --- a/composer.json +++ b/composer.json @@ -19,18 +19,19 @@ } }, "require": { - "illuminate/support": "^10", + "illuminate/support": "^10.0|^11.0", "aws/aws-sdk-php": "^3.20.6" }, "require-dev": { "mockery/mockery": "^1.5", - "laravel/framework": "^10", + "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": "^8" + "orchestra/testbench": "^8.0|^9.0", + "laravel/pint": "^1.16" }, "scripts": { "test": "phpunit", diff --git a/pint.json b/pint.json new file mode 100644 index 0000000..a2dd735 --- /dev/null +++ b/pint.json @@ -0,0 +1,9 @@ +{ + "preset": "laravel", + "rules": { + "not_operator_with_successor_space": false, + "concat_space": { + "spacing": "one" + } + } +} \ No newline at end of file diff --git a/src/BatchQueueServiceProvider.php b/src/BatchQueueServiceProvider.php index f8662e1..63e6c2e 100644 --- a/src/BatchQueueServiceProvider.php +++ b/src/BatchQueueServiceProvider.php @@ -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) diff --git a/src/Connectors/BatchConnector.php b/src/Connectors/BatchConnector.php index d0c9026..f7cbcc3 100644 --- a/src/Connectors/BatchConnector.php +++ b/src/Connectors/BatchConnector.php @@ -22,7 +22,6 @@ class BatchConnector extends DatabaseConnector /** * Establish a queue connection. * - * @param array $config * * @return \Illuminate\Contracts\Queue\Queue */ @@ -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', ]) ); diff --git a/src/Console/QueueWorkBatchCommand.php b/src/Console/QueueWorkBatchCommand.php index b46f82b..c37486e 100644 --- a/src/Console/QueueWorkBatchCommand.php +++ b/src/Console/QueueWorkBatchCommand.php @@ -15,14 +15,12 @@ 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 Command { @@ -38,10 +36,12 @@ class QueueWorkBatchCommand extends Command {--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) @@ -87,6 +87,7 @@ protected function runJob() $job, $this->gatherWorkerOptions() ); + return; } diff --git a/src/Jobs/BatchJob.php b/src/Jobs/BatchJob.php index 8ec389a..402a656 100644 --- a/src/Jobs/BatchJob.php +++ b/src/Jobs/BatchJob.php @@ -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) diff --git a/src/Queues/BatchQueue.php b/src/Queues/BatchQueue.php index aa8b77b..ce7597c 100644 --- a/src/Queues/BatchQueue.php +++ b/src/Queues/BatchQueue.php @@ -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)); } @@ -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; } @@ -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) @@ -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; @@ -105,7 +105,6 @@ public function getJobById($id) ->where('id', $id) ->first(); - if (!isset($job)) { throw new JobNotFoundException('Could not find the job'); } @@ -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) @@ -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, ]); } diff --git a/tests/BatchJobTest.php b/tests/BatchJobTest.php index 9631c87..14f44b1 100644 --- a/tests/BatchJobTest.php +++ b/tests/BatchJobTest.php @@ -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"]}'; @@ -22,7 +22,7 @@ public function setUp() : void $this->batchQueue = m::mock('LukeWaite\LaravelQueueAwsBatch\Queues\BatchQueue'), $this->job, 'testConnection', - 'defaultQueue' + 'defaultQueue', ])->getMock(); } diff --git a/tests/BatchQueueTest.php b/tests/BatchQueueTest.php index 186ef66..717ce5e 100644 --- a/tests/BatchQueueTest.php +++ b/tests/BatchQueueTest.php @@ -4,12 +4,12 @@ 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'), @@ -17,7 +17,7 @@ public function setUp() : void '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')); @@ -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(); @@ -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);