BackgroundScheduler doesn't work when app deployed on cloud

Hey guys, I have created my first dash app and am currently facing the following problem: I want the app to run 24/7 so I decided to deploy it on onrender . Everything works fine except one thing: I want to update the data every few hours. Therefore, I am using a BackgroundScheduler which calls a function that queries the new data and stores it locally in a csv file. When I run my app locally the scheduler works as expected. However, as soon as I deploy it on onrender the scheduler doesn‘t seem to work, because the scheduled functions never gets called, hence the data never gets updated. I am not sure if this issue is caused by the cloud provider or if it is due to something else. Does anybody have an idea what might cause this problem?

Here a minimal reproducible version of my code:

from functions import update_price

app = Dash(name, use_pages=True)
server = app.server

---------------------------------------- Layout ----------------------------------------

app.layout = dbc.Container(
[
dbc.Row([
dbc.Col([
dash.page_container
])
]),
], fluid=True
)

if name == ‘main’:
scheduler = BackgroundScheduler()
scheduler.add_job(update_price, ‘interval’, minutes=5)
scheduler.start()

app.run_server(debug=False)

I know the leaflet documents which are on that provider have to be reloaded after some period of having no connections so i think it just times out the instance as a whole.

1 Like

Thank you for your answer. I tested it with a shorter time interval (1 min) and it still didn’t execute. Just so you know: I don’t run it on a free instance, which like you said, will shut down after some minutes of inactivity, since I have upgraded to a standard instance (25$/month).

Hello @George9D,

When running on a server, your service won’t get started because the run using gunicorn. This background scheduler is only run when you run the whole app and not just use the server.


if name == ‘main’:
scheduler = BackgroundScheduler()
scheduler.add_job(update_price, ‘interval’, minutes=5)
scheduler.start()


app.run_server(debug=False)

With that being said, you probably shouldn’t do something like this. Id recommend something like crontab or supervisor set to loop every 5 minutes.

  • this would be independent of the server, meaning it wouldn’t get restarted each time you start the server
  • with it attached right now, each worker work run it at a different interval and possibly cause conflicts, if updating the same file

Thanks for your answert. I solved the issue by setting up a dedicated database which stores the data and a cron job which updates it.

1 Like