protected function daemon() { try { while (!$this->_sigterm) { // Find first task that is not being executed $task = task_queue::get_next_task(); if ($task != null && $task->loaded() && count($this->_pids) < $this->_config['max']) { // Task found // Write log to prevent memory issues Kohana::$log->write(); // Fork process to execute task $pid = pcntl_fork(); if ($pid == -1) { Kohana::$log->add('error', 'Queue. Could not spawn child task process.'); exit; } elseif ($pid) { // Parent - add the child's PID to the running list $this->_pids[$pid] = time(); } else { try { // Child - Execute task Kohana::$log->add('debug', strtr('Child - Execute task - route: :route, uri: :uri', array(':route' => $task->route, ':uri' => http_build_query($task->uri)))); // fun teh route/action - acts like an include (watch for include() or require() duplicates) Request::factory(Route::get($task->route)->uri($task->uri))->execute(); } catch (Exception $e) { // Task failed - log message Kohana::$log->add('error', strtr('Queue. Task failed - route: :route, uri: :uri, msg: :msg', array(':route' => $task->route, ':uri' => http_build_query($task->uri), ':msg' => $e->getMessage()))); } // Remove task from queue // either because it ran or because it failed task_queue::remove_task($task->id); unset($task); // exit; } } else { // No task in queue - sleep usleep($this->_config['sleep']); } } // clean up $this->clean(); } catch (Exception $e) { Kohana::$log->add('error', 'Queue. Daemon failed, msg: ' . $e->getMessage()); } }