Search code examples
python-3.xherokuscheduled-tasksnodejs-server

NodeJS | Python | Heroku - Scheduling Emails 48 Hours after form Post


I've made a PWA with ReactJS + NodeJS, running with a python backend that's being spawned as a child process by the Node server, I have to send and generate a document generated by python via email 48 hours after payment verification is called, how do I accomplish this? My current method includes :

def sendInTwoDays(recipient, filename):
    time = round(random.uniform(0.8, 2.0), 2)
    time = round(time * 24 * 3600, 2)
    time /= 10000 #for testing
    print("[python:sendInTwoDays()] > Sleep ({} seconds) : {} days".format(time, time/3600))
    sleep(time)
    sendNow(recipient, filename)

I hate it since it uses "sleep()" and sleep on a webserver just doesn't sit right with me.

Please suggest a better way to accomplish the same on heroku.


Solution

  • I would suggest a scheduling(cron) approach with Heroku Scheduler like to check due emails every fifteen minutes. Rather than letting the process sit for 48 hours, I would save the email data along with the emailing time in the database and let the scheduler can invoke an API/task to call the Python script to output and email documents which are due after 48 hours. The emails will not be sent out after exact 48 hours, but should be close.