在 Docker、Rancher 部署中,基于 Supervisor 定时执行 Yii 2 控制台命令
1、Supervisor 是可以在类 UNIX 系统中进行管理和监控各种进程的小型系统。Supervisor 的配置文件是一个Windows-INI风格(Python ConfigParser)文件。
2、Supervisor 的配置文件的 program 配置项
; * 为必选项 ;[program:theprogramname] ;command=/bin/cat ; * 要执行的命令路径(可以是相对与 PATH 的路径)也可带参数 ;startsecs=1 ; 需要考虑进程启动成功的时间, 当 running 状态超过该值时,表明启动成功 (def. 1) ;autorestart=unexpected ; 当进程退出时是否应该重启,可选值为 false true unexpected ,为 false 时表示不重启,为 true 表示重启,为 unexpected 时,如果退出状态码不是 exitcodes 中之一时进行重启 (def: unexpected) ;stopwaitsecs=10 ; 这个是当我们向子进程发送 stopsignal 信号后,到系统返回信息给 supervisord,所等待的最大时间。超过这个时间,supervisord会向该子进程发送一个强制 kill 的信号。(default 10) ;stdout_logfile=/a/path ; 进程 stdout 输出的日志文件路径,如果将其设置为NONE,则supervisord将不创建任何日志文件; default AUTO ;stderr_logfile=/a/path ; 除非redirect_stderr为true,否则将进程 stderr 输出放在此文件中。日志文件路径,如果将其设置为NONE,则supervisord将不创建任何日志文件; default AUTO
3、新建 \build\c_files\etc\supervisord.d\yii-task-submit.ini,如图1
[program:yii-task-submit] command = php /sobey/www/aims-rpc/yii task/submit autorestart = true startsecs = 0 stopwaitsecs = 10 stderr_logfile = /data/logs/yii-task-submit-stderr.log stdout_logfile = /data/logs/yii-task-submit-stdout.log
4、stopwaitsecs,在程序发送停止信号后,等待操作系统将SIGCHLD返回到supervisord的秒数。如果在supervisord从进程收到SIGCHLD之前经过了这个秒数,supervisord将尝试使用最终的SIGKILL终止它。现在默认间隔 1 秒钟执行一次 command,例:如果脚本执行时间为20秒,则间隔时间为21秒。
5、发现结果不符合 command 执行后的预期,进入容器命令行,查看 /data/logs/yii-task-submit-stderr.log
Exception 'yii\httpclient\Exception' with message 'Curl error: #6 - Could not resolve host: AIMS_RPC_CFG_AI_API_HOST_INFOAIMS_RPC_CFG_AI_API_BASE_URL; Name or service not known' in /sobey/www/aims-rpc/vendor/yiisoft/yii2-httpclient/src/CurlTransport.php:57 Stack trace: #0 /sobey/www/aims-rpc/vendor/yiisoft/yii2-httpclient/src/Client.php(221): yii\httpclient\CurlTransport->send(Object(yii\httpclient\Request)) #1 /sobey/www/aims-rpc/vendor/yiisoft/yii2-httpclient/src/Request.php(421): yii\httpclient\Client->send(Object(yii\httpclient\Request)) #2 /sobey/www/aims-rpc/common/logics/http/ai_api/Task.php(63): yii\httpclient\Request->send() #3 /sobey/www/aims-rpc/console/services/TaskService.php(233): common\logics\http\ai_api\Task->getQueryReady() #4 /sobey/www/aims-rpc/console/services/TaskService.php(33): console\services\TaskService->getAiQueryReady() #5 /sobey/www/aims-rpc/console/controllers/TaskController.php(43): console\services\TaskService->submit() #6 [internal function]: console\controllers\TaskController->actionSubmit() #7 /sobey/www/aims-rpc/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array) #8 /sobey/www/aims-rpc/vendor/yiisoft/yii2/base/Controller.php(157): yii\base\InlineAction->runWithParams(Array) #9 /sobey/www/aims-rpc/vendor/yiisoft/yii2/console/Controller.php(148): yii\base\Controller->runAction('submit', Array) #10 /sobey/www/aims-rpc/vendor/yiisoft/yii2/base/Module.php(528): yii\console\Controller->runAction('submit', Array) #11 /sobey/www/aims-rpc/vendor/yiisoft/yii2/console/Application.php(180): yii\base\Module->runAction('task/submit', Array) #12 /sobey/www/aims-rpc/vendor/yiisoft/yii2/console/Application.php(147): yii\console\Application->runAction('task/submit', Array) #13 /sobey/www/aims-rpc/vendor/yiisoft/yii2/base/Application.php(386): yii\console\Application->handleRequest(Object(yii\console\Request)) #14 /sobey/www/aims-rpc/yii(23): yii\base\Application->run() #15 {main}
6、解决了 Rancher 环境变量替换的问题后,再次升级,结果符合 command 执行后的预期,不过对于命令的一些字符串打印信息的输出,未自动换行,可读性较差,查看 /data/logs/yii-task-submit-stderr.log,如图2
状态:1(待提交)的1条任务:无获取状态:1(待提交)的1条任务:无获取状态:1(待提交)的1条任务:无获取状态:1(待提交)的1条任务:无获取状态:1(待提交)的1条任务:无获取状态:1(待提交)的1条任务:无获取状态:1(待提交)的1条任务:无
7、查看 /data/logs/yii-task-submit-stdout.log,默认是自动换行的
Could not open input file: /sobey/www/aims-rpc/yii Could not open input file: /sobey/www/aims-rpc/yii Could not open input file: /sobey/www/aims-rpc/yii Could not open input file: /sobey/www/aims-rpc/yii Could not open input file: /sobey/www/aims-rpc/yii Could not open input file: /sobey/www/aims-rpc/yii Could not open input file: /sobey/www/aims-rpc/yii Could not open input file: /sobey/www/aims-rpc/yii Could not open input file: /sobey/www/aims-rpc/yii Could not open input file: /sobey/www/aims-rpc/yii Could not open input file: /sobey/www/aims-rpc/yii Could not open input file: /sobey/www/aims-rpc/yii Could not open input file: /sobey/www/aims-rpc/yii Could not open input file: /sobey/www/aims-rpc/yii Could not open input file: /sobey/www/aims-rpc/yii Could not open input file: /sobey/www/aims-rpc/yii Could not open input file: /sobey/www/aims-rpc/yii Could not open input file: /sobey/www/aims-rpc/yii Could not open input file: /sobey/www/aims-rpc/yii Could not open input file: /sobey/www/aims-rpc/yii Could not open input file: /sobey/www/aims-rpc/yii Could not open input file: /sobey/www/aims-rpc/yii Could not open input file: /sobey/www/aims-rpc/yii Could not open input file: /sobey/www/aims-rpc/yii Could not open input file: /sobey/www/aims-rpc/yii Could not open input file: /sobey/www/aims-rpc/yii Could not open input file: /sobey/www/aims-rpc/yii Could not open input file: /sobey/www/aims-rpc/yii Could not open input file: /sobey/www/aims-rpc/yii Could not open input file: /sobey/www/aims-rpc/yii Could not open input file: /sobey/www/aims-rpc/yii Could not open input file: /sobey/www/aims-rpc/yii Could not open input file: /sobey/www/aims-rpc/yii Could not open input file: /sobey/www/aims-rpc/yii Could not open input file: /sobey/www/aims-rpc/yii Could not open input file: /sobey/www/aims-rpc/yii Could not open input file: /sobey/www/aims-rpc/yii
8、编辑 \console\controllers\TaskController.php,在输出 message 时,加上换行符
/** * 调用AI接口:提交任务 * 任务描述: * 1、查询任务队列是否空闲,如果否,则退出; * 2、获取1条任务,其条件与排序规则如下: * (1)任务,状态:1(待提交); * (2)租户优先级,状态:1(启用); * (3)排序规则:group_pri.group_pri(顺序),group_pri.group_pri_updated_at(倒序),task.pri(顺序),task.pri_updated_at(倒序),task.created_at(顺序); * 3、获取1条任务,如果无,则退出; * 4、获取当前任务的人工智能服务提交日志,如果为空,则提交,否则重做; * 5、人工智能服务:提交任务/重做任务; * 6、操作数据(事务) * (1)创建MySQL模型(人工智能服务提交日志); * (2)更新任务模型的状态:2(已提交),人工智能服务任务状态:1(处理中); * (3)创建MySQL模型(人工智能服务任务进度); */ public function actionSubmit() { $service = new TaskService(); $submitResult = $service->submit(); if ($submitResult['status'] === false) { $this->stderr($submitResult['message'] . "\n"); return $submitResult['code']; } return ExitCode::OK; }
9、升级后,再次查看 /data/logs/yii-task-submit-stderr.log,已经自动换行,如图3
获取状态:1(待提交)的1条任务:无 获取状态:1(待提交)的1条任务:无 获取状态:1(待提交)的1条任务:无 获取状态:1(待提交)的1条任务:无 获取状态:1(待提交)的1条任务:无 获取状态:1(待提交)的1条任务:无 获取状态:1(待提交)的1条任务:无 获取状态:1(待提交)的1条任务:无 获取状态:1(待提交)的1条任务:无 获取状态:1(待提交)的1条任务:无 获取状态:1(待提交)的1条任务:无 获取状态:1(待提交)的1条任务:无 获取状态:1(待提交)的1条任务:无 获取状态:1(待提交)的1条任务:无 获取状态:1(待提交)的1条任务:无
近期评论