cache->getValue('cloud_print_job_packing') == 1) { echo '后台打包脚本正在进行运行ing' . PHP_EOL; exit; } Yii::app()->cache->setValue('cloud_print_job_packing', 1); $connection = BusinessCloudPrintTask::model()->getDbConnection(); $taskList = $connection->createCommand('select * from cloud_print_task_pack where pack_status = 0')->query(); $connection->close(); foreach ($taskList as $key => $task) { //Yii::app()->cache->keyPrefix = $task['redis_key_perfix']; //判断任务是否在进行打包,redis记录该任务正在打包 $params['taskId'] = $task['cloud_print_id']; echo $task['cloud_print_id'] . '云印任务打包开始...' . PHP_EOL; echo '发送云印任务的学管:' . $task['auth_username'] . PHP_EOL; //请求打包接口 echo $task['cloud_print_id'] . '云印任务打包请求接口中...' . PHP_EOL; $rs = Curl::post($task['pack_api_url'], json_decode($task['pack_json'], true)); echo $task['cloud_print_id'] . '云印任务打包请求接口返回:' . PHP_EOL; var_export($rs); echo PHP_EOL; if (!($rs = json_decode($rs))) { $task['pack_exception'] = '打包接口异常'; $task['pack_status'] = self::PACK_STATUS_FAIL; } if (!isset($rs->downloadPath)) { $task['pack_exception'] = isset($rs->message) ? $rs->message : '打包接口异常'; $task['pack_status'] = self::PACK_STATUS_FAIL; } else { $task['pack_url'] = $rs->downloadPath; $task['pack_status'] = self::PACK_STATUS_SUCCESS; } if (!empty($task['group_id']) && isset($rs->downloadPath)) { if (YII_ENV == 'pro' || YII_ENV == 'production') { $rs->downloadPath = preg_replace('/http\:\/\/zstatic\d{1,2}/', 'http://zstatic' . $task['group_id'], $rs->downloadPath); $params['url'] = $rs->downloadPath; } } $params['url'] = isset($rs->downloadPath) ? $rs->downloadPath : ''; $params['status'] = $task['pack_status'] == self::PACK_STATUS_SUCCESS ? 0 : 6; $params['failReason'] = !empty($task['pack_exception']) ? $task['pack_exception'] : ''; //更新云印任务结果 TODO 地址及参数待补齐 echo '通知ims端云印任务打包结果...' . PHP_EOL; $http = http('cloud-print/task/coach/update', 'POST', $task['auth_username'], $params); $response = formatResponse($http); if ($response['status'] && $task['pack_status'] == self::PACK_STATUS_SUCCESS) { //Yii::app()->cache->set($task->redis_key, 1, 86400); $task['send_status'] = self::SEND_IMS_STATUS_SUCCESS; } else { Yii::app()->cache->deleteValue($task['redis_key']); $task['send_status'] = self::SEND_IMS_STATUS_FAIL; $task['send_exception'] = $response['data']; } // TODO //更新云印任务处理表数据 $pack_exception = isset($task['pack_exception']) ? $task['pack_exception'] : ''; $pack_status = isset($task['pack_status']) ? $task['pack_status'] : 0; $pack_url = isset($task['pack_url']) ? $task['pack_url'] : ''; $send_status = isset($task['send_status']) ? $task['send_status'] : 0; $send_exception = isset($task['send_exception']) ? $task['send_exception'] : ''; $connection = BusinessCloudPrintTask::model()->getDbConnection(); $connection->createCommand("UPDATE `cloud_print_task_pack` SET pack_exception='{$pack_exception}',pack_status={$pack_status},send_status={$send_status},send_exception='{$send_exception}',pack_url='{$pack_url}' WHERE id={$task['id']}")->query(); $connection->close(); echo $task['cloud_print_id'] . '云印任务打包结果发送完成' . PHP_EOL; } echo 'done'; Yii::app()->cache->deleteValue('cloud_print_job_packing'); } catch (Exception $e) { echo $e->getMessage(); self::apiPost('bug_log/write', array("msg" => $e->getMessage(), "sysType" => "zsyas2", "env" => YII_ENV), 2); Yii::app()->cache->deleteValue('cloud_print_job_packing'); //exit; } echo '云印任务打包脚本结束' . PHP_EOL; } /** * 程序异常停止调用的方法 */ public function shutDown() { $error_arr = error_get_last(); echo $error_arr; Yii::app()->cache->deleteValue('cloud_print_job_packing'); exit; } public static function apiPost($path, $arr, $type = 2) { $ch = @curl_init(); $result = FALSE; if ($ch) { // $data = json_encode($arr); $url = Yii::app()->params['api'][$type]['prefix'] . $path; $username = Yii::app()->params['api'][$type]['username']; $password = Yii::app()->params['api'][$type]['password']; // Digest认证 curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password); // 不输出头部 curl_setopt($ch, CURLOPT_HEADER, 0); // curl_exec 获取到的内容不直接输出, 而是返回 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 请求重启路由器的地址 传参 进行重启 curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_USERAGENT, 'Api Client/1.0.0 (chengfei@liancaitech.com)'); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $arr); // curl_setopt($ch, CURLOPT_HTTPHEADER, array( // 'Content-Type: application/json', // 'Content-Length: '. strlen($data), // )); if (!curl_errno($ch)) { //debug(curl_exec($ch)); $result = json_decode(curl_exec($ch)); } // 释放资源 curl_close($ch); } return $result; } }