Re-Seed Your Heroku Database Everyday With One-Off Dynos

Kevin Gleeson
Geek Culture
Published in
4 min readJun 15, 2021

--

In my last blog, I went into how you can seed your database from an external API. This is great for grabbing information from a source other than your own backend and is a pretty essential tool for building a lot of projects. However, what happens once you’ve pushed your new project onto Heroku? What if the information you seeded is constantly updating (stocks, weekly sales, etc)? Do you plan on resetting your database every single day so your site information stays current? I mean, sure you could, but in the grand scheme of things this is going to spell out trouble for you and your project. Enter Heroku dynos and one-off dynos.

No, not those dinos…dynos…

“Dynos are isolated, virtualized Linux containers that are designed to execute code based on a user-specified command.” -Heroku

As the above quote mentions, dynos are containers filled with code that execute on a user-specified command. Meaning that for code bases of any size, you can store code that will simplify workflow and enhance productivity.

A one-off dyno is executable code that we can use to manage all of our administrative and maintenance tasks. Seeding our database falls into this category, but what else does? According to the Heroku website, one-off dynos can be used in the following ways:

  • Initializing databases or running database migrations. (e.g. rake db:migrate or node migrate.js migrate)
  • Running a console or a REPL shell to run arbitrary code or inspect the app’s models against the live database. (e.g. rails console, irb, or node)
  • One-time scripts committed into the app’s repo (e.g. ruby scripts/fix_bad_records.rb or node tally_results.js).

For this guide, we’re going to be re-seeding our database, which falls under the first category. Also, like the previous guide, I’ll be using Ruby on Rails. Before we jump in, first make sure you’ve done the following.

  • Attached your backend to a Heroku site.
  • Installed the gem ‘database-cleaner
  • Install the Heroku Scheduler on your app (Very easy to do! Just follow the docs linked here. Once you have it set up, don’t worry about adding anything yet, we’ll get to it.)

Now that we have all of our pieces together, it’s time to build a one-off dyno! All we need to do to get started is to build a rake task. To do this, create a file called scheduler.rakein /app/db/lib/tasks. Once there, you can take a look at the following code.

Using ‘datbase-cleaner’, building a rake task that cleans our database is easy!

In this example, I created a namespace called :scheduled_tasks that will be the title for all of the tasks that we’ll want to schedule into our scheduler. Doing something like this is nice for scalability and legibility.

After describing what our rake task will do, we’ll create our task called :update_data , which calls our ‘database-cleaner’ gem to clean our database.

Once the cleaning is done, we need to re-seed our database. Luckily because db:seed is already a known rake task, we can just invoke it with the line Rake::Task[“db:seed”].invoke

Before moving on, be sure to push your changes up to your Heroku site.

Now that our rake task is built out, all we need to do is connect it to our scheduler. If you followed the instructions properly when installing, you should see the scheduler on your Heroku homepage for the website you’re working on. Click on your scheduler and find the option to Add a Job. A Job-Editor window should pop up that looks like this.

Here is where you’ll add the rake task that we had built to our application. What we’re essentially doing here is telling Heroku to run the rake task we had built at a certain time every day.

Once this is set up, check your scheduler after the time the rake task was supposed to run. You’ll be able to see when it was last executed.

I was really surprised that Heroku had something like this built into their site functionality and am really happy that my application is up and running with new daily information.

If you wanted to check out that project, a Covid-19 Tracker, you can view it here.

--

--

Kevin Gleeson
Geek Culture

Software Engineering Graduate from Flatiron School. Former expatriate and music teacher.