My users can post ads and later promote them with tokens which they buy. I have columns like pro_until, vip_until etc. for every type of promotion.
They are timestamps and are saved to the seconds.
When a user promotes his ad to be vip for 3 days I save now()->addDays(3)
to the vip_until column.
So what is the problem: My user needs to have a dashboard with notifications some of which will be notifications about the ad promotion expiration. Something like "22/03/2020 15:30:20 - Your VIP for ad 111 expired"
Here are the things I tried/thought about and how they worked out:
Cron jobs:
I can run a cron job every minute and check for expired columns $ad->vip_until->lt(now())
.
Problem is: how do I notify about this expired ad only once ?
I can't check for an exact match between now()
and {type}_until
because the cron is beign executed every minute and I keep the {type}_until
column to the exact seconds, so it is totally possible not to have a total match.
Delaying notifications:
I tried queueing the expiration notifications at the moment of creating the promotion https://laravel.com/docs/8.x/notifications#queueing-notifications.
I added the ->delay()
callback
Implemented the ShouldQueue interface
Used the Queueable trait
Added a queue worker
but the notifications were pushed immediately.
$delay = null;
if ($request->type == 'pro') {
$delay = $ad->promotePro($request->days);
}
if ($request->type == 'top') {
$delay = $ad->promoteTop($request->days);
}
if ($request->type == 'vip') {
$delay = $ad->promoteVip($request->days);
}
Auth::user()->payed -= $price;
Auth::user()->save();
if ($delay) {
Auth::user()->notify((new AdPromotionEndedNotification($ad, $request->type))->delay($delay));
}