spatie/laravel-dynamic-servers
Dynamically spin up and destroy servers from Laravel to handle variable queue workloads. Uses provider snapshots as templates and lets you determine server count (e.g., based on Horizon wait times) so extra workers are created automatically and removed when no longer needed.
You can install the package via composer:
composer require spatie/laravel-dynamic-servers
Next, you can run the installer with:
php artisan dynamic-servers:install
This command will:
dynamic_servers table in your database. This table is used to keep track of all servers created by the package.dynamic-servers.php config file in the /config directory of yourThis is how the config file looks like (there are more install instructions after the contents of the config file).
<?php
return [
'providers' => [
'up_cloud' => [
'class' => Spatie\DynamicServers\ServerProviders\UpCloud\UpCloudServerProvider::class,
'maximum_servers_in_account' => 20,
'options' => [
'username' => env('UP_CLOUD_USER_NAME'),
'password' => env('UP_CLOUD_PASSWORD'),
'disk_image' => env('UP_CLOUD_DISK_IMAGE_UUID'),
],
],
],
/*
* Overriding these actions will give you fine-grained control over
* how we handle your servers. In most cases, it's fine to use
* the defaults.
*/
'actions' => [
'generate_server_name' => Spatie\DynamicServers\Actions\GenerateServerNameAction::class,
'start_server' => Spatie\DynamicServers\Actions\StartServerAction::class,
'stop_server' => Spatie\DynamicServers\Actions\StopServerAction::class,
'find_servers_to_stop' => Spatie\DynamicServers\Actions\FindServersToStopAction::class,
'reboot_server' => Spatie\DynamicServers\Actions\RebootServerAction::class,
],
/*
* Overriding these jobs will give you fine-grained control over
* how we create, stop, delete and reboot your servers. In most cases,
* it's fine to use the defaults.
*/
'jobs' => [
'create_server' => Spatie\DynamicServers\Jobs\CreateServerJob::class,
'verify_server_started' => Spatie\DynamicServers\Jobs\VerifyServerStartedJob::class,
'stop_server' => Spatie\DynamicServers\Jobs\StopServerJob::class,
'verify_server_stopped' => Spatie\DynamicServers\Jobs\VerifyServerStoppedJob::class,
'delete_server' => Spatie\DynamicServers\Jobs\DeleteServerJob::class,
'verify_server_deleted' => Spatie\DynamicServers\Jobs\VerifyServerDeletedJob::class,
'reboot_server' => Spatie\DynamicServers\Jobs\RebootServerJob::class,
'verify_server_rebooted' => Spatie\DynamicServers\Jobs\VerifyServerRebootedJob::class,
],
/*
* When we detect that a server is taking longer than this amount of minutes
* to start or stop, we'll mark it has hanging, and will not try to use it anymore
*
* The `ServerHangingEvent` will be fired, that you can use to send yourself a notification,
* or manually take the necessary actions to start/stop it.
*/
'mark_server_as_hanging_after_minutes' => 10,
/*
* The dynamic_servers table holds records of all dynamic servers.
*
* Using Laravel's prune command all stopped servers will be deleted
* after the given amount of days.
*/
'prune_stopped_servers_from_local_db_after_days' => 7,
'throw_exception_when_hitting_maximum_server_limit' => false,
];
This package will keep track of all dynamic servers in the dynamic_servers table. To create that table, run these
commands:
php artisan migrate
This package uses queued jobs to start and stop servers. Make sure, you have configured one of the available queuing mechanisms in your Laravel app.
You should register a couple of commands in your kernel schedule.
The MonitorDynamicServersCommand command will take care of creating and destroying servers.
The HandleHangingServersCommand command will detect any servers that are starting and stopping, but never did start or stop completely.
To clean up records of stopped servers in the dynamic_servers table, you should add Laravel's model:prune command. If you already have this command in your schedule, add the \Spatie\DynamicServers\Models\Server::class model to its options.
You should add the commands to your schedule, and let them run every minute.
// in app/Console/Kernel.php
use Spatie\DynamicServers\Commands\MonitorDynamicServersCommand;
use Spatie\DynamicServers\Commands\DetectHangingServersCommand;
use Spatie\DynamicServers\Models\Server;
protected function schedule(Schedule $schedule)
{
$schedule->command(MonitorDynamicServersCommand::class)->everyMinute();
$schedule->command(DetectHangingServersCommand::class)->everyMinute();
$schedule->command('model:prune', [
'--model' => [Server::class],
])->daily();
}
How can I help you explore Laravel packages today?