PHP code example of ctfang / swoft-queue

1. Go to this page and download the library: Download ctfang/swoft-queue library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ctfang / swoft-queue example snippets



namespace App\Job;

use Swoft\Log\Helper\CLog;
use Swoft\Log\Helper\Log;
use Swoft\Queue\Annotation\Mapping\Job;
use Swoft\Queue\JobAbstract;
use Swoole\Coroutine;

/**
 * Class NewFeedJob
 * @package App\Job
 * @Job(queue="test",name="demo")
 */
class TestJob extends JobAbstract
{
    /**
     * 业务处理
     *
     * @param $msg
     */
    public function handle($msg): void
    {
        $id = uniqid();

        CLog::info($id." 启动 ".json_encode($msg));

        Coroutine::sleep(5);

        CLog::info($id." 结束 ");
    }
}

$queue = "test";
$job   = "demo";
$msg   = ['str'=>"这里传入Job的内容"];
$push  = [$queue,$job,$msg];
// 'queue' 是配置对应的 redis_key 值 
Redis::lPush('queue',json_encode($push));
bash
php bin/swoft queue:start