Esempio n. 1
0
 /**
  * 添加一个 Cron Task 到执行任务列表中
  *
  * @param string $user_name    哪个用户添加的
  * @param string $task_name    任务名
  * @param string $task_desc    任务描述
  * @param string $task_class   实现 ICronTask 接口的类名称(全名,包括 namespace)
  * @param int    $task_time    任务执行的时间,GMT 时间
  * @param array  $paramArray   任务执行需要的参数
  * @param string $search_param 用于任务的搜索
  */
 public function addCronTask($user_name, $task_name, $task_desc, $task_class, $task_time, array $paramArray, $search_param = null)
 {
     // 验证任务是否可以添加
     if (!CronHelper::loadTaskClass($task_class)) {
         throw new \InvalidArgumentException('class [' . $task_class . '] illegal');
     }
     $dataMapper = new DataMapper('cron_task');
     $dataMapper->user_name = $user_name;
     $dataMapper->task_name = $task_name;
     $dataMapper->task_desc = $task_desc;
     $dataMapper->task_class = $task_class;
     $dataMapper->task_time = $task_time;
     $dataMapper->task_param = json_encode($paramArray);
     $dataMapper->search_param = $search_param;
     $dataMapper->save();
     unset($dataMapper);
 }
Esempio n. 2
0
 /**
  * 驱动 Cron 任务执行
  *
  * @param $f3
  */
 public function Run($f3)
 {
     $taskCount = 0;
     while (($cronTask = CronHelper::loadNextUnRunCronTask(Time::gmTime())) && $taskCount++ < self::$maxRunTaskCount) {
         $cronResult = array('code' => -1, 'message' => '任务内部错误,请查看日志');
         // 加载 Task Class
         $taskInstance = CronHelper::loadTaskClass($cronTask['task_class']);
         // 无法实例化 task class,打印错误日志
         if (null == $taskInstance) {
             printLog('can not instantiate task ' . json_encode($cronTask->toArray()), __CLASS__, \Core\Log\Base::ERROR);
         } else {
             // 执行任务
             $cronResult = call_user_func_array(array($taskInstance, 'run'), array(json_decode($cronTask['task_param'], true)));
         }
         // 释放内存
         unset($taskInstance);
         // 更新任务执行状态
         $cronTask->task_run_time = Time::gmTime();
         $cronTask->return_code = @$cronResult['code'];
         $cronTask->return_message = @$cronResult['message'];
         $cronTask->save();
         unset($cronTask);
     }
 }