I want to initiate queue:work from my code level rather than using any artisan commands or other supervisor, daemon listeners, so i don't have to do extra queue listeners stuff on background.
Is this possible or not ? If not how can i able to make process working on background when needed
You can use following to call artisan command from the code.
$exitCode = Artisan::call('queue:work', [
'--option' => value,
]);
Note that if you call queue:listener, the code will enter into an infinite loop which would hang your existing process.
You can set the queue driver in the config/queue.php to sync. When you insert a job in the queue it will run immediately.
Related
I send mail as a cron job with Laravel. For this, when I want to use the last value I added in my resources/lang/de.json file in the mail blade template file(resources/views/mails/...blade.php), it gives an output as if such a value is not defined. However, if I use the same key in a blade file I created before, it works without any errors. In addition, the keys that I added to the same file (de.json) in the first time work without errors in the same mail blade file.
Thinking it's some kind of cache situation, I researched and found out that restarting the queue worker might fix the problem. However, both locally and on the server with ssh.
'php artisan queue:restart'
Even though I ran the command, there was no improvement.
Do you have any ideas?
Since queue workers are long-lived processes, they will not notice changes to your code without being restarted. So, the simplest way to deploy an application using queue workers is to restart the workers during your deployment process. https://laravel.com/docs/9.x/queues#queue-workers-and-deployment
but php artisan queue:restart will instruct all queue workers to gracefully exit after they finish processing their current job so that no existing jobs are lost. And I see a lot of issues with this command not to solve restart and deploy the worker.
So, Simplest way,
try to stop the worker manually (ctrl+C)
start the worker again with php artisan queue:work again.
might this help.
There is a migration task. User uploads file to the server, then it should be saved and migration command should be run async. The first path works well, there is an issue with the second part.
I've tried to put all code to console command and run it with
Artisan::call('user:migrate', ['user_id' => $userId]);
or
Artisan::queue('user:migrate', ['user_id' => $userId]);
the script works, but not async, controller's function waits for the end.
Also I've tried to create a Job and call it via:
$this->dispatch(new UserMigration($user));
and had the same result, script works but not async. Please help to realize how queues work and that approach is better for my task.
I've not created any queue migrations and configuration, because need this step just async calling.
In order to run tasks asynchronous, the general idea in Laravel is to push jobs to a queue (database table for instance) and have a background process pick them up.
See https://laravel.com/docs/8.x/queues for information directly from the source.
You can start a queue worker using:
php artisan queue:work
Note that this is an ongoing process that doesn't stop unless it's told to do so. This means that any changes you make to the code, will only be reflected once you restart that queue worker. It is therefore important to run php artisan queue:restart (or kill and start the running task) when you deploy your code.
So now your queue worker is running, you can for instance queue an email to be sent (like upon registration), and your controller will respond immediately instead of having to wait for the email to be sent.
Most if not all info can be found in the link above. If you are going to have lots and lots of background tasks, take a look at Laravel Horizon.
I am trying to run queued jobs, and pass additional parameters through the command line. My use case is this:
I have 4 running queue:work processes through supervisor. The jobs in my queue all require access to a proxy server, through which i can only have 4 processes running at any given time. When I start up a queued job, I have to find a process number (1 through 4) that is not currently being used, then run my command through that process.
I have been using a database table to store the processes and it has a column for in_use which keeps track of whether its being used, but the problem I'm seeing is when two queue:work commands run simultaneously, the same proxy process can be picked from the database for both.
What I want
php artisan queue:work --process=1
Then to somehow retrieve that argument inside the job, so I can run my 4 processes each in supervisor separately.
As a workaround, I have created a custom artisan command which will take the argument, but I then lose the queue functionality. I don't want to have to develop a custom queue process.
Is there a way to pass this argument? Or, alternatively, is there a way that I could pop jobs off the queue from within my custom artisan command, and then run them manually rather than through queue:work?
The problem could be solved by using dedicated queue's. So each queue has a specific proxy process attached to it. The only thing left is to create a function/process to determine to which queue the process should go.
https://laravel.com/docs/5.1/queues#pushing-jobs-onto-the-queue
Check out the part: Specifying The Queue For A Job
I am using Laravel Queues and I am using IronMQ for it. But I have little bit confusion about how this process.
I have set my default connection in queue.php as 'default' => 'iron' and also set iron settings in same file.
Now I use
$this->dispatch(new createEvents($data, $user));
while createEvents class is a job class created as explained in Laravel tutorial. Now when following code is executed
$this->dispatch(new createEvents($data, $user));
It successfully creates a queue in my ironmQ account under project.
Now here is my confusion starts. I have queued some task to that queue but now how will I run that queue? How will I run the task that is queued? Do I need to create some extra code for it or Do I need to do some settings for it. Please guide
You don't need to go to your server and run this command by hand, you need to have process that will keep running, and perform those jobs.
I would recomment "supervisord"
http://supervisord.org/
This programs is for launching a script and keep it running, even if it fails, it will relaunch it(until certain amount of failures of course)
After you install it, you should probably create this supervisor task file:
[program:queue]
command=php artisan queue:listen --tries=3 --env=your_environment
directory=/path/to/laravel
stdout_logfile=/path/to/laravel/app/storage/logs/supervisord.log
redirect_stderr=true
autostart=true
autorestart=true
You can do php artisan queue:listen it will start all listed queue
or if you specify the queue name php artisan queue:listen queue_name
Don't forget to run php artisan queue:failed-table. This will make failed_jobs table in your database.
So if anything goes wrong when the queue run it will save failed queue to the database.
If you want the failed queue to get insert the database add this when run listen:
php artisan queue:listen connection-name --tries=3
to run the failed queue php artisan queue:retry all
Hope i answer your question.
Once your job is in the queue, and according to your question it is, you have two simple options:
Run one or more queue listeners on the same/different servers (using supervisor is recommended in Laravel documentation, see sample configuration)
Run queue worker manually or automatically, on regular basis (crontab)
php artisan queue:work iron
This command will fetch one job from the queue and process it. You launch it again – it fetches one more, and so on.
If you don't do extra processing and your queue driver is not 'sync' – your job will never see the day light.
My advice – launch queue workers manually on your development/test machine, and use supervisor on production server.
If your project is small and it doesn't require great scalability, you may want to simply switch to 'sync' driver (jobs will be processed immediately). There is no need to make the infrastructure more complicated, unless there is real necessity!
I'm using Lumen queues. To start the jobs being processed, the docs indicate to run the queue listener from the command line like so:
php artisan queue:listen
I'd like to trigger my queue jobs processes directly from my code. How to do so?
You can call any artisan command you like directly from your code:
Artisan::call('queue:listen');
For information, read the docs.