} $_REQUEST = array_merge($_REQUEST, $_GET); } // Set 'url' GET parameter if (isset($_SERVER['argv'][1])) { $_REQUEST['url'] = $_SERVER['argv'][1]; $_GET['url'] = $_SERVER['argv'][1]; } $function = function ($args) { echo date('[Y-m-d H:i:s]') . " - running gearman job with args: {$args}..."; $raw = @unserialize($args); if (isset($raw[0])) { $path = $raw[0]; $based = base64_encode($args); $cmd = $path . '/framework/cli-script.php'; echo "Executing against {$cmd}\n"; if (file_exists($cmd)) { $cmd = "php {$cmd} dev/tasks/GearmanJobTask gearman_data=" . escapeshellarg($based); echo "Doing {$cmd}\n"; $output = `{$cmd}`; echo $output . "\n"; echo "Job complete, memory used " . memory_get_usage() . "\n"; return; } } echo "Invalid job discarded\n"; }; $worker = new \Net\Gearman\Worker(); $worker->addServer(); $worker->addFunction('silverstripe_handler', $function); $worker->work();
<?php //Use laravel IOC to resolve Net\Gearman\Worker if it is available (for testing), otherwise create instance //directly utilizing composer autoload file if (class_exists('Illuminate\\Foundation\\Application')) { $worker = App::make('Net\\Gearman\\Worker'); } else { require __DIR__ . '/../../../../../vendor/autoload.php'; $worker = new Net\Gearman\Worker(); } $worker->addServer(); $worker->addFunction('amz_product_api_throttle', 'throttle'); while ($worker->work()) { } function throttle() { static $last_call_microtime = null; static $calls_persec = 1; $log = "\n" . gmdate("Y-m-d H:i:s") . ': amz_product_api_throttle, last_call_microtime-> ' . $last_call_microtime . ', current_microtime-< ' . microtime(true); //throttle mechanism if ($last_call_microtime !== null) { $micro_diff = microtime(true) - $last_call_microtime; if ($micro_diff < 1 / $calls_persec) { $log .= ', request throttled'; usleep((1 / $calls_persec - $micro_diff) * 1000000); } } $last_call_microtime = microtime(true); //echo $log; // /*log throttle*/ // $gearman_client_logger = new GearmanClient();