Home > Back-end >  How do I use a job in command kernel?
How do I use a job in command kernel?

Time:07-08

I created a job in Laravel 8.x

<?php
namespace App\Jobs;

use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Modules\Quiz\Entities\Quiz;

class SetQuizStatus implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $quiz;

    /**
     * Create a new job instance.
     *
     * @return void
     */

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        Quiz::where('publish_datetime', '>=', Carbon::now())->update([
            'status' => EnumHelper::get('quiz.status.published'),
        ]);
    }
}

Then in app\Console\Kernel.php I scheduled this job in the schedule function:

protected function schedule(Schedule $schedule)
    {
        $schedule->command('backup:clean')->daily()->at('04:00');
        $schedule->command('backup:run')->daily()->at('05:00');
        $schedule->job(new SetQuizStatus)->everyMinute();
    }

I want to change this quiz status if the publish_datetime has been reached. I run php artisan schedule:run and it's listed in php artisan schedule:list but nothing happens.

.env QUEUE_CONNECTION is set to database.

What am I missing?

CodePudding user response:

I believe handle() wasn't being called when I instantiated SetQuizStatus. This fixed it and it now works. My .env file QUEUE_CONNECTION can be set to sync as well.

public function __construct()

{

    $this->handle();

}

CodePudding user response:

Do you run horizon? If you sent event to background you need horizon

  • Related