4

I have a flask web app deployed in heroku. I need to schedule a background task to be scheduled at a specific time. I have tried using the apscheduler module. While it allows to define periodic tasks easily adding them from your application at runtime is what I am looking for.

I tried sharing the same jobstores in apscheduler

import time

from apscheduler.scheduler import Scheduler
from apscheduler.jobstores.shelve_store import ShelveJobStore

sched = Scheduler()
sched.add_jobstore(ShelveJobStore('jobstore.db'), 'shelve')

sched.start()

And from terminal I tried this,

Python 2.7.5 (default, May 12 2013, 12:00:47) 
[GCC 4.8.0 20130502 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from apscheduler.scheduler import Scheduler
>>> sc = Scheduler()
>>> sc.add_jobstore('jobstore.db', 'shelve')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/dhananjay/git/blast/venv/lib/python2.7/site-packages/apscheduler/scheduler.py", line 168, in add_jobstore
    jobstore.load_jobs()
AttributeError: 'str' object has no attribute 'load_jobs'

I have come across this question, while looking for a celery based approach. It talks about the same problem from a django perspective but I can't get it to work with my app (I am completely oblivious to django)

Community
  • 1
  • 1
  • The issue here is that in the first case you call `sched.add_jobstore` with an instance of `ShelveJobStore` as its first argument and in the second you call it with a string as the first argument. Change your second example to use `ShelveJobStore` as well and things should just work. – Sean Vieira Jul 01 '14 at 13:47
  • A free Heroku instance will not allow background threads, not really mentioned here .. so no clue if thats the case. Background threads will count towards cpu usage on a payed account, and can be expensive.. so evaluate if Heroku is the right place for a scheduled job, or if you can eliminate this by doing some clever engineering. Even then, you would as the answer suggests, use a background dyno – Alex R Jun 06 '19 at 13:32

1 Answers1

-1

When you tried running it from the terminal, you gave add_jobstore a string as the first parameter, instead of a job store. It expects a job store as the first parameter, see the documentation for more info.

As for scheduling background tasks in Heroku, I would recommend reading the Worker Dynos, Background Jobs and Queueing article on the matter.

Christopher Su
  • 373
  • 3
  • 10