/** * Sets up forking. * * @param fork_daemon $fork_daemon Fork daemon object. * @return void */ public function setUpForking(fork_daemon $fork_daemon) { $this->fork_daemon = $fork_daemon; $this->fork_daemon->max_children_set($this->getNumChildren()); $this->fork_daemon->register_child_run(array($this, 'processWork')); $this->fork_daemon->register_parent_exit(array($this, 'cleanUp')); $this->fork_daemon->max_work_per_child_set($this->getItemCount()); }
/** * */ public function __destruct() { parent::__destruct(); }
<?php declare (ticks=1); require_once __DIR__ . '/../fork_daemon.php'; /* setup forking daemon */ $server = new fork_daemon(); $server->max_children_set(5); $server->max_work_per_child_set(3); $server->register_child_run("process_child_run"); $server->register_parent_child_exit("process_child_exit"); $server->register_logging("logger", fork_daemon::LOG_LEVEL_ALL); test_identifier(); function test_identifier() { global $server; $server->child_single_work_item_set(true); $server->max_work_per_child_set(1); echo "Adding 100 units of work\n"; /* add work */ $data_set = array(); for ($i = 0; $i < 100; $i++) { $data_set[] = $i; } shuffle($data_set); $data_set = array_chunk($data_set, 3); $i = 0; foreach ($data_set as $item) { $server->addwork($item, "IDn{$i}"); $i++; } echo "Processing work in non-blocking mode\n";
declare (ticks=1); namespace PHPMD\TextUI; use CodeClimate\PHPMD\Runner; error_reporting(E_ERROR | E_PARSE | E_NOTICE); date_default_timezone_set('UTC'); ini_set('memory_limit', -1); require_once __DIR__ . '/vendor/autoload.php'; require_once "JSONRenderer.php"; require_once "Runner.php"; require_once "Category.php"; use PHPMD\PHPMD; use PHPMD\RuleSetFactory; use PHPMD\Writer\StreamWriter; use PHPMD\Renderer\JSONRenderer; // obtain the config $config = json_decode(file_get_contents('/config.json'), true); // setup forking daemon $server = new \fork_daemon(); $server->max_children_set(3); $server->max_work_per_child_set(50); $server->store_result_set(true); $runner = new Runner($config, $server); $server->register_child_run(array($runner, "run")); $runner->queueDirectory("/code"); $server->process_work(true); foreach ($server->get_all_results() as $result_file) { echo file_get_contents($result_file); unlink($result_file); }
/** * This is the class constructor, initializes the object. * @access public */ public function __construct() { /* record pid of parent process */ self::$parent_pid = getmypid(); /* install signal handlers */ declare (ticks=1); pcntl_signal(SIGHUP, array(&$this, 'signal_handler_sighup')); pcntl_signal(SIGCHLD, array(&$this, 'signal_handler_sigchild')); pcntl_signal(SIGTERM, array(&$this, 'signal_handler_sigint')); pcntl_signal(SIGINT, array(&$this, 'signal_handler_sigint')); pcntl_signal(SIGALRM, SIG_IGN); pcntl_signal(SIGUSR2, SIG_IGN); pcntl_signal(SIGBUS, SIG_IGN); pcntl_signal(SIGPIPE, SIG_IGN); pcntl_signal(SIGABRT, SIG_IGN); pcntl_signal(SIGFPE, SIG_IGN); pcntl_signal(SIGILL, SIG_IGN); pcntl_signal(SIGQUIT, SIG_IGN); pcntl_signal(SIGTRAP, SIG_IGN); pcntl_signal(SIGSYS, SIG_IGN); /* add barracuda specific prefork functions (doesn't hurt anything) */ $this->parent_function_prefork = array('db_clear_connection_cache', 'memcache_clear_connection_cache'); }
/** * Update the last run time of the job after it is finished. * * @param int $pid The pid of the child exiting. * @return void */ public function parentChildExit($pid) { // Bucket should be named after the job class $class = $this->fork_daemon->getForkedChildren()[$pid]['bucket']; $this->jobFinished($class); }
<?php /** * Sample of passing in work and getting back results using a call at the * end of the child processing */ declare (ticks=1); require_once __DIR__ . '/../fork_daemon.php'; /* setup forking daemon */ $server = new fork_daemon(); $server->max_children_set(100); $server->max_work_per_child_set(3); $server->store_result_set(true); $server->register_child_run("process_child_run"); $server->register_parent_child_exit("process_child_exit"); $server->register_logging("logger", fork_daemon::LOG_LEVEL_ALL); // no callback with this method since we check results at the end test_nonblocking(); // since deferred results, check at the end $results = $server->get_all_results(); var_dump($results); echo "Sum: " . array_sum($results) . PHP_EOL; echo "Count: " . count($results) . PHP_EOL; function test_nonblocking() { global $server; echo "Adding 100 units of work\n"; /* add work */ $data_set = array(); for ($i = 0; $i < 100; $i++) { $data_set[] = $i;