0

I have a simple Python program that repeats the following process at 30 second intervals: uses the Gmail API to check my inbox, looks for a certain kind of mail, and if it is found, uses the Twilio API to call me.

I've been running this program on my machine, but I'd like for it to be running 24x7. A friend said I should be able to do this using Heroku. I'm unable to figure out how to do this and would appreciate any help. I set up Heroku, and first tried deploying my script (which I've reproduced below) and got errors reproduced at the end of post.

I also tried running it within a Flask app function, but the code only ran once when I opened the Heroku link, and I wasn't able to get it to continue running in the background.

Please let me know if I should be doing this differently/using a different hosting/platform provider.

#Procfile
web: python gmail_twilio.py 

#Logs error
2016-07-13T03:35:39.635528+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=pacific-woodland-28065.herokuapp.com request_id=54912af8-83a6-4feb-bb36-def791621e5f fwd="171.50.142.162" dyno= connect= service= status=503 bytes=

#gmail_twilio.py

while True:
  inbox_list = ListMessagesMatchingQuery(service, '##', query="in:inbox -label:called ")# create list with non called labels
  if len(inbox_list)>0:
    twiliocall() # function to call me
    for element in inbox_list:
      ModifyMessage(service, 'me', element[u'id'], {'removeLabelIds': [], 'addLabelIds': ['Label_3']}) # add called label
  time.sleep(30) # wait for 30 seconds
Sanat K.
  • 3
  • 2

1 Answers1

0

Note that the Heroku platform is designed for hosting web applications, not arbitrary daemons. When you look at the output of heroku logs, I imagine you'll see errors like "R10 Web process failed to bind". That's because Heroku expects your web process to listen for web requests by binding to the port passed in via $PORT; if that doesn't happen, Heroku assumes something went wrong and declares the app crashed.

You probably could make it work - for example, create a web process that just echoes status to satisfy the "bind" requirement, then use the the "Scheduler" add-on to run your script every few minutes or define your script as a "worker" process. Alternatively, you could look at other solutions, like AWS Lambda.

bimsapi
  • 4,985
  • 2
  • 19
  • 27