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

41,368

Solution 1

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

Solution 2

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

Solution 3

If you are using a QUEUE_DRIVER diferent to sync and you want to dispatch a queue that you have created before, from your project folder run the command:

php artisan queue:work --queue=MyQueueName

Check this link to configure a database QUEUE_DRIVER

Solution 4

for Job With Parameter Optional

try this one

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

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

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $prefix = '\\App\Jobs\\';

        $jobClassName = trim($this->argument('job'));
        if(stripos($jobClassName,"/")){
            $jobClassName = str_replace('/','\\',$jobClassName);
        }
        $class = '\\App\\Jobs\\' . $jobClassName;

        if(!class_exists($class)){
            $this->error("{$class} class Not exists");
        }else {
            if ($this->argument('parameter')) {
                $job = new $class($this->argument('parameter'));
            } else {
                $job = new $class();
            }

            dispatch($job);
            $this->info("Successfully Dispatch {$class} ");
        }

    }
}

now U can try like this in terminal

1.Without Folder

php artisan job:dispatch appJob

2.With Folder

php artisan job:dispatch folderName/appJob 

3.Folder With Parameter

php artisan job:dispatch folderName/appJob 12
Share:
41,368

Related videos on Youtube

Oscar Gallardo
Author by

Oscar Gallardo

Developer and team leader, prefer PHP, but open mind for another backend languages, good in Architecture and Design tools

Updated on July 09, 2022

Comments

  • Oscar Gallardo
    Oscar Gallardo almost 2 years

    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?

  • mutiemule
    mutiemule about 5 years
    Realised I was creating different queues, adding thhe chunk --queue=QueueName is very key
  • Leo Gasparrini
    Leo Gasparrini over 4 years
    Awesome, but I got - mongodb/mongodb 1.5.2 requires ext-mongodb ^1.6 -> the requested PHP extension mongodb has the wrong version (1.3.4) installed.
  • mixel
    mixel over 4 years
    @LeoGasparrini My package uses only "laravel/framework": "^5.5" dependency. It's more likely an issue with your project dependencies.
  • Nico Haase
    Nico Haase almost 4 years
    Please add some further explanation to your answer - it does not contain any information how this should be run using CLI
  • Amitesh Bharti
    Amitesh Bharti almost 4 years
    Hey Nico, I've added more details, hope that helps.