PHP code example of karlhsu / think-closure-dispatch

1. Go to this page and download the library: Download karlhsu/think-closure-dispatch 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/ */

    

karlhsu / think-closure-dispatch example snippets


use function Karlhsu\ClosureDispatch\dispatch;

// 基本使用
dispatch(function() {
    echo "Hello World";
})->push();

// 延迟执行
dispatch(function() {
    echo "延迟执行的任务";
})->delay(60)->push();  // 60秒后执行

// 指定队列
dispatch(function() {
    echo "在指定队列中执行的任务";
})->onQueue('emails')->push();

// 使用 Carbon 设置延迟时间
use Carbon\Carbon;

dispatch(function() {
    echo "在指定时间执行的任务";
})->delay(Carbon::now()->addMinutes(30))->push();


return [
    'default'     => 'sync',
    'connections' => [
        'sync'     => [
            'type' => 'sync',
        ],
        'database' => [
            'type'       => 'database',
            'queue'      => 'default',
            'table'      => 'jobs',
            'connection' => null,
        ],
        'redis'    => [
            'type'       => 'redis',
            'queue'      => 'default',
            'host'       => '127.0.0.1',
            'port'       => 6379,
            'password'   => '',
            'select'     => 0,
            'timeout'    => 0,
            'persistent' => false,
        ],
    ],
    'failed'      => [
        'type'  => 'none',
        'table' => 'failed_jobs',
    ],
];

// 不推荐
dispatch(function() use ($largeData) {
    // 处理大量数据
})->push();

// 推荐
$dataId = saveToDatabase($largeData);
dispatch(function() use ($dataId) {
    $data = getFromDatabase($dataId);
    // 处理数据
})->push();