0

Our system has some scheduled jobs, one of the jobs should run as a user to get authorized. What is the best way to do it?

App\Console\Kernel schedule has these,.

....
        $schedule->job(new EveryMinuteJob)->everyMinute();
        $schedule->job(new EveryFiveMinuteJob)->everyFiveMinutes();
....

the EveryMinuteJob is being handled in multiple places which are authorized only for admin role users. So in the EveryMinuteJob, using Auth::login() and ::logout() to run the job as admin as below. Is this the only way or any other best practice to do?

class EveryMinuteJob implements ShouldQueue {

    public function handle()
    {
            ..........

            Auth::login(User::where('super_admin',true)->first());

            ..........

            Auth::logout();

            ..........

    }
}

OutForCode
  • 381
  • 9
  • 26

1 Answers1

2

You can use like this

$schedule->job(new EveryMinuteJob)->everyMinute()->user($user);
tringuyen
  • 791
  • 5
  • 5