在 Yii 2.0 中基于 Workerman 启动运行命令行
1、Composer 安装 Workerman 成功。如图1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | PS E:\wwwroot\channel-pub-api> composer require workerman/workerman Using version ^4.0 for workerman/workerman ./composer.json has been updated Running composer update workerman/workerman Loading composer repositories with package information Updating dependencies Lock file operations: 1 install, 0 updates, 0 removals - Locking workerman/workerman (v4.0.19) Writing lock file Installing dependencies from lock file (including require-dev) Package operations: 1 install, 0 updates, 0 removals - Downloading workerman/workerman (v4.0.19) - Installing workerman/workerman (v4.0.19): Extracting archive 1 package suggestions were added by new dependencies, use `composer suggest` to see details. Package phpunit/php-token-stream is abandoned, you should avoid using it. No replacement was suggested. Generating autoload files 51 packages you are using are looking for funding. Use the `composer fund` command to find out more! PS E:\wwwroot\channel-pub-api> |
2、Linux系统可以使用以下脚本测试本机PHP环境是否满足WorkerMan运行要求。 curl -Ss http://www.workerman.net/check.php | php。全部显示ok,则代表满足 WorkerMan 要求。如图2
1 2 3 4 5 | [root@api-65d7f4df69-gpgvg /]# curl -Ss http://www.workerman.net/check.php | php PHP Version >= 5.3.3 [OK] Extension pcntl check [OK] Extension posix check [OK] [root@api-65d7f4df69-gpgvg /]# |
3、参考同事的实现,编辑文件 crontab.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | <?php defined( 'YII_DEBUG' ) or define( 'YII_DEBUG' , true); defined( 'YII_ENV' ) or define( 'YII_ENV' , 'dev' ); require __DIR__ . '/vendor/autoload.php' ; require __DIR__ . '/vendor/yiisoft/yii2/Yii.php' ; $config = require __DIR__ . '/ccp/config/console.php' ; ( new yii\console\Application( $config ))->init(); use Workerman\Lib\Timer; use Workerman\Worker; Worker:: $pidFile = __DIR__ . '/ccp/runtime/worker_man_crontab.pid' ; Worker:: $stdoutFile = __DIR__ . '/ccp/runtime/worker_man_stdout.log' ; Worker:: $logFile = __DIR__ . '/ccp/runtime/worker_man_' . date ( 'Y_m_d' ). '.log' ; $task = new Worker(); //$task->user = 'nginx'; $task -> count = 5; $task ->onWorkerStart = function ( $task ) { if ( $task ->id === 0) { $task ->name = 'crontab_vtt_download_asset_index' ; Timer::add(2, function () { $obj_service_model = new ccp\commands\DownloadAssetController(null, null); $obj_service_model ->actionIndex(); yii:: $app ->db->close(); yii:: $app ->redis->close(); }); } if ( $task ->id === 1) { $task ->name = 'crontab_vtt_upload_asset_index' ; Timer::add(2, function () { $obj_service_model = new ccp\commands\UploadAssetController(null, null); $obj_service_model ->actionIndex(); yii:: $app ->db->close(); yii:: $app ->redis->close(); }); } if ( $task ->id === 2) { $task ->name = 'crontab_vtt_publish_task_index' ; Timer::add(60, function () { $obj_service_model = new ccp\commands\PublishTaskController(null, null); $obj_service_model ->actionIndex(); yii:: $app ->db->close(); yii:: $app ->redis->close(); }); } if ( $task ->id === 3) { $task ->name = 'crontab_vtt_status_check_index' ; Timer::add(60, function () { $obj_service_model = new ccp\commands\StatusCheckController(null, null); $obj_service_model ->actionIndex(); yii:: $app ->db->close(); yii:: $app ->redis->close(); }); } if ( $task ->id === 4) { $task ->name = 'crontab_vtt_callback_index' ; Timer::add(60, function () { $obj_service_model = new ccp\commands\CallbackController(null, null); $obj_service_model ->actionIndex(); yii:: $app ->db->close(); yii:: $app ->redis->close(); }); } }; Worker::runAll(); |
4、启动运行命令成功,查看状态。如图3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | [root@api-55cdf94cd8-9drns ccp_api]# php /mcloud/www/ccp_api/crontab.php start -d Workerman[/mcloud/www/ccp_api/crontab.php] start in DAEMON mode ------------------------------------- WORKERMAN -------------------------------------- Workerman version:4.0.19 PHP version:7.4.13 -------------------------------------- WORKERS --------------------------------------- proto user worker listen processes status tcp root none none 5 [OK] -------------------------------------------------------------------------------------- Input "php /mcloud/www/ccp_api/crontab.php stop" to stop. Start success. [root@api-55cdf94cd8-9drns ccp_api]# php crontab.php -h Usage: php yourfile <command> [mode] Commands: start Start worker in DEBUG mode. Use mode -d to start in DAEMON mode. stop Stop worker. Use mode -g to stop gracefully. restart Restart workers. Use mode -d to start in DAEMON mode. Use mode -g to stop gracefully. reload Reload codes. Use mode -g to reload gracefully. status Get worker status. Use mode -d to show live status. connections Get worker connections. [root@api-55cdf94cd8-9drns ccp_api]# php crontab.php status Workerman[crontab.php] status ----------------------------------------------GLOBAL STATUS---------------------------------------------------- Workerman version:4.0.19 PHP version:7.4.13 start time:2021-09-03 13:24:54 run 0 days 0 hours load average: 0.62, 1, 1 event-loop:\Workerman\Events\Select 1 workers 5 processes worker_name exit_status exit_count none 0 0 ----------------------------------------------PROCESS STATUS--------------------------------------------------- pid memory listening worker_name connections send_fail timers total_request qps status 1210 6M none crontab_vtt_download_asset_index 0 0 1 0 0 [idle] 1211 4M none crontab_vtt_upload_asset_index 0 0 1 0 0 [idle] 1212 4M none crontab_vtt_publish_task_index 0 0 1 0 0 [idle] 1213 4M none crontab_vtt_status_check_index 0 0 1 0 0 [idle] 1214 4M none crontab_vtt_callback_index 0 0 1 0 0 [idle] ----------------------------------------------PROCESS STATUS--------------------------------------------------- Summary 22M - - 0 0 5 0 0 [Summary] [root@api-55cdf94cd8-9drns ccp_api]# |
5、参考网址:https://www.shuijingwanwq.com/2019/09/30/3534/ 。基于 supervisord 运行 PHP 命令行脚本,调整为 基于 Workerman 运行 PHP 命令行脚本。
基于 supervisord 运行 PHP 命令行脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | env | grep CHANNEL_PUB_API_CFG_CRONTAB_VTT || export CHANNEL_PUB_API_CFG_CRONTAB_VTT= "false" if [[ $CHANNEL_PUB_API_CFG_CRONTAB_VTT == "true" ]] then \ cp /mcloud/yii-crontab-vtt-download-asset-index .ini /etc/supervisord .d/ echo "复制 yii-crontab-vtt-download-asset-index.ini 至 /etc/supervisord.d/" \ cp /mcloud/yii-crontab-vtt-upload-asset-index .ini /etc/supervisord .d/ echo "复制 yii-crontab-vtt-upload-asset-index.ini 至 /etc/supervisord.d/" \ cp /mcloud/yii-crontab-vtt-publish-task-index .ini /etc/supervisord .d/ echo "复制 yii-crontab-vtt-publish-task-index.ini 至 /etc/supervisord.d/" \ cp /mcloud/yii-crontab-vtt-status-check-index .ini /etc/supervisord .d/ echo "复制 yii-crontab-vtt-status-check-index.ini 至 /etc/supervisord.d/" \ cp /mcloud/yii-crontab-vtt-callback-index .ini /etc/supervisord .d/ echo "复制 yii-crontab-vtt-callback-index.ini 至 /etc/supervisord.d/" \ chmod +x /etc/supervisord .d/* echo "添加 /etc/supervisord.d/* 的执行权限" elif [[ $CHANNEL_PUB_API_CFG_CRONTAB_VTT == "false" ]] then echo "copy without crontab_vtt" else echo "please set environment variable CHANNEL_PUB_API_CFG_CRONTAB_VTT true or false" fi |
基于 Workerman 运行 PHP 命令行脚本
1 2 3 4 5 6 7 8 9 10 | env | grep CHANNEL_PUB_API_CFG_CRONTAB_VTT || export CHANNEL_PUB_API_CFG_CRONTAB_VTT= "false" if [[ $CHANNEL_PUB_API_CFG_CRONTAB_VTT == "true" ]] then php /mcloud/www/ccp_api/crontab .php start -d elif [[ $CHANNEL_PUB_API_CFG_CRONTAB_VTT == "false" ]] then echo "crontab.php not running" else echo "please set environment variable CHANNEL_PUB_API_CFG_CRONTAB_VTT true or false" fi |
近期评论