If you haven’t already installed HHXSv5 / laravel-S, go to Installing Laravels and using WebSockets under Installing Laravel

This feature relies on Swoole’s AsyncTask and must first be set to config/laravels. The processing capability of asynchronous tasks depends on the number of Task processes. Therefore, set task_worker_num properly.

Creating a Task Class


      
/** * Created by PhpStorm. * User: wxiangqian */
namespace App\Tasks;

use Hhxsv5\LaravelS\Swoole\Task\Task;
use Illuminate\Support\Facades\Log;

class TestTask extends Task
{
    private $data;
    private $result;
    public function __construct($data)
    {
        $this->data = $data;
    }
    // The logic that processes the Task runs in the Task process and cannot post the Task
    public function handle()
    {
        Log::info(__CLASS__ . ':handle start'[$this->data]);
        sleep(2);// Simulate some slow event processing
        // An exception thrown here will be captured by the upper layer and logged to the Swoole log, requiring the developer to manually try/catch
        $this->result = 'the result of ' . $this->data;
    }
    
    // Optional, the completion event, the logic after the task is processed, runs in the Worker process, can post the task
    public function finish()
    {
        \Log::info(__CLASS__ . ':finish start'[$this->result]);
        Task::deliver(new TestTask2('task2')); // Post other tasks}}Copy the code

Delivery task

// Instantiate TestTask and deliver it through Deliver, which is asynchronous and returns immediately after delivery, and the Task process continues processing the Handle logic in TestTask
use Hhxsv5\LaravelS\Swoole\Task\Task;
$task = new TestTask('task data');
// $task->delay(3); // Delay the delivery task by 3 seconds
// $task->setTries(3); // If an exception occurs, the total number of attempts is 3
$ret = Task::deliver($task);
var_dump($ret);// Check whether the delivery is successful
Copy the code

Q&A

Class swoole does not exist

  • In LaravelS, Swoole is an Http Server started in CLI mode, replacing FPM.
  • The app(‘ Swoole ‘) is invoked to retrieve the Swoole\ HTTP \ Server instance from the Laravel container. This instance is injected into the container only when LaravelS are started.
  • So, once unloaded from LaravelS, you will not be able to successfully call app(‘swoole’) due to cross-process situations as follows:
    • Code that runs on various command lines, such as the Artisan command line, the PHP script command line;
    • SAPI Log::info(‘PHP SAPI’, [php_sapi_name()]); .

Ps: Post tasks via webSocket connections

Previous article: Installing Laravels and using WebSockets in Laravel can be tested by modifying onMessage code

public function onMessage(Server $server, Frame $frame)
{
     $task = new TestTask('task data');
      // $task->delay(3); // Delay the delivery task by 3 seconds
      // $task->setTries(3); // An exception occurs
      $ret = Task::deliver($task);
      $server->push($frame->fd, $ret);
}
Copy the code

conclusion

Hopefully, this article has helped you learn how to use asynchronous task queues. 👍

Related articles

Install Laravels and use websockets under Laravel

How to use hhXSV5 / LARavel-S asynchronous task queue