CloudPrintPackCommand.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. Yii::import('application.models.*');
  3. /**
  4. *
  5. * 云印任务后台处理脚本
  6. * Created by PhpStorm.
  7. * User: Administrator
  8. * Date: 2019/9/2 0002
  9. * Time: 13:30
  10. */
  11. class CloudPrintPackCommand extends CConsoleCommand
  12. {
  13. const PACK_STATUS_SUCCESS = 1; //打包成功
  14. const PACK_STATUS_FAIL = 2; //打包失败
  15. const SEND_IMS_STATUS_SUCCESS = 1; //发送云印成功
  16. const SEND_IMS_STATUS_FAIL = 0; //发送云印失败
  17. public function init()
  18. {
  19. parent::init();
  20. @ini_set('memory_limit', '1024M');
  21. set_time_limit(0);
  22. register_shutdown_function(array($this, "shutDown"));
  23. }
  24. public function actionIndex($YII_ENV = 'development')
  25. {
  26. echo '云印任务打包开始执行...' . PHP_EOL;
  27. try {
  28. //查询所以待打包任务
  29. if (Yii::app()->cache->getValue('cloud_print_job_packing') == 1) {
  30. echo '后台打包脚本正在进行运行ing' . PHP_EOL;
  31. exit;
  32. }
  33. Yii::app()->cache->setValue('cloud_print_job_packing', 1);
  34. $connection = BusinessCloudPrintTask::model()->getDbConnection();
  35. $taskList = $connection->createCommand('select * from cloud_print_task_pack where pack_status = 0')->query();
  36. $connection->close();
  37. foreach ($taskList as $key => $task) {
  38. //Yii::app()->cache->keyPrefix = $task['redis_key_perfix'];
  39. //判断任务是否在进行打包,redis记录该任务正在打包
  40. $params['taskId'] = $task['cloud_print_id'];
  41. echo $task['cloud_print_id'] . '云印任务打包开始...' . PHP_EOL;
  42. echo '发送云印任务的学管:' . $task['auth_username'] . PHP_EOL;
  43. //请求打包接口
  44. echo $task['cloud_print_id'] . '云印任务打包请求接口中...' . PHP_EOL;
  45. $rs = Curl::post($task['pack_api_url'], json_decode($task['pack_json'], true));
  46. echo $task['cloud_print_id'] . '云印任务打包请求接口返回:' . PHP_EOL;
  47. var_export($rs);
  48. echo PHP_EOL;
  49. if (!($rs = json_decode($rs))) {
  50. $task['pack_exception'] = '打包接口异常';
  51. $task['pack_status'] = self::PACK_STATUS_FAIL;
  52. }
  53. if (!isset($rs->downloadPath)) {
  54. $task['pack_exception'] = isset($rs->message) ? $rs->message : '打包接口异常';
  55. $task['pack_status'] = self::PACK_STATUS_FAIL;
  56. } else {
  57. $task['pack_url'] = $rs->downloadPath;
  58. $task['pack_status'] = self::PACK_STATUS_SUCCESS;
  59. }
  60. if (!empty($task['group_id']) && isset($rs->downloadPath)) {
  61. if (YII_ENV == 'pro' || YII_ENV == 'production') {
  62. $rs->downloadPath = preg_replace('/http\:\/\/zstatic\d{1,2}/', 'http://zstatic' . $task['group_id'], $rs->downloadPath);
  63. $params['url'] = $rs->downloadPath;
  64. }
  65. }
  66. $params['url'] = isset($rs->downloadPath) ? $rs->downloadPath : '';
  67. $params['status'] = $task['pack_status'] == self::PACK_STATUS_SUCCESS ? 0 : 6;
  68. $params['failReason'] = !empty($task['pack_exception']) ? $task['pack_exception'] : '';
  69. //更新云印任务结果 TODO 地址及参数待补齐
  70. echo '通知ims端云印任务打包结果...' . PHP_EOL;
  71. $http = http('cloud-print/task/coach/update', 'POST', $task['auth_username'], $params);
  72. $response = formatResponse($http);
  73. if ($response['status'] && $task['pack_status'] == self::PACK_STATUS_SUCCESS) {
  74. //Yii::app()->cache->set($task->redis_key, 1, 86400);
  75. $task['send_status'] = self::SEND_IMS_STATUS_SUCCESS;
  76. } else {
  77. Yii::app()->cache->deleteValue($task['redis_key']);
  78. $task['send_status'] = self::SEND_IMS_STATUS_FAIL;
  79. $task['send_exception'] = $response['data'];
  80. }
  81. // TODO
  82. //更新云印任务处理表数据
  83. $pack_exception = isset($task['pack_exception']) ? $task['pack_exception'] : '';
  84. $pack_status = isset($task['pack_status']) ? $task['pack_status'] : 0;
  85. $pack_url = isset($task['pack_url']) ? $task['pack_url'] : '';
  86. $send_status = isset($task['send_status']) ? $task['send_status'] : 0;
  87. $send_exception = isset($task['send_exception']) ? $task['send_exception'] : '';
  88. $connection = BusinessCloudPrintTask::model()->getDbConnection();
  89. $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();
  90. $connection->close();
  91. echo $task['cloud_print_id'] . '云印任务打包结果发送完成' . PHP_EOL;
  92. }
  93. echo 'done';
  94. Yii::app()->cache->deleteValue('cloud_print_job_packing');
  95. } catch (Exception $e) {
  96. echo $e->getMessage();
  97. self::apiPost('bug_log/write', array("msg" => $e->getMessage(), "sysType" => "zsyas2", "env" => YII_ENV), 2);
  98. Yii::app()->cache->deleteValue('cloud_print_job_packing');
  99. //exit;
  100. }
  101. echo '云印任务打包脚本结束' . PHP_EOL;
  102. }
  103. /**
  104. * 程序异常停止调用的方法
  105. */
  106. public function shutDown()
  107. {
  108. $error_arr = error_get_last();
  109. echo $error_arr;
  110. Yii::app()->cache->deleteValue('cloud_print_job_packing');
  111. exit;
  112. }
  113. public static function apiPost($path, $arr, $type = 2)
  114. {
  115. $ch = @curl_init();
  116. $result = FALSE;
  117. if ($ch) {
  118. // $data = json_encode($arr);
  119. $url = Yii::app()->params['api'][$type]['prefix'] . $path;
  120. $username = Yii::app()->params['api'][$type]['username'];
  121. $password = Yii::app()->params['api'][$type]['password'];
  122. // Digest认证
  123. curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  124. curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password);
  125. // 不输出头部
  126. curl_setopt($ch, CURLOPT_HEADER, 0);
  127. // curl_exec 获取到的内容不直接输出, 而是返回
  128. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  129. // 请求重启路由器的地址 传参 进行重启
  130. curl_setopt($ch, CURLOPT_URL, $url);
  131. curl_setopt($ch, CURLOPT_USERAGENT, 'Api Client/1.0.0 (chengfei@liancaitech.com)');
  132. curl_setopt($ch, CURLOPT_POST, 1);
  133. curl_setopt($ch, CURLOPT_POSTFIELDS, $arr);
  134. // curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  135. // 'Content-Type: application/json',
  136. // 'Content-Length: '. strlen($data),
  137. // ));
  138. if (!curl_errno($ch)) {
  139. //debug(curl_exec($ch));
  140. $result = json_decode(curl_exec($ch));
  141. }
  142. // 释放资源
  143. curl_close($ch);
  144. }
  145. return $result;
  146. }
  147. }