Forum Moderators: phranque

Message Too Old, No Replies

Scheduling a random task far in the future

         

NickMNS

3:57 am on Feb 15, 2022 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I need to send users an email at some point far in the future, in the order of 30 days, the exact time must be random.

My plan is:
When a user submits a specific form, I immediately generate a random number of days and calculate the future date when the email should be sent. I then save that date/time to a specific collection to the db (table for you SQL people), with the email address and other relevant data.

I then create a background task that runs a every few hours (4 times per day). That task checks the db collection for any entries that have a date/time that passed the current date/time. If any entries are found, those emails are sent and the documents (records) are deleted from the db.

Like this if anything goes wrong and the server shuts down at some point during the 30 days, when it comes back online it just resumes checking the db and sends whatever is in the "queue". To maintain the illusion of randomness I can randomize the intervals at which the background task runs.

Does anyone see a problem with the approach or maybe have a simpler solution?

LifeinAsia

1:06 am on Feb 16, 2022 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



If any entries are found, those emails are sent and the documents (records) are deleted from the db.
My personal preference: instead of deleting the record, mark the record as sent (or better- include the date/time sent). Otherwise, if you don't see any records in the table, you don't know if everything is caught up or if there's a problem and nothing got added in the first place.

But that's just me- I'm a data packrat.

tangor

5:29 am on Feb 16, 2022 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Either works ... just asking if the user is an opt in for the emails in the first place. These privacy things these days might meet users who forget they are in the pipeline in the first place.

Just an odd thought.

NickMNS

2:49 pm on Feb 16, 2022 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



mark the record as sent (or better- include the date/time sent)

Normally I would to take this approach too, but in this case traceability occurs elsewhere and this process exists only for coordination of the sending. The process can occur many times per user and thus the data has the potential to grow significantly over time.