laravel task scheduling from database

Published 1 year ago by lorvent

Hi, I am building a service where people can register to fetch API at regular intervals like 5mins, 15mins etc

and since every person uses a different schedule, i can't manually write all scheduling into kernel.php

is there any way to utilize database for task scheduling?

any help is appreciated.

selmonal

Does your client call your api every specific minutes right? I think its no need to use laravel schedule service. I think It's just be a simple javascript loop with setTimeout().

lorvent
lorvent
1 year ago (13,385 XP)

if i have 100 subscribers and each subscriber sets their own time, i can't make use of javascript

selmonal

If i understand your problem correctly, Try this:

protected function schedule(Schedule $schedule)
{
    $schedule->call(function () {
        
        $schedules = UserFetchSchedule::whereRaw('DATE_ADD(last_fetched_at, INTERVAL wait_minutes MINUTE) > now()')->get();
        
        foreach($schedules as $schedule) {
            // Do your api stuffs here.
        }
    })->everyMinutes();
}
lorvent
lorvent
1 year ago (13,385 XP)

Thanks, i will try this and update the post.

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