1

I have a Flask webserver that interacts with a POSTGREsql database to fetch and insert data from the website.

I also have an APScheduler Background Scheduler task that checks the database regularly, looks for certain objects with certain attributes like 5 minutes past creation, column x is over 10 etc, then sends that information in an email.

The problem I'm running into right now is that, when run in the local environment, only one task is executed, and it runs every (30) seconds like I want it to. However, if I deploy it on Heroku, two of the same tasks is executed within milliseconds of each other, and it causes problems.

The code responsible for running the background task would be this.

app = Flask(__name__)
#app.debug = True
app.debug = False
app.config.from_pyfile('config.py')
db = models.db
db.init_app(app)
migrate = Migrate(app, db)


manager = Manager(app)
manager.add_command('db', MigrateCommand)
job_defaults = {
    'coalesce': False,
    'max_instances': 1
}
sched = BackgroundScheduler(job_defaults=job_defaults)
sched.start()


@sched.scheduled_job('interval', seconds=30)
def my_job():
    with app.app_context():
        my_class = OutageCheck(db)
        my_class.checkOutage()
lonewaft
  • 812
  • 2
  • 11
  • 28

1 Answers1

0

It can be a reloader which runs 2 instances of flask on remote server. You can try this

app.run(debug=True, use_reloader=False)

Consult this question How to stop Flask from initialising twice in Debug Mode?

Community
  • 1
  • 1
Alexey Smirnov
  • 2,573
  • 14
  • 20