Skip to content

Commit 1f19287

Browse files
Added asynchronous actions
1 parent b96c61c commit 1f19287

File tree

17 files changed

+297
-17
lines changed

17 files changed

+297
-17
lines changed

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,10 @@
7171
"autoload": {
7272
"psr-4": {
7373
"DragonCode\\LaravelActions\\": "src"
74-
}
74+
},
75+
"classmap": [
76+
"polyfill"
77+
]
7578
},
7679
"autoload-dev": {
7780
"psr-4": {

config/actions.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
|
2525
*/
2626

27-
'table' => 'actions',
27+
'table' => 'actions',
2828

2929
/*
3030
|--------------------------------------------------------------------------
@@ -35,7 +35,7 @@
3535
|
3636
*/
3737

38-
'path' => base_path('actions'),
38+
'path' => base_path('actions'),
3939

4040
/*
4141
|--------------------------------------------------------------------------
@@ -56,5 +56,25 @@
5656
|
5757
*/
5858

59-
'exclude' => null,
59+
'exclude' => null,
60+
61+
/*
62+
|--------------------------------------------------------------------------
63+
| Queue
64+
|--------------------------------------------------------------------------
65+
|
66+
| This option specifies the queue settings that will process
67+
| asynchronous actions.
68+
|
69+
*/
70+
71+
'queue' => [
72+
// Connection name
73+
74+
'connection' => env('ACTIONS_QUEUE_CONNECTION', env('QUEUE_CONNECTION', 'sync')),
75+
76+
// Queue name
77+
78+
'name' => env('ACTIONS_QUEUE_NAME'),
79+
],
6080
];

docs/how-to-use/running.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,3 +229,33 @@ return new class extends Action
229229
}
230230
};
231231
```
232+
233+
## Asynchronous Call
234+
235+
In some cases, it becomes necessary to execute actions in an asynchronous manner without delaying the deployment process.
236+
237+
To do this, you need to override the `$async` property in the action class:
238+
239+
```php
240+
use DragonCode\LaravelActions\Action;
241+
242+
return new class extends Action
243+
{
244+
protected bool $async = true;
245+
246+
public function __invoke(): void
247+
{
248+
// some code
249+
}
250+
};
251+
```
252+
253+
In this case, the action file that defines this parameter will run asynchronously using the `DragonCode\LaravelActions\Jobs\ActionJob` class.
254+
255+
The name of the connection and queue can be changed through the [settings](https://github.com/TheDragonCode/laravel-actions/tree/main/config).
256+
257+
::: Info
258+
We remind you that in this case the [queuing system](https://laravel.com/docs/queues) must work in your application.
259+
260+
Using Laravel version 8.37 and above, checking for the [uniqueness](https://laravel.com/docs/10.x/queues#unique-jobs) of the execution is supported.
261+
:::

polyfill/ShouldBeUnique.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Illuminate\Contracts\Queue;
6+
7+
if (! interface_exists(ShouldBeUnique::class)) {
8+
interface ShouldBeUnique
9+
{
10+
}
11+
}

src/Action.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ abstract class Action extends Migration
5757
*/
5858
protected bool $before = true;
5959

60+
/**
61+
* Defines whether the action will run synchronously or asynchronously.
62+
*
63+
* @var bool
64+
*/
65+
protected bool $async = false;
66+
6067
/**
6168
* Determines the type of launch of the action.
6269
*
@@ -130,6 +137,16 @@ public function hasBefore(): bool
130137
return $this->before;
131138
}
132139

140+
/**
141+
* Defines whether the action will run synchronously or asynchronously.
142+
*
143+
* @return bool
144+
*/
145+
public function isAsync(): bool
146+
{
147+
return $this->async;
148+
}
149+
133150
/**
134151
* Method to be called when the job completes successfully.
135152
*

src/Concerns/Optionable.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ protected function availableOptions(): array
5151
[Options::STEP, null, InputOption::VALUE_OPTIONAL, 'Force the actions to be run so they can be rolled back individually'],
5252
[Options::MUTE, null, InputOption::VALUE_NONE, 'Turns off the output of informational messages'],
5353
[Options::ISOLATED, null, InputOption::VALUE_OPTIONAL, 'Do not run the actions command if another instance of the actions command is already running', false],
54+
[Options::SYNC, null, InputOption::VALUE_OPTIONAL, 'Makes all actions run synchronously', false],
5455
];
5556
}
5657

src/Console/Actions.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@ class Actions extends Command
2424
Options::REALPATH,
2525
Options::MUTE,
2626
Options::ISOLATED,
27+
Options::SYNC,
2728
];
2829
}

src/Constants/Options.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,6 @@ class Options
2323
public const REALPATH = 'realpath';
2424

2525
public const STEP = 'step';
26+
27+
public const SYNC = 'sync';
2628
}

src/Jobs/ActionJob.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DragonCode\LaravelActions\Jobs;
6+
7+
use DragonCode\LaravelActions\Constants\Names;
8+
use DragonCode\LaravelActions\Constants\Options;
9+
use Illuminate\Bus\Queueable;
10+
use Illuminate\Contracts\Queue\ShouldBeUnique;
11+
use Illuminate\Contracts\Queue\ShouldQueue;
12+
use Illuminate\Foundation\Bus\Dispatchable;
13+
use Illuminate\Queue\InteractsWithQueue;
14+
use Illuminate\Queue\SerializesModels;
15+
use Illuminate\Support\Facades\Artisan;
16+
17+
class ActionJob implements ShouldQueue, ShouldBeUnique
18+
{
19+
use Dispatchable;
20+
use InteractsWithQueue;
21+
use Queueable;
22+
use SerializesModels;
23+
24+
public function __construct(
25+
public string $filename
26+
) {
27+
$this->setQueueConnection();
28+
$this->setQueueName();
29+
}
30+
31+
public function handle(): void
32+
{
33+
Artisan::call(Names::ACTIONS, [
34+
'--' . Options::PATH => $this->filename,
35+
'--' . Options::SYNC => true,
36+
]);
37+
}
38+
39+
public function uniqueId(): string
40+
{
41+
return $this->filename;
42+
}
43+
44+
protected function setQueueConnection(): void
45+
{
46+
$this->onConnection(config('actions.queue.connection'));
47+
}
48+
49+
protected function setQueueName(): void
50+
{
51+
$this->onQueue(config('actions.queue.name'));
52+
}
53+
}

src/Notifications/Beautiful.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ public function task(string $description, Closure $task): void
3636
{
3737
if ($this->canSpeak()) {
3838
$this->components()->task($description, $task);
39-
39+
4040
return;
4141
}
42-
42+
4343
$task();
4444
}
4545

0 commit comments

Comments
 (0)