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.
Installation:
composer require spatie/laravel-dynamic-servers
Publish the config file:
php artisan vendor:publish --provider="Spatie\DynamicServers\DynamicServersServiceProvider"
First Use Case:
Define a server in config/dynamic-servers.php:
'servers' => [
'test-server' => [
'command' => 'php artisan queue:work',
'timeout' => 60,
'max_attempts' => 3,
],
],
Start a server via Tinker or a command:
use Spatie\DynamicServers\Facades\DynamicServers;
DynamicServers::start('test-server');
Key Files:
config/dynamic-servers.php: Central configuration for all servers.app/Providers/DynamicServersServiceProvider.php: Extend or override default behavior.app/Console/Commands/: Custom commands for server management (optional).Server Lifecycle Management:
DynamicServers::start('server-name');
DynamicServers::stop('server-name');
if (DynamicServers::isRunning('server-name')) { ... }
Dynamic Configuration:
DynamicServers::setConfig('server-name', ['timeout' => 120]);
Event-Driven Integration:
ServerStarted, ServerStopped):
DynamicServers::started(function ($serverName) {
Log::info("Server $serverName started");
});
Queue Workers:
DynamicServers::start('queue-worker', ['command' => 'php artisan queue:work --queue=high']);
Artisan Command Integration:
class StartBackupServerCommand extends Command
{
protected $signature = 'backup:start';
public function handle()
{
DynamicServers::start('backup-server');
}
}
.env to toggle server availability:
DYNAMIC_SERVERS_ENABLED=true
ServerLogger to track custom metrics.Process Leaks:
DynamicServers::stop('server-name', true) to force-kill processes. Monitor with:
ps aux | grep 'your-command'
Configuration Overrides:
DynamicServers::stop('server-name');
DynamicServers::setConfig('server-name', ['timeout' => 120]);
DynamicServers::start('server-name');
Command Escaping:
escapeshellarg() or escapeshellcmd() in custom server definitions.Resource Limits:
config/dynamic-servers.php:
'log_level' => 'debug',
DynamicServers::getProcessInfo('server-name') to debug PID/state.timeout and max_attempts for flaky environments.Custom Server Classes:
Spatie\DynamicServers\Server to add pre/post-start logic:
class CustomServer extends Server
{
public function beforeStart()
{
Log::info("Preparing server...");
}
}
DynamicServersServiceProvider.php:
$this->app->bind('server', function () {
return new CustomServer();
});
Event Customization:
ServerFailed) in EventServiceProvider:
protected $listen = [
'Spatie\DynamicServers\Events\ServerFailed' => [
'App\Listeners\HandleServerFailure',
],
];
Storage Backends:
ServerStorage for custom storage (e.g., database) by binding the interface:
$this->app->bind(
Spatie\DynamicServers\ServerStorage::class,
App\CustomServerStorage::class
);
How can I help you explore Laravel packages today?