UpgradeCrontabController.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. /**
  3. * UpgradeCrontabController 从服务器定时查询更新代码版本
  4. */
  5. namespace console\controllers;
  6. use common\enums\StatusEnum;
  7. use common\models\backend\SystemVersionLog;
  8. use common\models\backend\SystemVersionSlb;
  9. use yii\console\Controller;
  10. use yii\console\ExitCode;
  11. use Yii;
  12. class UpgradeCrontabController extends Controller
  13. {
  14. /**
  15. * 从服务器定时查询更新代码
  16. * * * * * * *
  17. * @Author: lun
  18. * @DateTime: 2021/11/17 0017 20:38
  19. * @Copyright: copyright (c) 2021 广东七件事集团
  20. * @return int
  21. */
  22. public function actionExecute(){
  23. try {
  24. // 获取最新的更新信息
  25. $log = SystemVersionLog::find()->where(['status' => StatusEnum::ENABLED])->orderBy('id desc')->asArray()->one();
  26. if (empty($log)) return ExitCode::OK;
  27. // 获取从服务器最新的更新记录
  28. $ip = Yii::$app->params['network_ip'];
  29. $slb = SystemVersionSlb::find()->where(['version_id' => $log['id'], 'ip' => $ip, 'status' => StatusEnum::ENABLED])->asArray()->one();
  30. if (empty($slb) && !empty($log['upgrade_params'])) {
  31. $model = SystemVersionSlb::initSetData($log['id']);
  32. if ($model == false) throw new \Exception('更新初始化失败:' . SystemVersionSlb::getStaticError());
  33. $upgrade_data = json_decode($log['upgrade_params'], true);// 解析更新参数
  34. Yii::$app->services->upgrade->slbUpgrade($upgrade_data, $model['id']);
  35. }
  36. return ExitCode::OK;
  37. } catch (\Exception $e) {
  38. echo $e->getMessage();
  39. $this->stderr('Error: ' . ExitCode::getReason(ExitCode::UNSPECIFIED_ERROR));
  40. return ExitCode::UNSPECIFIED_ERROR;
  41. }
  42. }
  43. }