jeudi 30 décembre 2021

Laravel 5.8 scheduler is not running commands

This question is asked before but non of the answers work for me. This is the Kernel.php

<?php

namespace App\Console;

use App\Console\Commands\TestRunKernel;
use App\Log;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;


class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        TestRunKernel::class
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        Log::create([
            'body' => 'test for log'
        ]);
        $schedule->call(function () {
           Log::create([
               'body' => 'test2 for log'
           ]);
        })->everyMinute();
        $schedule->command('test:run')->everyMinute();
    }

    /**
     * Register the commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        $this->load(__DIR__ . '/Commands');

        require base_path('routes/console.php');
    }
}

and this is the command

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Modules\User\Entities\User;

class TestRunKernel extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'test:run';

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

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $user = User::find(1);
        $user->update([
            'file' => 1
        ]);
    }
}

The Kernel.php is running by cron job and test for log message is written in Log model. Hence I am sure the file is running by server. But when I use $schedule it doesn't work and non of the commands work. How can I fix this?

Thanks in advance.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire