I want to notify each user that has not been notified for one week.
The code looks like this:
// Select all users where attribute `notified` is older then one week
$users = \App\User::lastNotified(7)->get();
foreach($users as $user){
$user->notifed = date('Y-m-d');
\Mail::to($user)send(new NotificationMail($user));
$user->save();
}
I want to make sure that if the code is executed parallel that each user will receive only one notification (no duplicates).
Have I understand it correctly that I have to use for this Pessimistic Locking like this:
DB::transaction(function () {
$users = \App\User::lastNotified(7)->lockForUpdate()->get();
foreach($users as $user){
$user->notifed = date('Y-m-d');
\Mail::to($user)send(new NotificationMail($user));
$user->save();
}
});
So if the the script is executed parallel, the slower script B would wait on line $users = \App\User::lastNotified(7)->lockForUpdate()->get(); until the quicker script A is done?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire