Worker能这么整吗

forwebreg
<?php

declare(strict_types=1);

namespace common\server;

use Workerman\Crontab\Crontab;
use Workerman\Worker;

class CrontabServer
{

    protected $worker;
    public function onWorkerStart($worker)
    {
        $this->worker = $worker;
        echo "Crontab Server started\n";
    }

    public function onClose($connection)
    {
        echo "Crontab Server connection closed\n";
    }

    public function onMessage($connection, $message)
    {
        $data = json_decode($message, true);
        switch ($data['action']) {
            case 'start':
                //$data = ['action' => 'start','crontabid'=>'1','callfunc'=>'aaa'];
                $worker = new Worker();
                //如何保存$worker信息 [crontabid_1_worker:???]
                $worker->onWorkerStart = function ($worker) use ($data) {
                    $status = new Worker();
                    $status->onWorkerStart = function ($status) {
                        new Crontab('*/1 * * * * *', function () {
                            echo "Worker is runing\n";
                            //记录状态为运行中 [crontabid_1_status:1]
                        });
                    };
                    new Crontab('*/1 * * * * *', function () use ($data) {
                        echo "Worker Task\n";
                        call_user_func($data['callfunc']);
                    });
                };
                break;
            case 'stop':
                //$data = ['action' => 'stop','crontabid'=>'1'];
                //获取crontabid_1_worker的信息 然后停止对应的worker
                break;
            default:
                $connection->send(json_encode([403, 'Action error']));
                break;
        }
    }
}
205 3 0
3个回答

forwebreg

对了 直接执行这一段代码 好像也不会打印

                $worker = new Worker();
                //如何保存$worker信息 [crontabid_1_worker:???]
                $worker->onWorkerStart = function ($worker) use ($data) {
                    $status = new Worker();
                    $status->onWorkerStart = function ($status) {
                        new Crontab('*/1 * * * * *', function () {
                            echo "Worker is runing\n";
                            //记录状态为运行中 [crontabid_1_status:1]
                        });
                    };
                    new Crontab('*/1 * * * * *', function () use ($data) {
                        echo "Worker Task\n";
                        call_user_func($data['callfunc']);
                    });
                };
  • 暂无评论

workerman 不支持动态创建进程

  • forwebreg 2天前

    好吧 那这个路是走不通了 大佬有没有别的思路

胡桃

为什么要创建进程,用协程不行?

$task = function () { ... };
go($task);
  • forwebreg 12小时前

    协程的话 要改用Swoole了 现在没用上 担心会引起其他问题

×
🔝