Running commands from Controller async - laravel-5

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.

Related

Recently added json language file values are not updated in email blade

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.

Laravel how to stop a scheduled task in php code

I have several scheduled tasks runs in background mode using Laravel scheduler, I want to use php code to control which one to start and which one to stop, I know how to start a scheduled task, but how to stop a scheduled task in PHP code?
I don't see it in the Laravel scheduler doc.
The best way would be to create a flag somewhere (Model, Cache) and check it when the task gets processed.
The queue in Laravel is just that, a queue. It is not meant to hold state or other information.

Laravel run Queue work from code

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.

Laravel + Beanstalkd queue - buried jobs getting kicked

I'm using Laravel 4.1 with beanstalkd to run some intensive Photoshop PSD file processing in the background. I also installed phpBeanstalkdAdmin to monitor what's going on the queue.
The jobs being processed take around 7-10 minutes, but I've noticed that some of my jobs get restarted, even when they are still busy running.
Keeping an eye on phpBeanstalkdAdmin, I can see the job being buried when queue:listen picks up the job, but after a while it gets kicked back up, to ready.
To start the listening to the queue, I'm using:
$ ./artisan queue:listen --queue=my_queue --memory=512 --timeout=600
Inside the queue handler's fire() method, I'm simply starting an artisan command with
Artisan::call(
'tms:parse',
[
'--alias' => $data['alias'],
'--notify' => $data['email']
]
);
and calling
if ($job != null) {
$job->delete();
}
once the job is done. But I can't figure out why it gets kicked to ready halfway through the job being busy.
Does Laravel kick the job back to ready after nothing happens to it in a preconfigured interval?
Seems like this issue was resolved a while back, no one bothered updating the Laravel documentation however:
https://github.com/laravel/framework/pull/3766

How to fire Laravel Queues with beanstalkd

I'm pretty new to the whole Queue'd jobs thing in Laravel 4. I have some process heavy tasks I need the site to run in the background after being fired by the user doing a particular action.
When I was doing the local development for my site I was using this:
Queue::push('JobClass', array('somedata' => $dataToBeSent));
And I was using the local "sync" driver to do it. (The jobs would just automatically fire, impacting on the user experience but I assumed when going into the production phase I could switch it to beanstalkd and they would then be run in the background)
Which brings me to where I'm at now. I have beanstalkd set up with the dependencies installed with composer and the beanstalkd process listening for new jobs. I installed a beanstalk admin interface and can see my jobs going into the queue, but I have no idea how to actually get them to run!
Any help would be apprieciated, thanks!
This is actually a really badly documented feature in Laravel.
What you actually need to do is have the JobClass.php in a folder that is auto-loaded, I use app/commands, but they can also be in app/controllers or app/models if you like. And this function needs to have a fire event that takes the $job and $data argument.
To run these, simply execute php artisan queue:listen --timeout=60 in your terminal, and it will be busy emptying the queue, until it's empty, or it's been running for longer then 60 seconds. (Small note: The timeout is the time-limit to start a queue, so it may run for 69 seconds if 1 job takes 10 seconds.
If you only want to run 1 job (perfect for testing), run php artisan queue:work
There are tools like Supervisord that make sure your job handlers keep running, but I recommend to just make a Cron task that starts every X minutes based on how fast the data needs to be processed, and on how much data comes in.
Keep in mind you need to path your artisan.
php /some/path/to/artisan queue:work

Resources