Skip to content

Instantly share code, notes, and snippets.

@frznk-tank
Last active August 1, 2017 10:12
Show Gist options
  • Save frznk-tank/f30d378dd1e3e03557e1e2239608fdd4 to your computer and use it in GitHub Desktop.
Save frznk-tank/f30d378dd1e3e03557e1e2239608fdd4 to your computer and use it in GitHub Desktop.
Heroku Reporting Dynos

Setup

  • Add a new worker to your Procfile. In this gist I used reporter
  • Add the Heroku Platform gem to your Gemfile
  • Create the HerokuReporterDyno module in lib or any other auto-loadable path
  • Create two jobs. A caller (Reporting::GeneratorJob) and a runner (Reporting::RunnerJob)
  • Deploy
  • Once deployed, log into Heroku and change the newly added reporter dyno to a Performance M or Performance M dyno

How this all works

Simply run the caller job Reporting::GeneratorJob.perform_later and it will scale the reporter dyno up to 1 and run the runner job. Once the runner job is complete, it will scale the reporter dynoe back to 0.

Why do this?

Simple... To run very heavy reporting aggregation jobs using high spec dynos at affordable costs

gem 'platform-api'
# app/jobs/reporting/generator_job.rb
class Reporting::GeneratorJob < ActiveJob::Base
queue_as :default
def perform
HerokuReporterDyno.add
Reporting::RunnerJob.perform_later
end
end
# lib/heroku_reporter_dyno.rb
module HerokuReporterDyno
def self.add
heroku = PlatformAPI.connect_oauth(ENV['HEROKU_API_KEY'])
heroku.formation.update(ENV['HEROKU_APP_NAME'], 'reporter', { quantity: 1 })
end
def self.remove
heroku = PlatformAPI.connect_oauth(ENV['HEROKU_API_KEY'])
heroku.formation.update(ENV['HEROKU_APP_NAME'], 'reporter', { quantity: 0 })
end
end
web: ...
worker: bundle exec sidekiq -C config/sidekiq.yml
reporter: bundle exec sidekiq -c 25 -q reports
# app/jobs/reporting/runner_job.rb
class Reporting::RunnerJob < ActiveJob::Base
queue_as :reports
def perform
do_some_heavy_reporting_stuff
HerokuReporterDyno.remove
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment