Пример #1
0
<?php

define('APPROOT', dirname(__DIR__));
require_once APPROOT . '/src/GAS/includes/autoloader.inc.php';
\GAS\config\GasConfig::instance()->setWorkerStarterScript(APPROOT . '/scripts/startWorker.php');
\GAS\config\GasConfig::instance()->loadFromJson(APPROOT . '/examples/config.json');
$gasManager = new \GAS\GasManager();
// Use the simple file-based logger (not ideal due to contention on concurrent file access but good enough for demo purposes)
// You'd use a different implemenation in production.
$gasManager->setJobLogger(new \GAS\FileJobLogger());
$jobs = array();
echo "\n\nSubmitting jobs to the queue...\n";
for ($i = 0; $i < 5; $i++) {
    $jobHandle = $gasManager->doBackground('calcPi', '');
    $jobs[$jobHandle] = array();
}
echo "\n\nSubmitted all the jobs, now waiting until they are all complete...\n";
$knownJobsRemaining = true;
while ($knownJobsRemaining) {
    $jobsStillToProcess = 0;
    $knownJobsRemaining = false;
    foreach ($jobs as $jobHandle => $values) {
        if ($gasManager->isJobKnown($jobHandle)) {
            $knownJobsRemaining = true;
            $jobsStillToProcess++;
        }
    }
    if ($jobsStillToProcess) {
        echo "\n{$jobsStillToProcess} jobs still to process...";
        sleep(10);
    } else {
Пример #2
0
 function run()
 {
     GasConfig::instance()->loadFromJson($this->configJson);
     $arrWorker = GasConfig::instance()->getWorkersConfig();
     $jobQueue = new \GAS\models\JobQueue();
     $status = $jobQueue->getStatus();
     // Check CPU load to determine whether it's safe to start any new workers
     $arrAverageLoad = sys_getloadavg();
     $averageLoad1 = round($arrAverageLoad[0], 2);
     if ($this->debug) {
         echo "\nGAS-PHP monitorQueue.php";
         echo "\n\nAverage load of last minute: {$averageLoad1}";
         echo "\n\nConfigured worker functions:-";
         if (empty($arrWorker)) {
             echo "\n - no workers defined via GasConfig->registerWorkerClass()";
         } else {
             foreach ($arrWorker as $functionName => $values) {
                 $className = $values['className'];
                 echo "\n - " . $functionName . ' (' . $className . ')';
             }
         }
         echo "\n\nGearman queue:-";
         echo "\n\n" . str_pad('Function', 50) . str_pad('Queued', 10) . str_pad('Running', 10) . str_pad('Workers', 10) . "\n";
     }
     $gasManager = new GasManager();
     foreach ($status as $s) {
         $functionName = $s['function'];
         if ($functionName == '.') {
             continue;
         }
         echo "\n" . str_pad($s['function'], 50) . str_pad($s['queued'], 10) . str_pad($s['running'], 10) . str_pad($s['workers'], 10);
         $queued = $s['queued'];
         $workers = $s['workers'];
         /*
          * If there are more things on the queue than there are current workers then launch a new
          * worker, up to the max number of workers.
          */
         if ($queued > 0 && $queued > $workers) {
             $workerSettings = GasConfig::instance()->getWorkerSettingsForFunctionName($functionName);
             if (isset($workerSettings['maxWorkers'])) {
                 $maxWorkers = $workerSettings['maxWorkers'];
             } else {
                 $maxWorkers = GasConfig::instance()->getMaxWorkers();
             }
             if ($workers == $maxWorkers) {
                 /*
                  * We've hit the max worker count - no more workers can be started...
                  */
                 echo ' - Max worker count reached (' . $maxWorkers . ')';
             } elseif ($averageLoad1 > 1.5 && $workers > 0) {
                 /*
                  * If there is already a worker running and current load is over a threshold
                  * then don't start another worker...
                  */
                 echo ' - Not starting new worker, load = ' . $averageLoad1;
             } else {
                 /*
                  * Start a new worker, if configuration allows...
                  */
                 if (GasConfig::instance()->isMonitorAllowedToStartNewWorkers()) {
                     echo ' - Starting new worker';
                     $gasManager->startWorker($functionName);
                 } else {
                     echo ' - Monitor is not allowed to start new workers';
                 }
             }
         }
     }
     echo "\n\n";
 }
Пример #3
0
<?php

/*
 * Monitor the Gearman queue to see if the workers need auto-scaling
 */
define('APPROOT', dirname(__DIR__));
require_once APPROOT . '/vendor/autoload.php';
require APPROOT . '/src/GAS/includes/autoloader.inc.php';
$options = getopt('c:d:');
$configJson = null;
if (isset($options['c']) && !empty($options['c'])) {
    $configJson = $options['c'];
} else {
    echo "\n\nWARNING: no -c config option specified, using demo configuration\n\n";
    $configJson = APPROOT . '/examples/config.json';
}
$workerStarterScript = APPROOT . '/scripts/startWorker.php';
\GAS\config\GasConfig::instance()->setWorkerStarterScript($workerStarterScript);
\GAS\config\GasConfig::instance()->loadFromJson($configJson);
$queueMonitor = new \GAS\QueueMonitor($configJson, true);
$queueMonitor->run();
Пример #4
0
 protected function getGearmanClient()
 {
     if ($this->gearmanClient == null) {
         $this->gearmanClient = new \GearmanClient();
         $this->gearmanClient->addServer(GasConfig::instance()->getGearmanServerHost(), GasConfig::instance()->getGearmanServerPort());
         //$this->gearmanClient->setTimeout(1000);
         //TODO Try to get various callback handlers to work
         //NB I think the callbacks only work for non-background jobs?
         $this->gearmanClient->setCreatedCallback(array($this, 'cb_created'));
         $this->gearmanClient->setExceptionCallback(array($this, "taskException"));
         $this->gearmanClient->setFailCallback(array($this, "taskFail"));
     }
     return $this->gearmanClient;
 }
Пример #5
0
define('APPROOT', dirname(__DIR__));
require_once APPROOT . '/src/GAS/includes/autoloader.inc.php';
$worker = new GearmanWorker();
$worker->addServer();
$workerClass = @$argv[1];
$jsonConfigFile = @$argv[2];
if (empty($workerClass)) {
    die("\nWorker class is required\n\n");
}
if (!class_exists($workerClass)) {
    die("\nWorker class cannot be found:{$workerClass}\n\n");
}
if (!empty($jsonConfigFile)) {
    if (file_exists($jsonConfigFile)) {
        \GAS\config\GasConfig::instance()->loadFromJson($jsonConfigFile);
    } else {
        die("\nJSON config file is specified but cannot be found: {$jsonConfigFile}\n\n");
    }
}
/*
 * Get the functions to register with Gearman
 */
$gasWorker = new $workerClass();
$arrWorkerFunctions = $gasWorker->getFunctions();
foreach ($arrWorkerFunctions as $functionName) {
    $worker->addFunction($functionName, array($gasWorker, $functionName));
}
$maxJobs = $gasWorker->getMaxJobs();
$workerLifetimeMins = $gasWorker->getWorkerLifetimeMins();
//$worker->setId('worker_' . uniqid(true));