Skip to content
This repository has been archived by the owner on Jun 4, 2024. It is now read-only.

Commit

Permalink
Add laravel vapor support
Browse files Browse the repository at this point in the history
  • Loading branch information
SimplyCorey committed Oct 10, 2021
1 parent 3e087ac commit 2cb8cca
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 86 deletions.
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
"phpunit/phpunit": "~9",
"friendsofphp/php-cs-fixer": "^3.2"
},
"suggest": {
"laravel/vapor-core": "Allows SQS disk based storage while using Laravel Vapor."
},
"autoload": {
"psr-4": {
"SimpleSoftwareIO\\SqsDisk\\": "src"
Expand Down
94 changes: 94 additions & 0 deletions src/SqsDiskBaseJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace SimpleSoftwareIO\SqsDisk;

use Aws\Sqs\SqsClient;
use Illuminate\Support\Arr;
use Illuminate\Container\Container;

trait SqsDiskBaseJob
{
use ResolvesPointers;

/**
* The Amazon SQS client instance.
*
* @var \Aws\Sqs\SqsClient
*/
protected $sqs;

/**
* The Amazon SQS job instance.
*
* @var array
*/
protected $job;

/**
* Holds the raw body to prevent fetching the file from
* the disk multiple times.
*
* @var string
*/
protected $cachedRawBody;

/**
* The disk options for the job.
*
* @var array
*/
protected $diskOptions;

/**
* Create a new job instance.
*
* @param \Illuminate\Container\Container $container
* @param \Aws\Sqs\SqsClient $sqs
* @param array $job
* @param string $connectionName
* @param string $queue
*
* @return void
*/
public function __construct(Container $container, SqsClient $sqs, array $job, $connectionName, $queue, array $diskOptions)
{
$this->sqs = $sqs;
$this->job = $job;
$this->queue = $queue;
$this->container = $container;
$this->connectionName = $connectionName;
$this->diskOptions = $diskOptions;
}

/**
* Delete the job from the queue.
*
* @return void
*/
public function delete()
{
parent::delete();

if (Arr::get($this->diskOptions, 'cleanup') && $pointer = $this->resolvePointer()) {
$this->resolveDisk()->delete($pointer);
}
}

/**
* Get the raw body string for the job.
*
* @return string
*/
public function getRawBody()
{
if ($this->cachedRawBody) {
return $this->cachedRawBody;
}

if ($pointer = $this->resolvePointer()) {
return $this->cachedRawBody = $this->resolveDisk()->get($pointer);
}

return parent::getRawBody();
}
}
87 changes: 1 addition & 86 deletions src/SqsDiskJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,95 +2,10 @@

namespace SimpleSoftwareIO\SqsDisk;

use Aws\Sqs\SqsClient;
use Illuminate\Support\Arr;
use Illuminate\Queue\Jobs\SqsJob;
use Illuminate\Container\Container;
use Illuminate\Contracts\Queue\Job as JobContract;

class SqsDiskJob extends SqsJob implements JobContract
{
use ResolvesPointers;

/**
* The Amazon SQS client instance.
*
* @var \Aws\Sqs\SqsClient
*/
protected $sqs;

/**
* The Amazon SQS job instance.
*
* @var array
*/
protected $job;

/**
* Holds the raw body to prevent fetching the file from
* the disk multiple times.
*
* @var string
*/
protected $cachedRawBody;

/**
* The disk options for the job.
*
* @var array
*/
protected $diskOptions;

/**
* Create a new job instance.
*
* @param \Illuminate\Container\Container $container
* @param \Aws\Sqs\SqsClient $sqs
* @param array $job
* @param string $connectionName
* @param string $queue
*
* @return void
*/
public function __construct(Container $container, SqsClient $sqs, array $job, $connectionName, $queue, array $diskOptions)
{
$this->sqs = $sqs;
$this->job = $job;
$this->queue = $queue;
$this->container = $container;
$this->connectionName = $connectionName;
$this->diskOptions = $diskOptions;
}

/**
* Delete the job from the queue.
*
* @return void
*/
public function delete()
{
parent::delete();

if (Arr::get($this->diskOptions, 'cleanup') && $pointer = $this->resolvePointer()) {
$this->resolveDisk()->delete($pointer);
}
}

/**
* Get the raw body string for the job.
*
* @return string
*/
public function getRawBody()
{
if ($this->cachedRawBody) {
return $this->cachedRawBody;
}

if ($pointer = $this->resolvePointer()) {
return $this->cachedRawBody = $this->resolveDisk()->get($pointer);
}

return parent::getRawBody();
}
use SqsDiskBaseJob;
}
2 changes: 2 additions & 0 deletions src/SqsDiskServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ public function boot()
{
$manager = $this->app->make('queue');
$manager->addConnector('sqs-disk', fn () => new SqsDiskConnector());

$this->app->extend('command.vapor.work', fn () => new VaporWorkCommand($this->app['queue.vaporWorker']));
}
}
11 changes: 11 additions & 0 deletions src/VaporSqsDiskJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace SimpleSoftwareIO\SqsDisk;

use Laravel\Vapor\Queue\VaporJob;
use Illuminate\Contracts\Queue\Job as JobContract;

class VaporSqsDiskJob extends VaporJob implements JobContract
{
use SqsDiskBaseJob;
}
31 changes: 31 additions & 0 deletions src/VaporWorkCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace SimpleSoftwareIO\SqsDisk;

use Laravel\Vapor\Console\Commands\VaporWorkCommand as LaravelVaporWorkCommand;

class VaporWorkCommand extends LaravelVaporWorkCommand
{
/**
* Marshal the job with the given message ID.
*
* @param array $message
*
* @return \Laravel\Vapor\Queue\VaporJob
*/
protected function marshalJob(array $message)
{
$normalizedMessage = $this->normalizeMessage($message);

$queue = $this->worker->getManager()->connection('sqs');

return new SqsDiskJob(
$this->laravel,
$queue->getSqs(),
$normalizedMessage,
'sqs',
$this->queueUrl($message),
config('queue.connections.sqs.disk_options')
);
}
}

0 comments on commit 2cb8cca

Please sign in to comment.