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

Commit 2cb8cca

Browse files
committed
Add laravel vapor support
1 parent 3e087ac commit 2cb8cca

File tree

6 files changed

+142
-86
lines changed

6 files changed

+142
-86
lines changed

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
"phpunit/phpunit": "~9",
3232
"friendsofphp/php-cs-fixer": "^3.2"
3333
},
34+
"suggest": {
35+
"laravel/vapor-core": "Allows SQS disk based storage while using Laravel Vapor."
36+
},
3437
"autoload": {
3538
"psr-4": {
3639
"SimpleSoftwareIO\\SqsDisk\\": "src"

src/SqsDiskBaseJob.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
namespace SimpleSoftwareIO\SqsDisk;
4+
5+
use Aws\Sqs\SqsClient;
6+
use Illuminate\Support\Arr;
7+
use Illuminate\Container\Container;
8+
9+
trait SqsDiskBaseJob
10+
{
11+
use ResolvesPointers;
12+
13+
/**
14+
* The Amazon SQS client instance.
15+
*
16+
* @var \Aws\Sqs\SqsClient
17+
*/
18+
protected $sqs;
19+
20+
/**
21+
* The Amazon SQS job instance.
22+
*
23+
* @var array
24+
*/
25+
protected $job;
26+
27+
/**
28+
* Holds the raw body to prevent fetching the file from
29+
* the disk multiple times.
30+
*
31+
* @var string
32+
*/
33+
protected $cachedRawBody;
34+
35+
/**
36+
* The disk options for the job.
37+
*
38+
* @var array
39+
*/
40+
protected $diskOptions;
41+
42+
/**
43+
* Create a new job instance.
44+
*
45+
* @param \Illuminate\Container\Container $container
46+
* @param \Aws\Sqs\SqsClient $sqs
47+
* @param array $job
48+
* @param string $connectionName
49+
* @param string $queue
50+
*
51+
* @return void
52+
*/
53+
public function __construct(Container $container, SqsClient $sqs, array $job, $connectionName, $queue, array $diskOptions)
54+
{
55+
$this->sqs = $sqs;
56+
$this->job = $job;
57+
$this->queue = $queue;
58+
$this->container = $container;
59+
$this->connectionName = $connectionName;
60+
$this->diskOptions = $diskOptions;
61+
}
62+
63+
/**
64+
* Delete the job from the queue.
65+
*
66+
* @return void
67+
*/
68+
public function delete()
69+
{
70+
parent::delete();
71+
72+
if (Arr::get($this->diskOptions, 'cleanup') && $pointer = $this->resolvePointer()) {
73+
$this->resolveDisk()->delete($pointer);
74+
}
75+
}
76+
77+
/**
78+
* Get the raw body string for the job.
79+
*
80+
* @return string
81+
*/
82+
public function getRawBody()
83+
{
84+
if ($this->cachedRawBody) {
85+
return $this->cachedRawBody;
86+
}
87+
88+
if ($pointer = $this->resolvePointer()) {
89+
return $this->cachedRawBody = $this->resolveDisk()->get($pointer);
90+
}
91+
92+
return parent::getRawBody();
93+
}
94+
}

src/SqsDiskJob.php

Lines changed: 1 addition & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -2,95 +2,10 @@
22

33
namespace SimpleSoftwareIO\SqsDisk;
44

5-
use Aws\Sqs\SqsClient;
6-
use Illuminate\Support\Arr;
75
use Illuminate\Queue\Jobs\SqsJob;
8-
use Illuminate\Container\Container;
96
use Illuminate\Contracts\Queue\Job as JobContract;
107

118
class SqsDiskJob extends SqsJob implements JobContract
129
{
13-
use ResolvesPointers;
14-
15-
/**
16-
* The Amazon SQS client instance.
17-
*
18-
* @var \Aws\Sqs\SqsClient
19-
*/
20-
protected $sqs;
21-
22-
/**
23-
* The Amazon SQS job instance.
24-
*
25-
* @var array
26-
*/
27-
protected $job;
28-
29-
/**
30-
* Holds the raw body to prevent fetching the file from
31-
* the disk multiple times.
32-
*
33-
* @var string
34-
*/
35-
protected $cachedRawBody;
36-
37-
/**
38-
* The disk options for the job.
39-
*
40-
* @var array
41-
*/
42-
protected $diskOptions;
43-
44-
/**
45-
* Create a new job instance.
46-
*
47-
* @param \Illuminate\Container\Container $container
48-
* @param \Aws\Sqs\SqsClient $sqs
49-
* @param array $job
50-
* @param string $connectionName
51-
* @param string $queue
52-
*
53-
* @return void
54-
*/
55-
public function __construct(Container $container, SqsClient $sqs, array $job, $connectionName, $queue, array $diskOptions)
56-
{
57-
$this->sqs = $sqs;
58-
$this->job = $job;
59-
$this->queue = $queue;
60-
$this->container = $container;
61-
$this->connectionName = $connectionName;
62-
$this->diskOptions = $diskOptions;
63-
}
64-
65-
/**
66-
* Delete the job from the queue.
67-
*
68-
* @return void
69-
*/
70-
public function delete()
71-
{
72-
parent::delete();
73-
74-
if (Arr::get($this->diskOptions, 'cleanup') && $pointer = $this->resolvePointer()) {
75-
$this->resolveDisk()->delete($pointer);
76-
}
77-
}
78-
79-
/**
80-
* Get the raw body string for the job.
81-
*
82-
* @return string
83-
*/
84-
public function getRawBody()
85-
{
86-
if ($this->cachedRawBody) {
87-
return $this->cachedRawBody;
88-
}
89-
90-
if ($pointer = $this->resolvePointer()) {
91-
return $this->cachedRawBody = $this->resolveDisk()->get($pointer);
92-
}
93-
94-
return parent::getRawBody();
95-
}
10+
use SqsDiskBaseJob;
9611
}

src/SqsDiskServiceProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,7 @@ public function boot()
1313
{
1414
$manager = $this->app->make('queue');
1515
$manager->addConnector('sqs-disk', fn () => new SqsDiskConnector());
16+
17+
$this->app->extend('command.vapor.work', fn () => new VaporWorkCommand($this->app['queue.vaporWorker']));
1618
}
1719
}

src/VaporSqsDiskJob.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace SimpleSoftwareIO\SqsDisk;
4+
5+
use Laravel\Vapor\Queue\VaporJob;
6+
use Illuminate\Contracts\Queue\Job as JobContract;
7+
8+
class VaporSqsDiskJob extends VaporJob implements JobContract
9+
{
10+
use SqsDiskBaseJob;
11+
}

src/VaporWorkCommand.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace SimpleSoftwareIO\SqsDisk;
4+
5+
use Laravel\Vapor\Console\Commands\VaporWorkCommand as LaravelVaporWorkCommand;
6+
7+
class VaporWorkCommand extends LaravelVaporWorkCommand
8+
{
9+
/**
10+
* Marshal the job with the given message ID.
11+
*
12+
* @param array $message
13+
*
14+
* @return \Laravel\Vapor\Queue\VaporJob
15+
*/
16+
protected function marshalJob(array $message)
17+
{
18+
$normalizedMessage = $this->normalizeMessage($message);
19+
20+
$queue = $this->worker->getManager()->connection('sqs');
21+
22+
return new SqsDiskJob(
23+
$this->laravel,
24+
$queue->getSqs(),
25+
$normalizedMessage,
26+
'sqs',
27+
$this->queueUrl($message),
28+
config('queue.connections.sqs.disk_options')
29+
);
30+
}
31+
}

0 commit comments

Comments
 (0)