Reputation: 830
Apologies if the answer to this is a simple one, but my brain just can't seem to solve this today, I'm hoping someone has had to solve something similar.
So in my database I have various records with timestamps.
In the 10 minutes prior to the timestamp, I'd like to perform an action and store a result in a field.
I'm well aware that this would be easily solved if I just stored these records vertically, as it is I'd like to have the extra fields tenminutes
nineminutes
etc in the db - I know this is probably bad DB design but ssshhh, just go with it!
Ok, so my (pseudo)code at the minute read a bit like this:
// In a command that is executed every minute using the scheduler....
$current = Carbon::now();
foreach ($things as $thing) {
if ($thing->thingStartTime->diffInMinutes($current) <= -10) {
//Get data
//Update table field `tenminutes`
}
if ($thing->thingStartTime->diffInMinutes($current) <= -9) {
//Get data
//Update table field `nineminutes`
}
}
Can you see how horrible this will become?
I was thinking along the lines of an associative array, loop through it and have a kind of 'tenminutes' => 10
thing going on?
Or is there a funkier way of using carbon
I dont know about? Any ideas?
Other info, this is inside a cron job executed every minute! So if thres a way I can use Laravels scheduler to be smart about this, that would be good to know!
Upvotes: 0
Views: 1364
Reputation: 6392
You could iterate through the items and dispatch jobs with different delays?
$current = Carbon::now();
foreach ($things as $thing) {
$delayTime = $current - $thing->thingStartTime;
$job = (new SendReminderEmail($user))->delay($delayTime);
$this->dispatch($job);
}
There is more documentation here.
Upvotes: 1