You can use the ensure($numberOfServerNeeded)
method to make sure that the given number of servers are available.
use Spatie\DynamicServers\Facades\DynamicServers;
DynamicServers::ensure(5);
There's also a determineServerCount
function that accepts a callable. That callable will be executed each minute by the MonitorDynamicServersCommand
you scheduled when configuring the package.
Here's how you could use ensure
with the callable passed to determineServerCount
.
use Laravel\Horizon\WaitTimeCalculator;
use Spatie\DynamicServers\Facades\DynamicServers;
use Spatie\DynamicServers\Support\DynamicServersManager;
DynamicServers::determineServerCount(function(DynamicServersManager $servers) {
$waitTimeInMinutes = app(WaitTimeCalculator::class)->calculate('default');
$numberOfServersNeeded = round($waitTimeInMinutes / 10);
$servers->ensure($numberOfServersNeeded);
});
In addition to using determineServerCount
, you could also listen for Horizon's LongWaitDetected
event. This way, servers will be started immediately when your queue grows long, and we don't have to wait until the schedule is called.
use Illuminate\Support\Facades\Event;
use Laravel\Horizon\Events\LongWaitDetected;
use Spatie\DynamicServers\Facades\DynamicServers;
Event::listen(function (LongWaitDetected $event) {
$waitTimeInMinutes = app(WaitTimeCalculator::class)->calculate('default');
$numberOfServersNeeded = round($waitTimeInMinutes / 10);
DynamicServers::ensure($numberOfServersNeeded);
});