/** * Constructor method * * Prepare extender environment, do checks and fire extender.ready event */ public final function __construct() { // check if extender is running from cli if (Checks::cli() === false) { echo "Extender runs only in php-cli, exiting"; self::end(1); } // setup default timezone (in daemon mode, timezone warning may break extender) $default_timezone = ini_get('date.timezone'); if (empty($default_timezone)) { date_default_timezone_set(defined('EXTENDER_TIMEZONE') ? EXTENDER_TIMEZONE : 'Europe/Rome'); } $this->timestamp_absolute = microtime(true); $this->color = new Console_Color2(); // get command line options (vsdh) list($this->verbose_mode, $this->debug_mode, $this->summary_mode, $this->daemon_mode, $help_mode) = self::getCommandlineOptions(); if ($help_mode) { self::showHelp($this->color); self::end(0); } $this->logger = ExtenderLogger::create($this->verbose_mode, $this->debug_mode); // do checks $check_constants = Checks::constants(); if ($check_constants !== true) { $this->logger->critical($check_constants); self::end(1); } if (Checks::signals() === false and $this->daemon_mode) { $this->logger->critical("Extender cannot run in daemon mode without PHP Process Control Extensions"); self::end(1); } if (Checks::database() === false) { $this->logger->critical("Extender database not available, exiting"); self::end(1); } $this->tasks = TasksTable::load($this->logger); $this->events = Events::load($this->logger); // setup extender parameters $this->max_result_bytes_in_multithread = defined('EXTENDER_MAX_RESULT_BYTES') ? filter_var(EXTENDER_MAX_RESULT_BYTES, FILTER_VALIDATE_INT) : 2048; $this->max_childs_runtime = defined('EXTENDER_MAX_CHILDS_RUNTIME') ? filter_var(EXTENDER_MAX_CHILDS_RUNTIME, FILTER_VALIDATE_INT) : 600; $this->multithread_mode = defined('EXTENDER_MULTITHREAD_ENABLED') ? filter_var(EXTENDER_MULTITHREAD_ENABLED, FILTER_VALIDATE_BOOLEAN) : false; // if in daemon mode, remember parent pid, setup lock and register signal handlers if ($this->daemon_mode) { $this->parent_pid = posix_getpid(); Lock::register($this->parent_pid); $this->adjustNiceness(); if (Checks::signals()) { $this->registerSignals(); } } // init the runner $this->runner = new JobsRunner($this->logger, $this->getMultithreadMode(), $this->max_result_bytes_in_multithread, $this->max_childs_runtime); $this->logger->notice("Extender ready"); // store initial status and queue information Status::dump($this->timestamp_absolute, $this->parent_pid, $this->completed_processes, $this->failed_processes, $this->paused); Queue::dump(0, 0); // we are ready to go! }
public function testDump() { $result = \Comodojo\Extender\Queue::dump($this->running, $this->queued); $this->assertNotFalse($result); $this->assertGreaterThan(1, $result); }
/** * Execute job(s) in current queue * * @return array An array of completed processes */ public final function run() { // if jobs > concurrent jobs, create the queue if ($this->multithread and defined("EXTENDER_MAX_CHILDS") and sizeof($this->jobs) > EXTENDER_MAX_CHILDS and EXTENDER_MAX_CHILDS != 0) { $this->queued_processes = sizeof($this->jobs); // split jobs in chunks $this->queued_chunks = array_chunk($this->jobs, EXTENDER_MAX_CHILDS, true); // exec chunks, one at time foreach ($this->queued_chunks as $chunk) { $this->queued_processes = $this->queued_processes - sizeof($chunk); Queue::dump(sizeof($chunk), $this->queued_processes); $this->forker($chunk); if ($this->multithread) { $this->logger->info("Extender forked " . sizeof($this->forked_processes) . " process(es) in the running queue", $this->forked_processes); } $this->catcher(); $this->forked_processes = array(); } } else { Queue::dump(sizeof($this->jobs), 0); $this->forker($this->jobs); if ($this->multithread) { $this->logger->info("Extender forked " . sizeof($this->forked_processes) . " process(es) in the running queue", $this->forked_processes); } $this->catcher(); } // Dump the end queue status Queue::dump(sizeof($this->running_processes), $this->queued_processes); return $this->completed_processes; }