Example #1
0
 public static function run(Task $task)
 {
     for ($i = 0; $i < 6; $i++) {
         $task->response()->set_log(ceil(rand(0, 100) / 10));
     }
     return $task->get_request_data() * 2;
 }
Example #2
0
 /**
  * Modify task in queue
  * @param Task $task
  * @throws Exceptions\QueueException
  * @return Task
  */
 public function modify_task($task)
 {
     if (!$task instanceof Task) {
         return false;
     }
     $need_commit = false;
     if ($task->have_subtasks()) {
         $need_commit = true;
         $database_data = $this->driver->prepare_modify($task->get_uniqid());
         if (is_null($database_data)) {
             throw new QueueException('Task for modify not found');
         }
         foreach ($database_data as $id => $task_database_data) {
             /**
              * @var Task $modify_task
              */
             $current_task = $id == 0 ? $task : $task->sub_tasks()->get($task_database_data[TaskConst::UNIQID]);
             $current_task_data = $current_task->to_array();
             //check not modified parameters
             foreach ($this->not_modifiable_parameters as $not_modifiable_parameter) {
                 if ($current_task_data[$not_modifiable_parameter] != $task_database_data[$not_modifiable_parameter]) {
                     throw new QueueException("Parameter {$not_modifiable_parameter} was changed");
                 }
             }
             $diff = array_diff($current_task_data, $task_database_data);
             $current_task->set_array($diff);
         }
         $task->set_subtasks_quantity_not_performed(0)->set_subtasks_error(0);
         $have_in_process = false;
         foreach ($task->sub_tasks()->get_all() as $sub_task) {
             if ($sub_task->get_status() == Task::STATUS_IN_PROCESS) {
                 $have_in_process = true;
             }
             switch ($sub_task->get_status()) {
                 case Task::STATUS_CALLBACK_ERROR:
                     $task->completed_error(Task::STATUS_CALLBACK_ERROR);
                 case Task::STATUS_ERROR:
                     $task->inc_subtasks_error();
                     if ($sub_task->settings()->get_nested_settings('error_break')) {
                         $task->completed_error();
                     }
                     break;
                 case Task::STATUS_NEW:
                     $task->inc_subtasks_quantity_not_performed();
                     break;
             }
         }
         if (!$have_in_process && $task->get_subtasks_quantity_not_performed() == 0 && !in_array($task->get_status(), array(Task::STATUS_ERROR, Task::STATUS_CALLBACK_ERROR))) {
             if ($task->get_subtasks_error() > 0) {
                 $task->completed_error(Task::STATUS_DONE_WITH_ERROR);
             } else {
                 $task->completed_ok();
             }
         }
         $this->driver->modify_task($task->sub_tasks()->get_perform_task());
     }
     $this->driver->modify_task($task, $need_commit);
     return $task;
 }
Example #3
0
 /**
  * Get parent request data
  * @return null
  */
 public function get_parent_request_data()
 {
     if (is_null($this->parent_task)) {
         return null;
     }
     return $this->parent_task->get_request_data();
 }
Example #4
0
 public static function run(Task $task)
 {
     return $task->get_request_data() * 2;
 }
Example #5
0
 /**
  * Set current perform sub task
  * This task already have status "IN_PROCESS" in base
  * @param \PhpQueue\Task $perform_task
  * @return $this
  */
 public function add_perform_task(Task $perform_task)
 {
     $this->perform_task = $perform_task->get_uniqid();
     $this->add($perform_task);
     return $this;
 }
Example #6
0
 /**
  * Execute task
  * @param Task $task
  * @return Task
  */
 public function execute_task($task)
 {
     if (!$task instanceof Task) {
         return false;
     }
     if (is_null($task->get_start_date())) {
         $task->set_start_date(date('Y-m-d H:i:s'));
     }
     $task->set_performer($this->performer_name);
     if ($task->have_subtasks()) {
         if (!$task->get_exclusive()) {
             $task->null_performer();
         }
         $this->execute_task($task->sub_tasks()->get_perform_task());
         return $task;
     }
     $callback_classes_founded = $task->check_callback_class_names();
     if (!$callback_classes_founded) {
         $task->set_status(Task::STATUS_ERROR);
         return $task;
     }
     $exec_array = $this->default_job_priority;
     for ($job_id = 0; $job_id < count($exec_array); $job_id++) {
         /**
          * @var \PhpQueue\Interfaces\IJob|\PhpQueue\Interfaces\ICallback|\PhpQueue\Interfaces\IErrorCallback
          */
         $exec_class = null;
         $job_type = $exec_array[$job_id];
         switch ($job_type) {
             case TaskConst::JOB:
                 $exec_class = $task->get_task_name();
                 break;
             case TaskConst::TASK_CALLBACK:
                 $exec_class = $task->get_callback();
                 break;
             case TaskConst::PARENT_CALLBACK:
                 $exec_class = $task->get_parent_callback();
                 break;
             case TaskConst::GLOBAL_CALLBACK:
                 $exec_class = $this->get_global_callback();
                 break;
             case TaskConst::TASK_ERROR_CALLBACK:
                 $exec_class = $task->get_error_callback();
                 break;
             case TaskConst::PARENT_ERROR_CALLBACK:
                 $exec_class = $task->get_parent_error_callback();
                 break;
             case TaskConst::GLOBAL_ERROR_CALLBACK:
                 $exec_class = $this->get_global_error_callback();
                 break;
         }
         if (is_null($exec_class)) {
             continue;
         }
         $method_name = $this->interface_methods[Helper::interface_job($job_type)];
         try {
             $response = call_user_func_array(array($exec_class, $method_name), array($task));
             if ($job_type == TaskConst::JOB) {
                 $task->completed_ok()->response()->set_response($response);
             }
         } catch (\Exception $exception) {
             $task->response()->set_error($exception->getMessage() . " File: " . $exception->getFile() . " Line: " . $exception->getLine());
             if ($job_type != TaskConst::JOB) {
                 $task->set_status(Task::STATUS_CALLBACK_ERROR);
                 break;
             }
             $job_id = 0;
             $exec_array = $this->default_error_job_priority;
             if ($task->settings()->get_trial() >= $task->settings()->get_nested_settings('error_max_trial')) {
                 $task->completed_error();
             } else {
                 $task->set_status(Task::STATUS_NEW)->settings()->inc_trial();
             }
         }
     }
     return $task;
 }
Example #7
0
 /**
  * Modify task in queue
  * @param Task $task
  * @param bool $need_commit Transaction commit required
  * @return bool
  */
 public function modify_task(Task $task, $need_commit = false)
 {
     if (is_null($this->file_handler)) {
         $this->lock_file();
     }
     $xml_dom = $this->get_xml_dom();
     $task_xml = $xml_dom->xpath("//" . self::TASK_XML . "[@" . TaskConst::UNIQID . "='" . $task->get_uniqid() . "']");
     $task_xml = $task_xml[0];
     foreach ($task->get($this->index_row) as $item_name => $item_value) {
         $task_xml[$item_name] = $item_value;
     }
     $this->save_task($task);
     $this->save_xml_dom($xml_dom);
     $this->release_file();
     return $this;
 }
 /**
  * @depends test_get_task_without_set_process_by_uniqid
  */
 public function test_task_execution_time()
 {
     $simple_task = new Task(self::JOB);
     $simple_task->set_execution_date(date('Y-m-d H:i:s', strtotime('now') + 3));
     self::$queue->add_task($simple_task);
     $task = self::$queue->get_task();
     $this->assertFalse($task);
     sleep(10);
     $task = self::$queue->get_task();
     $this->check_asserts($task, array());
     self::$task_performer1->execute_task($task);
     $this->check_asserts($task, array('status' => Task::STATUS_DONE));
     self::$queue->modify_task($task);
 }
Example #9
0
 /**
  * Modify task in queue
  * @param Task $task
  * @param bool $need_commit Transaction commit required
  * @return bool
  */
 public function modify_task(Task $task, $need_commit = false)
 {
     $task_array = $task->to_array(true);
     if (count($task_array) > 1) {
         $update_statement = implode(",", array_filter(array_map(function ($entity) {
             if ($entity == TaskConst::ID) {
                 return null;
             }
             return $entity . "=:" . $entity;
         }, array_keys($task_array))));
         $query = $this->connection->prepare("UPDATE {$this->table_name} SET {$update_statement} where id = :id");
         $query->execute($task_array);
     }
     if ($need_commit) {
         $this->connection->commit();
     }
     return $this;
 }