Ali Gajani
Ali Gajani

Reputation: 15091

How to delay execution of script while using Laravel's chunk feature?

I am trying to delay the execution of a script by a few seconds after it processes say 200 records using Laravel's chunk method. Here's what I've right now. Should I put a counter somewhere? But then that will not work I believe, are there other techniques?

    Article::chunk(200, function ($articles)
    { 
        foreach ($articles as $article)
        {

        }

    });

Thanks to WereWolf, it worked. Can't believe it was so simple.

[2014-08-27 07:18:23] local.INFO: Article metrics:198 [] []
[2014-08-27 07:18:24] local.INFO: Article metrics:199 [] []
[2014-08-27 07:18:24] local.INFO: Article metrics:200 [] []
[2014-08-27 07:18:30] local.INFO: Article metrics:201 [] []
[2014-08-27 07:18:34] local.INFO: Article metrics:202 [] []

Upvotes: 5

Views: 12947

Answers (1)

The Alpha
The Alpha

Reputation: 146191

You may use sleep function, for example:

Article::chunk(200, function ($articles) {

    foreach ($articles as $article) {

        // ...

    }

    sleep(2); // Delay the script execution for 2 seconds
});

Upvotes: 9

Related Questions