Skip to content

[3.x] Isolating Action Execution #105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jan 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions config/actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
|
*/

'table' => 'migration_actions',
'table' => 'migration_actions',

/*
|--------------------------------------------------------------------------
Expand All @@ -35,7 +35,7 @@
|
*/

'path' => base_path('actions'),
'path' => base_path('actions'),

/*
|--------------------------------------------------------------------------
Expand All @@ -56,5 +56,5 @@
|
*/

'exclude' => null,
'exclude' => null,
];
26 changes: 19 additions & 7 deletions docs/how-to-use/running.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ bar/2022_10_14_000003_test3 # 3
2022_10_14_000004_test4 # 4
```

## Isolating Action Execution

If you are deploying your application across multiple servers and running migrations as part of your deployment process, you likely do not want two servers attempting to migrate
the database at the same time. To avoid this, you may use the `isolated` option when invoking the `migrate:actions` command.

When the `isolated` option is provided, Laravel will acquire an atomic lock using your application's cache driver before attempting to run your migrations. All other attempts to
run the `migrate:actions` command while that lock is held will not execute; however, the command will still exit with a successful exit status code:

```bash
php artisan migrate:actions --isolated
```

## Split Launch Option

Sometimes it becomes necessary to launch actions separately, for example, to notify about the successful deployment of a project.
Expand All @@ -39,7 +51,7 @@ For backwards compatibility, the `before` parameter is set to `true` by default,
```php
use DragonCode\LaravelActions\Action;

return new class () extends Action
return new class extends Action
{
protected $before = false;

Expand Down Expand Up @@ -95,7 +107,7 @@ To do this, override the `$once` variable in the action file:
```php
use DragonCode\LaravelActions\Action;

return new class () extends Action
return new class extends Action
{
protected $once = false;

Expand Down Expand Up @@ -123,7 +135,7 @@ For this you can use the `$environment` parameter:
```php
use DragonCode\LaravelActions\Action;

return new class () extends Action
return new class extends Action
{
/** @var string|array|null */
protected $environment = 'production';
Expand All @@ -140,7 +152,7 @@ You can also specify multiple environment names:
```php
use DragonCode\LaravelActions\Action;

return new class () extends Action
return new class extends Action
{
/** @var string|array|null */
protected $environment = ['testing', 'staging'];
Expand All @@ -163,7 +175,7 @@ For this you can use the `$except_environment` parameter:
```php
use DragonCode\LaravelActions\Action;

return new class () extends Action
return new class extends Action
{
/** @var string|array|null */
protected $exceptEnvironment = 'production';
Expand All @@ -180,7 +192,7 @@ You can also specify multiple environment names:
```php
use DragonCode\LaravelActions\Action;

return new class () extends Action
return new class extends Action
{
/** @var string|array|null */
protected $exceptEnvironment = ['testing', 'staging'];
Expand All @@ -205,7 +217,7 @@ will reduce the time it takes to create the action.
```php
use DragonCode\LaravelActions\Action;

return new class () extends Action
return new class extends Action
{
protected $transactions = true;

Expand Down
35 changes: 35 additions & 0 deletions src/Concerns/Isolatable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace DragonCode\LaravelActions\Concerns;

use DragonCode\LaravelActions\Constants\Options;
use DragonCode\LaravelActions\Services\Mutex;

trait Isolatable
{
protected function isolationMutex(): Mutex
{
return $this->getLaravel()->make(Mutex::class);
}

protected function isolatedStatusCode(): int
{
if ($isolate = $this->getIsolateOption()) {
return is_numeric($isolate) ? $isolate : self::SUCCESS;
}

return self::SUCCESS;
}

protected function getIsolateOption(): int|bool
{
return $this->hasIsolateOption() ? $this->option(Options::ISOLATED) : false;
}

protected function hasIsolateOption(): bool
{
return $this->hasOption(Options::ISOLATED) && $this->option(Options::ISOLATED);
}
}
2 changes: 2 additions & 0 deletions src/Concerns/Optionable.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ trait Optionable
Options::CONNECTION,
Options::FORCE,
Options::MUTE,
Options::ISOLATED,
];

protected function configure(): void
Expand Down Expand Up @@ -49,6 +50,7 @@ protected function availableOptions(): array
[Options::REALPATH, null, InputOption::VALUE_NONE, 'Indicate any provided action file paths are pre-resolved absolute path'],
[Options::STEP, null, InputOption::VALUE_OPTIONAL, 'Force the actions to be run so they can be rolled back individually'],
[Options::MUTE, null, InputOption::VALUE_NONE, 'Turns off the output of informational messages'],
[Options::ISOLATED, null, InputOption::VALUE_OPTIONAL, 'Do not run the actions command if another instance of the actions command is already running', false],
];
}

Expand Down
26 changes: 24 additions & 2 deletions src/Console/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@
namespace DragonCode\LaravelActions\Console;

use DragonCode\LaravelActions\Concerns\ConfirmableTrait;
use DragonCode\LaravelActions\Concerns\Isolatable;
use DragonCode\LaravelActions\Concerns\Optionable;
use DragonCode\LaravelActions\Processors\Processor;
use DragonCode\LaravelActions\Values\Options as OptionsDto;
use Illuminate\Console\Command as BaseCommand;
use Illuminate\Container\Container;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

abstract class Command extends BaseCommand
{
use ConfirmableTrait;
use Isolatable;
use Optionable;

protected Processor|string $processor;
Expand All @@ -24,10 +28,28 @@ public function handle(): int
$this->resolveProcessor()->handle();
$this->forgetProcessor();

return 0;
return self::SUCCESS;
}

return 1;
return self::FAILURE;
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
if ($this->getIsolateOption() !== false && ! $this->isolationMutex()->create($this)) {
$this->comment(sprintf('The [%s] command is already running.', $this->getName()));

return $this->isolatedStatusCode();
}

try {
return parent::execute($input, $output);
}
finally {
if ($this->getIsolateOption() !== false) {
$this->isolationMutex()->forget($this);
}
}
}

protected function resolveProcessor(): Processor
Expand Down
1 change: 1 addition & 0 deletions src/Console/Fresh.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ class Fresh extends Command
Options::PATH,
Options::REALPATH,
Options::MUTE,
Options::ISOLATED,
];
}
1 change: 1 addition & 0 deletions src/Console/Migrate.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ class Migrate extends Command
Options::PATH,
Options::REALPATH,
Options::MUTE,
Options::ISOLATED,
];
}
1 change: 1 addition & 0 deletions src/Console/Refresh.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ class Refresh extends Command
Options::PATH,
Options::REALPATH,
Options::MUTE,
Options::ISOLATED,
];
}
1 change: 1 addition & 0 deletions src/Console/Reset.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ class Reset extends Command
Options::PATH,
Options::REALPATH,
Options::MUTE,
Options::ISOLATED,
];
}
1 change: 1 addition & 0 deletions src/Console/Rollback.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ class Rollback extends Command
Options::REALPATH,
Options::STEP,
Options::MUTE,
Options::ISOLATED,
];
}
2 changes: 2 additions & 0 deletions src/Constants/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class Options

public const FORCE = 'force';

public const ISOLATED = 'isolated';

public const MUTE = 'mute';

public const NAME = 'name';
Expand Down
47 changes: 47 additions & 0 deletions src/Services/Mutex.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace DragonCode\LaravelActions\Services;

use Carbon\CarbonInterval;
use DragonCode\LaravelActions\Console\Command;
use Illuminate\Contracts\Cache\Factory as Cache;
use Illuminate\Contracts\Cache\Repository;

class Mutex
{
protected ?string $store = null;

public function __construct(
protected Cache $cache
) {
}

public function create(Command $command): bool
{
return $this->store()->add($this->name($command), true, $this->ttl());
}

public function forget(Command $command): void
{
$this->store()->forget(
$this->name($command)
);
}

protected function store(): Repository
{
return $this->cache->store($this->store);
}

protected function ttl(): CarbonInterval
{
return CarbonInterval::hour();
}

protected function name(Command $command): string
{
return 'framework' . DIRECTORY_SEPARATOR . 'action-' . $command->getName();
}
}
Loading