Hellocode's avatar

How to create Queue in Laravel

Hello everyone, I would like to ask how to write a code that limits only three executions per 10 seconds after creating a Queue in Laravel, and determines whether it is true or false in the handle to end the Queue or the code that expires after 120 seconds? Thanks

0 likes
4 replies
LaryAI's avatar
Level 58

To create a queue in Laravel that limits only three executions per 10 seconds and determines whether it is true or false in the handle to end the queue or expires after 120 seconds, you can follow these steps:

  1. Create a Job: First, create a job using the Artisan command.

    php artisan make:job ExampleJob
    
  2. Implement the Job Logic: Open the generated job file located in app/Jobs/ExampleJob.php and implement the logic. You can use Laravel's rate limiting features to limit the job executions.

  3. Dispatch the Job: You can dispatch the job from a controller, command, or any other part of your application.

    use App\Jobs\ExampleJob;
    
    // Dispatch the job
    ExampleJob::dispatch();
    
  4. Configure Queue Settings: Ensure your queue settings are properly configured in config/queue.php and that you have a queue worker running.

    php artisan queue:work
    

This setup ensures that the job is rate-limited to three executions per 10 seconds and will timeout after 120 seconds if not completed. The job will also end if the performTask method returns false.

Hellocode's avatar

@martinbean Hello, thank you for your reply. I've read the documentation and tried it, but it didn't meet my expectations. So I came to the group to ask for help. Thank you.

martinbean's avatar

@Hellocode You‘re literally asking about limiting a job and timeouts, which Laravel queues support, and is described in the documentation. You just need to actually write the code.

Please or to participate in this conversation.