简体   繁体   中英

Laravel Task schedule with cron column in database

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?

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!

Seems that there's some syntax errors (maybe some classes aren't listed in use?)

Have you checked laravel and PHP logs? Most likely there will be some explanations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM