Laravel : Command bus not going asynchronous - php

I'm using Laravel 5.1's Command bus to run a specific task (upload a file, validate then record it in my db) on a background process.
I tried uploading small csv file like 1.4kb (40 rows) and it worked.
When when i tried uploading a 1MB csv file (20000 rows), I noticed it is not running in background process. It waits for the job to be finished and then loads the correct page which is not the way I wanted it :(.
I think I followed the Laravel documentation on how to run a command bus in asynchronous process just by php artisan make:command PurchasePodcast --queued.
Reference
My code :
class ImportPricelistCommand extends Command implements SelfHandling, ShouldQueue
{
use InteractsWithQueue, SerializesModels;
Am i missing something? Please help.

In Laravel 5.1 the command bus is replace by jobs. You are using Laravel 5.1 and following 5.0 documentation.

The command bus was renamed jobs in Laravel 5.1, and the “command” stuff left to not break 5.0 applications. Jobs can be dispatched asynchronously as well as queued to be processed asynchronously.
Basically, you want to remove the ShouldQueue interface and InteractsWithQueue trait from your command/job class. As you may have gathered, this tells the dispatcher to queue them rather than process them synchronously.

Related

Laravel call to controller method from command line to run in background

I have method in laravel controller with some logic that need to run in the background in an infinite loop. With core php with was simple as we used to set it nohup php and call the php file to run in the background. How could we do the same with laravel to run the things in background.
Note: I cannot use cronjob for this as its an infinite loop which need to kept running in the background
Kinda hard to imagine what task you want to accomplish, but you could create laravel command, which could call controller method directly.
How to call controller method from within command
Check comment under the answer to resolve controller correctly.
Next, you should run command by the supervisor.
Supervisor tutorial
you can use define cronjob on your linux machine or use laravel schedule library . look at the following link :
https://laravel.com/docs/5.1/scheduling

How to use Jenssegers\Mongodb\Eloquent\Model instead of Illuminate\Database\Eloquent\Model in php artisan make:model command

I'm developing a webapp with Laravel and MongoDB (jenssegers/laravel-mongodb).
While creating a new model with php artisan make:model, the command uses Illuminate\Database\Eloquent\Model declaration in the file and every time I need to replace Illuminate\Database\Eloquent\Model with Jenssegers\Mongodb\Eloquent\Model manually.
Is there a way to automate the process?
It doesn't look like the package provides an Artisan command to create a MongoDB model stub, which seems like a bit of an oversight. However, it's not terribly hard to create this kind of generator command for Artisan yourself if you need it.
The model make command is at https://github.com/laravel/framework/blob/5.7/src/Illuminate/Foundation/Console/ModelMakeCommand.php and the stub file used to create it is at https://github.com/laravel/framework/blob/5.7/src/Illuminate/Foundation/Console/stubs/model.stub. If you extend the command class to replace the stub file with your MongoDB version, and amend the stub file to be a MongoDB model, then you should be able to create a command for generating MongoDB models. It might even be worth forking the package to add this and submitting a pull request to get it added to the package. I would refer to the part of the Laravel documentation that deals with Artisan for more details, as that describes the process of adding your own Artisan commands in detail.
Another approach would be to write your own class generator and then overwrite the command make:model
Add the following in the file routes/console.php to override the command
use Path\To\Class\MyCustomClassGenerator;
Artisan::command('make:model', function(){
new MyCustomClassGenerator();
$this->comment('new MongoDB Model generated');
});

Typo3 scheduler: Can i somehow execute Action of controllers of my extension with it? Or how to run my own code with it?

A while ago i was tasked to program a Typo3 extension to write so called .conf files for the icinga2 montoring tool (has nothing to do with Typo3). Still let me explain some parts of it: Basically the backend user needs to create records of records of specific classes and set values for each records properties. Then i need to process the records to create these .conf files with the specific values with a php script.
I was tasked to use the scheduler in Typo3 for this. And here come the problems: How do i use this? I checked the documentation (https://docs.typo3.org/typo3cms/extensions/scheduler/Introduction/Index.html), but i still can't wrap my head around how to use it for my task. I can easily write an Action in a controller of a class to be executed in the frontend and in turn generate the con files... basically manually without the scheduler. But where do i put my php code to be run by the scheduler? I somehow seem not to understand the basical principle of the scheduler. Can i just run an Action of a specific controller of a class of my extension like i would in the Frontend via the scheduler?
I would suggest you use a command controller for this task.
The documentation shows how to create a command controller, which also may accept arguments.
Command controller tasks can directly be executed by TYPO3 scheduler (see screenshot below)
You may even configure task arguments for command controller tasks in TYPO3 scheduler.

Laravel 5.1 jobs and listeners

So Laravel 5.1 changes the folders Commands to Jobs and Handlers to Listeners. What I'm a bit confused about is that the Listeners folder now only handles Events and not Jobs. Are all Jobs self-handling in 5.1? Or should i create a Handlers folder and handle the jobs in there?
For now, Jobs are self-handling. They are like Commands and implement the SelfHandling interface. The change is basically just a folder rename so people do not confuse it with other commands (e.g console commands). Try to think of a Job like a Command, since a command is actually performing a job.
Or still, you can create the app/Commands and app/Handlers directory and continue using commands as you are used to. Jobs and Commands are just different ways to do the same thing.

Laravel 4: Why is my artisan command called 5 times in succession?

Today I bumped into something strange regarding an Artisan Command's lifecycle.
I added an artisan command named cronjobs:MyCommand.
Created the necessary files and classes.
Class MyCommand extends BaseCommand.
BaseCommand is something I put up to benchmark command line operations.
It consists of a constructor and destructor. The constructor keeps track of
the starting time of the operation, the destructor logs the current time minus
the starting time in a log table.
To my surprise I found out that the __destruct() function on my BaseCommand is
called 5 times in total every time I issue the cronjobs:MyCommand via artisan.
Is this normal behavior and if so how am I supposed to take care of logging instead?
Further testing with Symfony 2's command class showed that this is a problem native to Symfony 2, not so much with laravel 4.
Using a constructor/destructor in an artisan command's class (or its parent) in any useful way seems impossible at this point in time.
Any input is still highly appreciated!
Issue seems still not fixed.. I had also creaed a basecommand with __destruct method which is gettigng executed multiple times.
I created a shutdown method in my basecommand and calling at the end of mycommand.

Categories