Search code examples
phplaravelqueuejobslaravel-5.4

Detecting when job is completed and re-run it


I have put my web scraping function into a job and I want to run it continuously. I want to detect when the job is completed, and re-run the job. I created a command for triggering it once.

php artisan update:catalog works. In my command, inside handle, I placed dispatch the job

public function handle()
{
    dispatch(new \App\Jobs\UpdateCatalog());
}

And my job looks like this:

public function handle()
{
    doTheScraping();
}

Now, if I watch through php artisan queue:listen database and run the update:catalog command, it works for once.

How can I detect when the job is completed and re-run the job in completion?


Solution

  • You can dispatch the job recursively:

    public function handle()
    {
        doTheScraping();
        if (!$exit_condition) {
            dispatch(new self)
        }
    }