Rainieren
1 month ago
146
3
Laravel

Laravel 8 - Dispatch a scheduled job every Minute

Posted 1 month ago by Rainieren

I am working on fetching all my GitHub repos with a job every minute so when I make changes to any repo these changes automatically go to my website as well. I created a job called ProcessGithub which uses GrahamCampbell's package to fetch the repos like this:

$repos = GitHub::connection('main')->me()->repositories();

I tested the line above and it correctly returns all my repos so no issues there.

The handle method within my Job looks like the one below and I added a job schedular to my Kernal.php

public function handle()
    {
        $repos = GitHub::connection('main')->me()->repositories();

        foreach($repos as $repo) {
            DB::table('projects')
                ->updateOrInsert(
                    ['name' => $repo["name"]],
                    [
                        'name' => $repo["name"],
                        'html_url' => $repo["html_url"],
                        'description' => $repo["description"],
                        'stargazers_count' => $repo["stargazers_count"],
                        'language' => $repo["language"],
                    ]
                );
        }
    }

Kernel.php

protected function schedule(Schedule $schedule)
    {
         $schedule->job(new ProcessGithub)->everyMinute();
    }

But it does not dispatch the job every minute and my database remains empty. I know I don't need to fetch the repos every minute but that is just for testing purposes, If it works every minute I could always change it back to days/hours

How can I accomplish this?

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