Reputation: 341
I am using Laravel's artisan scheduler to manage cron jobs for an application. One of the features of this app are virtual shops that "restock" at an interval that's logged in the database (i.e stock_min = 5
and stock_max = 15
would mean the shop would restock every 5-15 minutes). These values can change at any time.
How would I go about setting up the cron in Laravel so it would adhere to the interval given with a sufficient amount of randomness and look to see if the interval has changed since the last restock?
Upvotes: 0
Views: 211
Reputation: 2944
You'd need to create a model called 'stock_update_tracking' or something similar, to log when the next time the stock should be restocked.
stock_update_tracking
next_update
Then, you can call a cron job every minute and simply do a check to see if the next_update value is more than the current timestamp.
$schedule->call(function() {
$nextUpdate = DB::table('stock_update_tracking')
->select('next_update')
->where('next_update', '<' Carbon::now())
->count();
if($nextUpdate) {
// Update the stock
// Calculate a new next update time
$newNextUpdate = Carbon::now();
$newNextUpdate = $newNextUpdate->addMinutes(rand(5,15));
// Save the newNextUpdate value in the stock_update_tracking table
DB::table('stock_update_tracking')->update(['next_update' => $newNextUpdate])
}
})->everyMinute()->withoutOverlapping()
Upvotes: 1