The Task Scheduling feature should be working properly in Ravel, but it does not work.

Asked 2 years ago, Updated 2 years ago, 38 views

I used the task scheduling function in Ravel to create a random update of the quiz, but the task schedule should be working normally, but there is no change at all.Is the description of handle() in TimeQuiz.php strange?
If you write the same program in Controller.php, it works normally.

phpartisan schedule:run results

Running scheduled command: '/usr/bin/php' 'artisan' command:quiz>'/dev/null'2>&1

TimeQuiz.php

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
useApp\Quiz;

class TimeQuiz extensions Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature='command:quiz';

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

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $quiz=Quiz::with('answer')
            ->inRandomOrder()
            ->get();

            return$quiz;
    }
}

Kernel.php

<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use Illuminate\Support\Facades\Log;

class Kernel extensions ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        'App\Console\Commands\TimeQuiz'
    ];

    /**
     * Define the application's command schedule.
     *
     * @param\Illuminate\Console\Scheduling\Schedule$schedule
     * @return void
     */
    protected function schedule (Schedule$schedule)
    {
        $schedule->command('command:quiz')
        ->everyMinute()
        ->onSuccess(function(){
            Log::info('success');
        });
    }

laravel

2022-09-30 14:38

1 Answers

It's the same as doing nothing with the current handle().

Keep commands + schedules in cache forever.

$quiz=Quiz::with ('answer')
            ->inRandomOrder()
            ->get();

cache()->forever('quiz',$quiz);

Save it and take it out where you want it to be used.

$quiz=cache('quiz');

Save on the back (updated regularly on schedule) and take it out of the cache when using it, so it's fast.
This is an advantage, so in the case of this question, there is no point in executing it with a command.
If it's random, it's better to get it for each user's access, so there's no cache.


2022-09-30 14:38

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.