public static function add($controller_name, $method_name, $parameters = array(), $priority = 5, $application_path = '')
 {
     if ($priority < 1 or $priority > 10) {
         Kohana::log('error', 'The priority of the task was out of range!');
         return FALSE;
     }
     $application_path = empty($application_path) ? APPPATH : $application_path;
     $old_module_list = Kohana::config('core.modules');
     Kohana::config_set('core.modules', array_merge($old_module_list, array($application_path)));
     // Make sure the controller name and method are valid
     if (Kohana::auto_load($controller_name)) {
         // Only add it to the queue if the controller method exists
         if (Kohana::config('queue.validate_methods') and !method_exists($controller_name, $method_name)) {
             Kohana::log('error', 'The method ' . $controller_name . '::' . $method_name . ' does not exist.');
             return FALSE;
         }
         // Add the action to the run queue with the priority
         $task = new Task_Model();
         $task->set_fields(array('application' => $application_path, 'class' => $controller_name, 'method' => $method_name, 'params' => serialize($parameters), 'priority' => $priority));
         $task->save();
         // Restore the module list
         Kohana::config_set('core.modules', $old_module_list);
         return TRUE;
     }
     Kohana::log('error', 'The class ' . $controller_name . ' does not exist.');
     return FALSE;
 }
示例#2
0
 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;
 }
示例#3
0
 public function removeTask()
 {
     $user = $this->authenticate();
     $emptyrequest = !isset($_GET) && !isset($_POST) || sizeof($_GET) == 0 && sizeof($_POST) == 0;
     $input;
     if (!$emptyrequest) {
         $input = new Validation(array_merge($_GET, $_POST));
         $input->add_rules('task_id', 'required', 'numeric');
         $validator = new TaskValidation_Model();
         $validator->expectedUser_id = $user->user_id;
         $input->add_callbacks('task_id', array($validator, "validateExists"));
         $input->add_callbacks('task_id', array($validator, "validateUserOwnsTask"));
     } else {
         $input = new Validation(array());
         $input->add_error('task_id', 'required');
     }
     if ($input->validate()) {
         $task = new Task_Model();
         $task->task_id = $input->task_id;
         $task->retrieveInfoFromDB();
         $task->removeFromDB();
         Kohana::render($this->encode($task));
     } else {
         //@TODO : make better error messages......
         Kohana::render($this->encode(NULL, $input->errors()));
     }
 }
 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();
             }
         }
     }
 }
 public function __construct()
 {
     // load database library into $this->db (can be omitted if not required)
     parent::__construct();
 }