How do you schedule cron jobs using APScheduler on Heroku?
<p>I am trying to use the APScheduler and SendGrid on Heroku to create a cron job for sending an email.</p>

<p>Even though the add_job method call seems to be executing  correctly, I am getting the following error.</p>

<p>Below are the logs from heroku</p>

<pre><code>2016-09-11T22:33:37.776867+00:00 heroku[clock.1]: State changed from crashed to starting
2016-09-11T22:33:40.672563+00:00 heroku[clock.1]: Starting process with command `python clock.py`
2016-09-11T22:33:41.353373+00:00 heroku[clock.1]: State changed from starting to up
2016-09-11T22:33:43.527949+00:00 app[clock.1]: created background scheduler
2016-09-11T22:33:43.528848+00:00 app[clock.1]: started background scheduler
2016-09-11T22:33:43.572751+00:00 app[clock.1]: added cron job
2016-09-11T22:33:43.585801+00:00 app[clock.1]: Exception in thread APScheduler (most likely raised during interpreter shutdown):
2016-09-11T22:33:43.585807+00:00 app[clock.1]: Traceback (most recent call last):
2016-09-11T22:33:43.585808+00:00 app[clock.1]:   File "/app/.heroku/python/lib/python2.7/threading.py", line 801, in __bootstrap_inner
2016-09-11T22:33:43.585810+00:00 app[clock.1]:   File "/app/.heroku/python/lib/python2.7/threading.py", line 754, in run
2016-09-11T22:33:43.585827+00:00 app[clock.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/apscheduler/schedulers/blocking.py", line 29, in _main_loop
2016-09-11T22:33:43.585829+00:00 app[clock.1]:   File "/app/.heroku/python/lib/python2.7/threading.py", line 614, in wait
2016-09-11T22:33:43.585848+00:00 app[clock.1]:   File "/app/.heroku/python/lib/python2.7/threading.py", line 364, in wait
2016-09-11T22:33:43.585851+00:00 app[clock.1]: &lt;type 'exceptions.ValueError'&gt;: list.remove(x): x not in list
2016-09-11T22:33:43.695569+00:00 heroku[clock.1]: Process exited with status 0
2016-09-11T22:33:43.719265+00:00 heroku[clock.1]: State changed from up to crashed
</code></pre>

<p>I am running one clock process, which is in the clock.py file below.</p>

<pre><code>from apscheduler.schedulers.background import BackgroundScheduler
import sendgrid
import os
from sendgrid.helpers.mail import *

def send_email():
  try:
    sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY'))
    print("created send grid api client")
    from_email = Email("ohta.g@husky.neu.edu")
    print("created from email")
    subject = "Weekly update"
    to_email = Email("ohta.g@husky.neu.edu")
    print("created to email")
    content = Content("text/plain", "Hello, Email!")
    print("created content")
    mail = Mail(from_email, subject, to_email, content)
    print("created mail")
    response = sg.client.mail.send.post(request_body=mail.get())
  except Exception as e:
    return e

try:
  sched = BackgroundScheduler()
  print("created background scheduler")
  sched.start()
  print("started background scheduler")
  sched.add_job(send_email, 'cron', day_of_week=6, hour=22, minute=20)
  print("added cron job")
except Exception as e:
  print e.message
</code></pre>

<p>Here is my Procfile.</p>

<pre><code>clock: python clock.py
</code></pre>

<p>Here is my requirements.txt file.</p>

<pre><code>APScheduler==3.1.0
sendgrid==3.4.0
</code></pre>

<p>Could someone please tell me what I'm doing wrong?</p>

<p>You started a background scheduler but then allowed your main thread to exit, which also exits the clock process. This is the entire reason why <code>BlockingScheduler</code> exists. Have you not read Heroku's <a href="https://devcenter.heroku.com/articles/clock-processes-python" rel="nofollow">APScheduler instructions</a>?</p>

python heroku cron sendgrid apscheduler