PHP code example of tourze / symfony-async-bundle

1. Go to this page and download the library: Download tourze/symfony-async-bundle 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/ */

    

tourze / symfony-async-bundle example snippets


return [
    // ...
    Tourze\Symfony\Async\AsyncBundle::class => ['all' => true],
];



use Symfony\Component\Messenger\MessageBusInterface;use Symfony\Component\Messenger\Stamp\AsyncStamp;use Tourze\AsyncCommandBundle\Message\RunCommandMessage;

class MyController
{
    public function __construct(
        private readonly MessageBusInterface $messageBus
    ) {}

    public function someAction()
    {
        // Create a message to run a command asynchronously
        $message = new RunCommandMessage();
        $message->setCommand('app:my-command');
        $message->setOptions([
            '--option1' => 'value1',
            '--option2' => 'value2'
        ]);

        // Dispatch to queue
        $this->messageBus->dispatch($message, [
            new AsyncStamp()
        ]);

        return 'Command queued for execution';
    }
}



namespace App\Service;

use Tourze\Symfony\AopAsyncBundle\Attribute\Async;

class ReportGenerator
{
    #[Async(retryCount: 3, delayMs: 5000)]
    public function generateLargeReport(int $userId): void
    {
        // This method will be executed asynchronously
        // with 3 retry attempts and 5-second delay

        // ...time-consuming operations
    }
}

$reportGenerator->generateLargeReport(123);
// This returns immediately, with the actual work happening in the background