PHP code example of spatie / laravel-dynamic-servers

1. Go to this page and download the library: Download spatie/laravel-dynamic-servers library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

spatie / laravel-dynamic-servers example snippets


// typically, in a service provider

use Laravel\Horizon\WaitTimeCalculator;
use Spatie\DynamicServers\Facades\DynamicServers;
use Spatie\DynamicServers\Support\DynamicServersManager;

/*
 * The package will call the closure passed 
 * to `determineServerCount` every minute
 */
DynamicServers::determineServerCount(function(DynamicServersManager $servers) {
   /*
    * First, we'll calculate the number of servers needed. 
    * 
    * In this example, we will take a look at Horizon's 
    * reported waiting time. Of course, in your project you can 
    * calculate the number of servers needed however you want.    
    */
    $waitTimeInMinutes = app(WaitTimeCalculator::class)->calculate('default');
    $numberOfServersNeeded = round($waitTimeInMinutes / 10);

   /*
    * Next, we will pass the number of servers needed to the `ensure` method.
    * 
    * If there currently are less that that number of servers available,
    * the package will start new ones.
    * 
    * If there are currently more than that number of servers running,
    *  the package will stop a few servers.
    */
    $servers->ensure($numberOfServersNeeded);
});