7

I have jobs to send several emails.

In my controller I call the job:

dispatch(new SendStartPatEmail($data));

And record is saved in table jobs.

But to execute the job I have to run php artisan queued:work manually. How can I do this automatically?

user3242861
  • 1,839
  • 12
  • 48
  • 93
  • you can achieve this by setting up the command to run in the background and almost permanently in that even when the user logs out the work will still keep on running using this command : nohup php artisan queue:work --daemon & – stanley mbote Sep 28 '21 at 09:23
  • If you want to log the output to a log you can do that by modifying the above command to this : nohup php artisan queue:work --daemon > /dev/null 2>&1 & – stanley mbote Sep 28 '21 at 09:25

4 Answers4

6

There are lots of different ways, all depending on the environment that you're using. Laravel tends to recommend using Supervisor to monitor your queue workers and keep them running.

Alternatively, you may wish to have your jobs execute immediately, instead of adding them to a queue. You can do this by setting your queue driver to sync, either in your config:

config/queue.php

'default' => env('QUEUE_DRIVER', 'sync'),

or in your .env file (assuming your config is set up as above)

.env

QUEUE_DRIVER=sync
Jonathon
  • 15,873
  • 11
  • 73
  • 92
2

Already answered here

Yes, if you use Linux you can use for example supervisor which will run php artisan queue:listen (you need to add this command to supervisor configuration file) and it will make sure all the time this command is running.

spartanz51
  • 311
  • 3
  • 9
  • A simple way you may consider to run in background for linux systems is adding ampersand(&) at the end of the artisan command. For this case ``php artisan queue:work &`` – Dennis Kiprotich Feb 15 '22 at 06:52
0

php artisan queue:work is a simple command that listens to a queue and executes some jobs.

What's the whole concept?

You can run this simple command on the background and all jobs in queue will be executed.
But running a process (queue:work) on the background is not always safe.

Why? because there is always the chance that the process may be terminated or stuck because of a memory leak.

In this case laravel recommends the use of a Supervisor .The supervisor is another process working like a service.It is responsible to check whether the process that php artisan queue:work creates, works normally or it should be restarted.

This way php artisan queue:work runs on the background but there is a mechanism (supervisor) which can restart the process in case something goes wrong

Dionisis K
  • 614
  • 5
  • 17
0

There is the dispatch_now( ... ) method for specifying those jobs you want run synchronously.

I don't care for underscores, so I usually create a helper method dispatchNow( ... ) that calls the underscore version..

parker_codes
  • 3,267
  • 1
  • 19
  • 27