在 Yii 2.0 中基于 Workerman 启动运行命令行

1、Composer 安装 Workerman 成功。如图1

Composer 安装 Workerman 成功

图1

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

Linux系统可以使用以下脚本测试本机PHP环境是否满足WorkerMan运行要求。 curl -Ss http://www.workerman.net/check.php | php。全部显示ok,则代表满足 WorkerMan 要求

图2

[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

<?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

启动运行命令成功,查看状态

图3

[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 命令行脚本

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 命令行脚本

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

 

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/250612.html

(0)
上一篇 2022年4月30日
下一篇 2022年4月30日

相关推荐

发表回复

登录后才能评论