public function index() { // $str = 'php '.DOCROOT.KOHANA.' application_queue/run'; // exec( $str.' 2>&1', $buffer ); // var_dump( $buffer ); $db = new Database(); $r = $db->select('*')->from(Kohana::config('queue.table_name'))->orderby('priority', 'desc')->limit(1)->get(); if ($r->count()) { echo 'execute: '; $queue = new Task_Model(); $queue->scan()->execute(); } echo 'done'; exit; }
public function run() { ini_set("max_execution_time", "0"); ini_set("max_input_time", "0"); set_time_limit(0); pcntl_signal(SIGCHLD, array($this, 'sig_handler')); pcntl_signal(SIGTERM, array($this, 'sig_handler')); pcntl_signal(SIGHUP, array($this, 'sig_handler')); declare (ticks=1); $pid = pcntl_fork(); // fork if ($pid == -1) { Kohana::log('error', 'Could not fork.'); exit; } else { if ($pid) { Kohana::log('debug', 'Forked Once'); exit; } else { // Write the log to ensure no memory issues Kohana::log_save(); $pid = pcntl_fork(); if ($pid == -1) { Kohana::log('error', 'Could not fork.'); exit; } elseif ($pid) { // We don't do anything Kohana::log('error', 'Parent.'); } else { Kohana::log('debug', 'Starting the scanning'); $task_model = new Task_Model(); while (!$this->sigterm) { // Only scan based on our config item usleep(Kohana::config('queue.scan_delay')); $this->connect(); if ($this->sighup) { // We need to reload the config Kohana::config_clear('queue'); Kohana::config_clear('database'); Kohana::config_clear('log'); Kohana::config_clear('routes'); Kohana::config_clear('core'); $this->sighup = FALSE; } $sql = 'SELECT * FROM `' . Kohana::config('queue.table_name') . '` ORDER BY `priority` DESC LIMIT 1'; $result = mysql_query($sql, $this->connection); $num_rows = is_resource($result) ? mysql_num_rows($result) : 0; // Scan the queue if (count($this->pids) < Kohana::config('queue.parallel_operations') and $num_rows > 0) { // Write the log to ensure no memory issues Kohana::log_save(); $pid = pcntl_fork(); if ($pid == -1) { Kohana::log('error', 'Could not spawn child task process.'); exit; } elseif ($pid) { // Add the child's PID to the running list $this->pids[$pid] = time(); } else { $queue = new Task_Model(); $queue->scan()->execute(); exit; } mysql_free_result($result); } else { // var_dump( $this->pids ); // var_dump( Kohana::config('queue.parallel_operations') ); // var_dump( $num_rows ); // var_dump( $result ); // var_dump( $sql ); // var_dump( $this->connection ); // var_dump( "mysql_connect(".Kohana::config('database.default.connection.host').", ".Kohana::config('database.default.connection.user').", ".Kohana::config('database.default.connection.pass').", TRUE);" ); // exit; } mysql_close($this->connection); } // Run cleanup stuffs $this->cleanup(); } } } }