Exemplo n.º 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;
 }
Exemplo 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;
 }