Running a Dash App 24/7 on Heroku with a Scheduled Worker
By: Kelvin Kramp
Tags: Programming, Heroku, Python, Dash, Software Development

Options to make clock working

There are three options for getting a clock or scheduler running in the background on Heroku while your Dash app is running:

1. Frontend-hacky but fairly customizable way

Create a connection of the frontend dash app with the background clock through a function import and call the job in de dash Python file within a thread (see main.py).

A webpage in Heroku will idle after 30 minutes if there are no visitors, so the clock will stop after 30 min if there are no visitors if you connect your clock to your frontend in this way. There is an elaborate post about this issue on StackOverflow this.

You can change this by paying or you can use another third party that pings your website to keep the website from idling. Kaffeine is the most workable solution described within the above StackOverflow post in my opinion. Kaffeine is a web app that uses Heroku itself for pinging the Heroku URL you give it as input, thereby preventing your app from going into the idle stage.

2. Official and friendly but imprecise Heroku way.

Create a Scheduler job in Heroku. Click on Resources within your app and in the text field of add-ons fill in “scheduler”. Start a Scheduler. You have to give your credit card number, but you won’t be charged if you don’t go over the limit. Next, run your job according to a schedule by filling in a command like:

python clock.py

You can check whether you have passed the non-pay limit with the command “heroku ps”. The Scheduler works well but is not very accurate due to its dependency on the availability of background processes within the Heroku system. It was hard to set my clock schedule according to a specific time interval with this option.

3. The professional fully customizable Procfile way

This third option is probably the most professional one: you use the processes file, named Procfile, to set up your clock. What is a Procfile?

“A Procfile is a text file named Procfile placed in the root of your application that lists the process types in an application. Each process type is a declaration of a command that is executed when a container of that process type is started.”

The Procfile contains three lines of code that refer to three files:

web: gunicorn dash_app_name:server

This is the reference to Dash main app file. Gunicorn is the Web Server Gateway Interface (WSGI) HTTP server, I think this is a complex piece of code that makes it possible for a request of your URL to get referred to your working Dash app. worker: python do_something.py

Reference to the file with the function that does something and is supposed to do the scheduled work. clock: python clock.py

This is the reference to the clock file that is supposed to run on the Heroku server continually.

If you use this option you have to start your clock and worker manually after the first time when you’ve pushed it with git with the commands:

heroku ps:scale worker=1

heroku ps:scale clock=1

The first line, the web process, in the Procfile runs automatically after pushing your project to the Heroku server with git.

The downside of option 3

Unfortunately, there are two downsides. First, you can only implement a limited amount of processes as a non-paying user. If you run your clock in this way you have no other options for other processes such as starting a Heroku command-line interface from your Linux prompt, which takes an additional process within a container.

The second problem is that it’s impossible to let the Dash app in the web container create a JSON file that is used to interactively adjust the clock that coordinates the clock container. According to the Heroku website:

“During the dyno’s lifetime its running processes can use the filesystem as a temporary scratchpad, but no files that are written are visible to processes in any other dyno and any files written will be discarded the moment the dyno is stopped or restarted.”

If these two issues don’t pose a problem to the functioning of your project then running a clock process is the best option. If not, I would suggest going for option 1.

Things to keep in the back of your mind

If you see your app running twice at every scheduled run this could be caused by two things:

1. You need to set your web_concurrency to 1:

heroku config:set WEB_CONCURRENCY=1

2. You need to set your debugging mode at the end of your Dash app file to False.

One last thing

If you only want to schedule something without a GUI to control the scheduling you can easily do that by creating a worker process that runs a Python file with apscheduler or another schedule module within it.

Although there will not be a way to interact with the worker process, the worker process is quite reliable in my experience and runs 24/7. Here, you can find an example of Python code that gets financial data and stores it every 15 minutes on the google cloud via firebase.

Hope this saves you time. Good luck! :-)

More content at plainenglish.io