PHP code example of friendsofhyperf / async-queue-closure-job

1. Go to this page and download the library: Download friendsofhyperf/async-queue-closure-job 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/ */

    

friendsofhyperf / async-queue-closure-job example snippets


use function FriendsOfHyperf\AsyncQueueClosureJob\dispatch;

// Simple closure dispatch
dispatch(function () {
    // Your job logic here
    var_dump('Hello from closure job!');
});

// With max attempts (retry limit)
dispatch(function () {
    // Your job logic here
})->setMaxAttempts(3);

use function FriendsOfHyperf\AsyncQueueClosureJob\dispatch;

// Chain multiple configurations
dispatch(function () {
    // Your job logic here
})
    ->onConnection('high-priority')
    ->delay(60) // Execute after 60 seconds
    ->setMaxAttempts(5);

use function FriendsOfHyperf\AsyncQueueClosureJob\dispatch;

$condition = true;

// Execute only when condition is true
dispatch(function () {
    // Your job logic here
})
    ->when($condition, function ($dispatch) {
        $dispatch->onConnection('conditional-connection');
    });

// Execute only when condition is false
dispatch(function () {
    // Your job logic here
})
    ->unless($condition, function ($dispatch) {
        $dispatch->delay(30);
    });

use function FriendsOfHyperf\AsyncQueueClosureJob\dispatch;

// Automatic dependency injection
dispatch(function (UserService $userService, LoggerInterface $logger) {
    $users = $userService->getActiveUsers();
    $logger->info('Processing ' . count($users) . ' users');
    // Process users...
});

// With custom parameters
dispatch(function (UserService $userService, int $userId) {
    $user = $userService->find($userId);
    // Process user...
})->setMaxAttempts(3);

// Old (still supported)
use function FriendsOfHyperf\AsyncQueueClosureJob\dispatch;

// New (recommended for new code)
use function FriendsOfHyperf\Support\dispatch;