Zach Handley
Zach Handley

Reputation: 922

Laravel - Scheduling a Large Task

Trying to run a function in Laravel that's quite large and fetches a lot of data from Google Places API and stores parts in my database as new entries in a table. The problem is it auto-discovers new entries for me near my current entries, and that creates more jobs.

When I just access the command via GET it times out eventually. I've tried running it as a scheduled command with Redis but to be frank I can't seem to figure out how it works. I've created a job, I tried to queue it with dispatch, but then it tries to run it immediately right now and it times out eventually again.

How do I run this large task without it pausing my entire server?

Thanks

Upvotes: 1

Views: 1564

Answers (1)

Adam Kozlowski
Adam Kozlowski

Reputation: 5896

I is really simple and you do not need to stop server. It is just CRON job. Use Laravel Schedule.

When using the scheduler, you only need to add the following Cron entry to your server.

* * * * * php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1

Class example:

namespace App\Console;

use DB;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        //
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        $schedule->call(function () {
            DB::table('recent_users')->delete();
        })->daily();
    }
}

More: https://laravel.com/docs/5.6/scheduling

Nice video about laravel scheduling: https://www.youtube.com/watch?v=mp-XZm7INl8

Upvotes: 1

Related Questions