booni3
3 months ago
333
2
Laravel

Task schedule with cron column in database

Posted 3 months ago by booni3

I have a "reportSchedule" model which contains the report name and a cron_request column such as */15 * * * *.

I want to be able to adjust the cron within the database and affect the times which the report is requested. For example, the following is working from directly within the console/Kernel.php:

ReportSchedule::all()->each(function(ReportSchedule $reportSchedule) use($schedule){
    if(isset($reportSchedule->cron_request)){
        $schedule->call(function() use ($reportSchedule) {
            ReportRequestNow::dispatch($reportSchedule);
        })->cron($reportSchedule->cron_request);
    }
});

However, having the model called from directly within the kernel causes other issues. For example database migrations now do not work and errors are thrown when caching the routes or running route:list. In general, it does not seem to like it!

So my idea was either create a seeder job or put this into its own schedule, however neither work.

// Doesnt work - the every minute schuedle is called but ReportRequestNow is never reached.

$schedule->call(function() use($schedule){
    ReportSchedule::all()->each(function(ReportSchedule $reportSchedule) use($schedule){
        if(isset($reportSchedule->cron_request)){
            $schedule->call(function() use ($reportSchedule) {
                ReportRequestNow::dispatch($reportSchedule);
            })->cron($reportSchedule->cron_request);
        }
    });
})->everyMinute();

// Also does not work

$schedule->job(new ReportScheduleSeeder(), 'high')->everyMinute();

Can anyone suggest a why this does not work or how to get it working?

Please sign in or create an account to participate in this conversation.