Esempio n. 1
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;
 }
Esempio n. 2
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;
 }