Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Laravel keeps calling schedule() with every Artisan Command?

I have one table called dc_user_meta and I've created one artisan command and scheduled it in kernel.php. Just after cloning the repository, when I try to run PHP artisan migrate, I get this error.

  [Illuminate\Database\QueryException]                                                                                                              
  SQLSTATE[42S02]: Base table or view not found: 1146 Table 'database.dc_user_meta' doesn't exist (SQL: select * from `dc_user_met  
  a` where `meta_key` = usage_in_days)

Not only php artisan migrate but I am unable to run any artisan command at all! I don't know why PHP keeps calling schedule method every time I try to execute any artisan command.

Here in this case, What I can do to solve this error is put the cover over my logic in schedule method just like this.

if(Schema::hasTable('dc_user_meta')){
    // Code here
}

But I don't think it's good in Long run. What's the right way to solve this error?


UPDATE: I just tried covering call to command in kernel.php just like this but still no success!

if(Schema::hasTable('dc_user_meta')){
    $schedule->command('usage:update')->daily();
}

UPDATE: I got the solution. But I don't think it's the answer to the question. It solves my problem but I don't think it's standard Solution. I just covered by Command login just like this.

if(Schema::hasTable('dc_user_meta')){
    // Command Logic
}

Any specific answer to why Laravel calls schedule() with every artisan command and how to solve the error in a standard way if something like this happens!

like image 868
p01ymath Avatar asked Jul 03 '17 03:07

p01ymath


People also ask

How do I stop the php artisan serve command?

Press Ctrl + Shift + ESC. Locate the php process running artisan and kill it with right click -> kill process. Reopen the command-line and start back the server. Note that you should be able to kill the process just by sending it a kill signal with Ctrl + C.

What does php artisan schedule Run do?

The schedule:run Artisan command will evaluate all of your scheduled tasks and determine if they need to run based on the server's current time.

Which file is responsible for scheduling any command in Laravel?

The process of scheduling tasks in Laravel is handled by files such as Kernel. php , Schedule. php , Event. php , ScheduleRunCommand.


1 Answers

Technically the schedule method ist called via the constructor of Illuminate\Foundation\Console\Kernel ( This is the parent class of app\Console\Kernel.php)

So every time the console Kernel is instantiated, the schedule() method gets executed.

Let's see what gets executed in which scenario ( $schedule->call() can be replaced with $schedule->command() or $schedule->exec()):

protected function schedule(Schedule $schedule)
{
    // everything that is inside the schedule function is executed everytime the console kernel is booted.

    // gets exectuted every time
    \App\User::where('foo', 1)->get();


    $schedule->call(function() {
        // gets executed for every call to php artisan schedule:run
        \App\User::where('foo', 1)->get();
    });

    $schedule->call(function() {
        // gets executed for every call to php artisan schedule:run
        // IF the closure in the when() function is true;

        \App\User::where('foo', 1)->get();
    })->when(function() {
         // if true is returned the scheduled command or closure is executed otherwise it is skipped
         \Schema::hasColumn('user', 'foo');
    });

}

But why HAS the schedule command to be exectuted with every command?

Well, obviously php artisan schedule:run is a console command itself. So it definitely needs information about scheduled commands.

Also other commands could need information about scheduled commands... For example if you want to write an artisan command list:scheduledTasks. This command would require that all scheduled commands have been added to the console schedule list.

Maybe there are several other (internal) arguments why the schedule function has to run everytime. ( I did not dig too deep in the source code... )
Nevertheless... information about scheduled commands could be useful to a variety of use cases.

like image 137
shock_gone_wild Avatar answered Nov 15 '22 13:11

shock_gone_wild