没有不值得去解决的问题,也没有不值得去学习的技术!

Run Command-Line Tasks in Yii 2.0 With Workerman

The command started and ran successfully, checking the status

1. Workerman installed successfully via Composer. See Figure 1.

Workerman installed successfully via Composer
Figure 1
Plaintext

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. On Linux systems, you can use the following script to test whether the local PHP environment meets WorkerMan’s running requirements: curl -Ss http://www.workerman.net/check.php | php. If everything shows OK, it means the environment meets WorkerMan’s requirements. See Figure 2.

On Linux systems, you can use the following script to test whether the local PHP environment meets WorkerMan's running requirements: curl -Ss http://www.workerman.net/check.php | php. If everything shows OK, it means the environment meets WorkerMan's requirements
Figure 2
Plaintext

[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. Referring to a colleague’s implementation, edit the file crontab.php.

PHP

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. The command started and ran successfully. Checking the status. See Figure 3.

The command started and ran successfully, checking the status
Figure 3
Plaintext

[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  [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. Reference link: Running PHP command-line scripts based on supervisord, adjusted to running PHP command-line scripts based on Workerman.
Running PHP command-line scripts based on supervisord

ShellScript

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

Running PHP command-line scripts based on Workerman

ShellScript

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

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系