polyfony-inc / polyfony

Polyfony is an intuitive, light and powerful PHP micro-framework
GNU General Public License v3.0
10 stars 1 forks source link

onTerminate email sending can produce duplicate sending #263

Open AnnoyingTechnology opened 4 years ago

AnnoyingTechnology commented 4 years ago

Assuming one does

// register async emails sending
Polyfony\Events::register('onTerminate',function(){
    // send all pending emails
    Models\Emails::sendAllPending();
});

the request terminates, and PHP tries to send any pending email. If you have other request shortly after (typically within the time period required to send an email ~500ms), then your email still appears as not send in the database, and the other PHP thread will try to send it too. Sending duplicates email.

Suggested fix is to, upon call of sendAllPending(), tag all available emails with a ['spooled_for_sending_at' => time()'] this will be saved on the database immediately, so that other processes don't try to send those, before the date in spooled_for_sending_at is more than x seconds. At which time, the new process will update said date, and try again.

AnnoyingTechnology commented 4 years ago

possible fix Screenshot from 2019-11-04 17-17-45