Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

请问如何在控制器里面投递异步任务到task进程执行? #126

Open
ronylee opened this issue Aug 14, 2019 · 2 comments
Open

Comments

@ronylee
Copy link

ronylee commented Aug 14, 2019

实现dotask, dofinish可以处理任务,但是不知道如何投递,能否支持投递到指定的task类处理呢

@viliy
Copy link
Member

viliy commented Aug 14, 2019

`
// 新建 HttpTaskServer
class HttpTaskServer extends HTTPServer
{

public function doTask(swoole_server $server, $data, $taskId, $workerId)
{
    $this->dispatchJob($data);
}

/**
 * @param $job
 */
protected function dispatchJob($job)
{
    try {
        $job->run();
    } catch (Throwable $exception) {
        if ($job instanceof TaskInterface) {
            dispatcher()->queue($job);
        }
        logger()->error($exception);
    }
}

}

// 新建投放task处理类

class TaskDispatcher
{

public function task(TaskInterface $job)
{
    if (app()->has('server')) {
        server()->getSwoole()->task($job);
    } else {
        $this->queue($job);
    }
}

public function queue(TaskInterface $job)
{
    if ($job instanceof AbstractJob) {
        queue()->push($job);
    }
}

}

// 调度
app()->get(TaskDispatcher::class)->task(
new NoticeAsync()
);

`

@ronylee
Copy link
Author

ronylee commented Aug 14, 2019

收到,谢谢!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants