public function init()
{
parent::init();
$this->parseParams();
//初始化解析参数
//重写exception handler
set_exception_handler(function ($e) {
//捕获错误信息
$this->exception = \yii\base\ErrorHandler::convertExceptionToString($e);
});
//重写register_shutdown
register_shutdown_function(function () {
//非致命错误脚本结束写日志
$task = isset($this->data['task']) ? $this->data['task'] : '';
$task_id = isset($this->data['info']['task_id']) ? $this->data['info']['task_id'] : 0;
$orgcode = isset($this->data['info']['orgcode']) ? $this->data['info']['orgcode'] : '';
$task_type = isset($this->data['info']['task_type']) ? $this->data['info']['task_type'] : '';
$start_time = isset($this->data['info']['start_time']) ? $this->data['info']['start_time'] : '';
$msg = [];
$msg['info'] = $this->loginfo['info'];
$msg['exitStatus'] = \Yii::$app->response->exitStatus;
if (isset($this->data['retry_times'])) {
$msg['retry_times'] = $this->data['retry_times'];
}
$end_time = date('Y-m-d H:i:s');
if ($this->exception) {
$msg['exitStatus'] = 1;
$IsSuccess = 0;
} elseif ($msg['exitStatus'] == 0) {
$IsSuccess = 1;
} elseif ($msg['exitStatus'] == 1) {
$IsSuccess = 0;
}
$this->task_log($task_id, $orgcode, $task, $task_type, $start_time, $end_time, $msg, $IsSuccess, $this->exception);
if ($IsSuccess) {
//成功则更细
$this->updateTaskStatus($task_id, ['progress' => 0, 'retry_times' => 0]);
//更新任务状态
} else {
//失败则更新
$retry_times = isset($this->data['retry_times']) ? $this->data['retry_times'] : 0;
// $this->updateTaskStatus($task_id,['retry_times'=>$retry_times]); //更新任务状态,重试次数
$this->updateTaskStatus($task_id, ['progress' => 0, 'retry_times' => 0]);
//更新任务状态
}
if ($this->exception) {
echo $this->exception, PHP_EOL;
exit(1);
}
});
}