This question already has answers here:
How to keep Laravel Queue system running on server
(20 answers)
Closed 4 years ago.
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?
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
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.
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
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.. 😛
Related
How can I run a service-based command after the build process in gitlab-ci.yml?
For example, i'd like to run:
php artisan queue:listen --timeout=0 &
The issue is the build runs perpetually and does not finish as it waits for the results of this command (even though this command never finishes).
Is there anyway I can run it as a background task? I tried nohup with no luck.
As mentioned here:
Process started with Runner, even if you add nohup and & at the end, is marked with process group ID.
When the job is finished, the Runner is sending a kill signal to the whole process group.
So any process started directly from CI job will be terminated at job end.
Using a systenter code hereemd service (as in this same page) remains an option, if you control the target server.
With VonC's help - this is the approach I took.
I use Alpine Linux so slightly different to the link he provided, but same approach.
I created a file in /etc/init.d and gave it chmod +x permissions.
With the following contents:
#!/sbin/openrc-run
command="php /var/www/artisan queue:listen"
command_args="--timeout=0"
command_background=true
pidfile="/run/${RC_SVCNAME}.pid"
I then ran it with rc-service laravel-queue start within the gitlab-ci configuration file.
I know you can stop all workers in Laravel using queue:restart. But I'm looking for a way to stop all workers working on a specific queue. Something like this:
php artisan queue:restart --queue=my_queue
As far as I read the documentation,
php artisan queue:clear redis --queue=emails
is only available for The SQS, Redis, and database queue drivers.
But this command is clearing, not stopping.
I need to convert/resize uploaded images using Laravel so they are available during the next seconds or minute or so...
I was wondering, to have less pressure on the server, is it better to do this using Laravel Schedules or Queues
If Queues is the way to go, how to dispatch? (delayed?)
If you are expecting multiple users uploading multiple images concurrently, in that case, I would stick with Queue for this task. Here's a link to Laravel 7.x Queue, they have really good documentation.
Step 1: php artisan queue:table & php artisan migrate. This will create 2 tables: jobs and failed_jobs.
Step 2: Create a Job php artisan make:job ResizeImage. This will create a new file under jobs folder. It's the implements ShouldQueue that's gonna do the magic for you.
Step 3: To make a dispatch call. ResizeImage::dispatch($params);. To delay, add ->delay(now()->addMinutes(10));
Step 4: In your .env file, change QUEUE_CONNECTION=sync to QUEUE_CONNECTION=database. This config will be used by config/queue.php file.
Step 5: Clear config and cache. php artisan config:clear php artisan cache:clear
Things to understand:
When you dispatch a job, a new row will be added to jobs table. Whatever you add to the handle() method in your jobs file will be added to the payload column of the table.
To trigger the queue, you can either add a supervisor to your server or a simple php artisan queue:work will run the jobs.
Please go through the docs, they have really good + deep + better explanation with examples. Cheers!
I have this process setting.
php artisan queue:work beanstalkd --sleep=3 --tries=1 --timeout=0 --queue=medium,messages
I also have a job with the setting
public $timeout = 100000000;
But the job is stopped much before that with message has been attempted too many times or run too long. The job may have previously timed out Which was run for the first time.
I also have this in php.ini
max_execution_time = 0
What am I missing here?
Console
Things I want to execute from the command line (As you mentioned with your job creates more jobs). But the thing is, you could extract the business logic of it to a command.
Example: A console command with fires a command to fetch images from Imgur. The Class FetchImages contains the actual business logic of fetching images.
Command
Class which contains the actual logic. You should also be able to call this command from your application with app()->make(Command::class)->handle().
Example: Command mentioned in Example 1. Contains logic which does the actual API calls to Imgur and process returned data.
Jobs
Laravel 5.0 so jobs weren't a thing back then. But as I see it, Jobs are like commands but they are queued and can be dispatched. (As you may have seen in those examples, those commands implement your mentioned Interfaces SelfHandling and ShouldBeQueued).
I see changes in Commands and Jobs are quite difficult to understand.
EDIT:
From the Laravel Docs:
The app/Commands directory has been renamed to app/Jobs. However, you are not required to move all of your commands to the new location, and you may continue using the make:command and handler:command Artisan commands to generate your classes.
Likewise, the app/Handlers directory has been renamed to app/Listeners and now only contains event listeners. However, you are not required to move or rename your existing command and event handlers, and you may continue to use the handler:event command to generate event handlers.
By providing backwards compatibility for the Laravel 5.0 folder structure, you may upgrade your applications to Laravel 5.1 and slowly upgrade your events and commands to their new locations when it is convenient for you or your team.
I'm trying to use Laravel Queues for sending emails using the database driver, I have already configured it, run the migration for the "jobs" table and when I run this:
Mail::to($user->email)->queue(new CompraRealizadaAdmin(Cart::content(), $monto_descuento, $envio, $user_array, $direccion, $compra));
A record is added on the "jobs" table, but, how do I run the queue on the database table?, I understand that for triggering it at the moment it is added, I will need to run the command php artisan queue:listen, or if I need to run all the ones that are still on queue, I will use php artisan queue:work.
But how do I run the command without the need to open terminal and keep it open until it has finished...?
I had the idea of creating a schedule and run it every minute and just execute the code: Artisan::call('queue:work'); but that does not work.
Any ideas?
Depending on your needs, preferences and your target OS you can use
supervisord (cross platform)
upstart / systemd (linux)
launchd (OS X)
or alike services to manage your queue worker processes.
In fact Laravel documentation explains in great detail how to install and configure supervisord for this.
It depends on which OS you are working on for Ubuntu or linux you can use supervisor and hup.
butt be careful you have to run hup every time you reboot your machine.
thats how you can run this command. hup php artisan queue:work.
Hope this helps