In Docker, Rancher deployment, execute Yii 2 console commands based on the Supervisor timing
1. Supervisor is a small system that can manage and monitor various processes in UNIX-like systems. The configuration file of supervisor is a Windows-ini style (Python ConfigParser) file.
2. The program configuration item of the configuration file of Supervisor
; * 为必选项
;[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. Create a new \build\c_files\etc\supervisord.d\yii-task-submit.ini, as shown in Figure 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, after the program sends a stop signal, wait for the operating system to return SIGCHLD to the number of seconds. If the number of seconds passed before the supervisord receives sigchld from the process, the supervisord will try to terminate it with the final sigkill. The default interval is now 1 second to execute the command, for example: if the script execution time is 20 seconds, the interval is 21 seconds.
5. It is found that the result does not meet the expectations of command execution, enter the container command line, check /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. After solving the problem of Rancher environment variable replacement, upgrade again, the result is in line with the expectation of command after execution, but for the output of some string printing information of the command, there is no automatic wrapping, and the readability is poor. /data/logs/yii-task-submit-stderr.log, as shown in Figure 2
状态:1(待提交)的1条任务:无获取状态:1(待提交)的1条任务:无获取状态:1(待提交)的1条任务:无获取状态:1(待提交)的1条任务:无获取状态:1(待提交)的1条任务:无获取状态:1(待提交)的1条任务:无获取状态:1(待提交)的1条任务:无
7. Check /data/logs/yii-task-submit-stdout.log, the default is automatic wrapping
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. Edit \Console\Controllers\TaskController.php, add a newline when outputting the 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. After the upgrade, check /data/logs/yii-task-submit-stderr.log again, and it has been automatically lined, as shown in Figure 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条任务:无


