Bob
Bob

Reputation: 8704

Process dispatched jobs from queue automatically

I would like to process jobs from queue automatically.

I have this command:

dispatch($importUserJob);

Is it possible to run these jobs automatically or after few seconds?

Upvotes: 0

Views: 719

Answers (1)

Tim Lewis
Tim Lewis

Reputation: 29258

When a Job is dispatch()'ed, it is configured to be handled by the process configured in config/queue.php:

return [
  'default' => env('QUEUE_CONNECTION', 'sync'),
  'connections' => [
    'sync' => [...],
    'database' => [...],
    'beanstalkd' => [...],
    'sqs' => [...],
    'redis' => [...]
  ]
];

By default, this is set to sync(), and Jobs are automatically processed when run. In many production environments, this is set to database, and uses a jobs table (see https://laravel.com/docs/7.x/queues#driver-prerequisites Database section).

To process a Job dispatched to the Queue, you need a listener:

php artisan queue:work

This is a background process that automatically listens for dispatch(), and using the jobs table, processes the Job and it's 'payload'. There are a variety of configuration options available, but they all function similarily.

Lastly, if you want to delay a Job processing, using the ->delay() option:

dispatch(new ExampleJob(...))->delay(now()->addMinutes(10));

This will set the jobs.available_at column to the appropriate timestamp, and defer running until that time is reached.

Upvotes: 1

Related Questions