Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manually run a laravel/lumen job using command line

I have created a Job file in the folder app/Jobs/MyJob.php and i would like to run it just once, if its possible using command line.

Something like:

> php MyJob:run

what command should I use to run a method in this file or the handle?

like image 907
Oscar Gallardo Avatar asked Apr 11 '17 22:04

Oscar Gallardo


People also ask

How do I run a Laravel server command?

Via Laravel Installer First, download the Laravel installer using Composer. Make sure to place the ~/. composer/vendor/bin directory in your PATH (or C:\%HOMEPATH%\AppData\Roaming\Composer\vendor\bin if working with Windows) so the laravel executable is found when you run the laravel command in your terminal.

How do I run an artisan command?

To create a new artisan command, we can use the make:command artisan command. This command will make a new command class within the app/Console/Commands catalog. In case the directory does not exist in our laravel project, it'll be automatically made the primary time we run the artisan make:command command.


2 Answers

UPDATE

I created mxl/laravel-job composer package providing Laravel command for dispatching jobs from command line:

$ composer require mxl/laravel-job
$ php artisan job:dispatch YourJob # for jobs in app/Jobs directory (App\Jobs namespace)
$ php artisan job:dispatch '\Path\To\YourJob' # dispatch job by its full class name
$ php artisan job:dispatchNow YourJob # dispatch immediately
$ php artisan job:dispatch YourJob John 1990-01-01 # dispatch with parameters

Package also provides a way to reduce boilerplate by using base Job class and has FromParameters interface that allows to implement command line parameters parsing and use job from PHP code and command line simultaneously.

Read more about its features at the package GitHub page.

OLD ANSWER

Run

php artisan make:command DispatchJob

to create special artisan command that runs jobs.

Open created DispatchJob.php file and define DispatchJob class like this:

class DispatchJob extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'job:dispatch {job}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Dispatch job';

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $class = '\\App\\Jobs\\' . $this->argument('job');
        dispatch(new $class());
    }
}

Now you should start queue worker:

php artisan queue:work

and after that you can run jobs from command line:

php artisan job:dispatch YourJobNameHere
like image 117
mixel Avatar answered Sep 30 '22 19:09

mixel


The most simple way is to call with the use of Tinker

It's Laravel command using for debugging, use it by running below command from from project root

php artisan tinker

To dispatch job on a specific queue from tinker

\Queue::pushON('rms', new App\Jobs\UpdateRMS());

first parameter - Queue name

second parameter - job name

Dispatch multiple jobs at once to a specific queue

\Queue::bulk([new App\Jobs\UpdateRMS(), new App\Jobs\UpdateRMS()], null, 'rms');

Demo (product-service is my project name) Just follow it enter image description here Find a dispatched job in the queue) my case queue is configured as Redis

enter image description here

like image 40
Amitesh Bharti Avatar answered Sep 30 '22 21:09

Amitesh Bharti