• Stars
    star
    226
  • Rank 176,514 (Top 4 %)
  • Language
    Ruby
  • Created about 12 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Start/stop Sidekiq workers on Heroku

Sidekiq Heroku Autoscaler

Sidekiq performs background jobs. While its threading model allows it to scale easier than worker-pre-process background systems, people running test or lightly loaded systems on Heroku still want to scale down to zero to avoid racking up charges.

Requirements

Tested on Ruby 2.1.7 and Heroku Cedar stack.

Installation

gem install autoscaler

Getting Started

This gem uses the Heroku Platform-Api gem, which requires an OAuth token from Heroku. It will also need the heroku app name. By default, these are specified through environment variables. You can also pass them to HerokuPlatformScaler explicitly.

AUTOSCALER_HEROKU_ACCESS_TOKEN=.....
AUTOSCALER_HEROKU_APP=....

Install the middleware in your Sidekiq.configure_ blocks

require 'autoscaler/sidekiq'
require 'autoscaler/heroku_platform_scaler'

Sidekiq.configure_client do |config|
  config.client_middleware do |chain|
    chain.add Autoscaler::Sidekiq::Client, 'default' => Autoscaler::HerokuPlatformScaler.new
  end
end

Sidekiq.configure_server do |config|
  config.server_middleware do |chain|
    chain.add(Autoscaler::Sidekiq::Server, Autoscaler::HerokuPlatformScaler.new, 60) # 60 second timeout
  end
end

Limits and Challenges

  • HerokuPlatformScaler includes an attempt at current-worker cache that may be overcomplication, and doesn't work very well on the server
  • Multiple scale-down loops may be started, particularly if there are multiple jobs queued when the servers comes up. Heroku seems to handle multiple scale-down commands well.
  • The scale-down monitor is triggered on job completion (and server middleware is only run around jobs), so if the server never processes any jobs, it won't turn off.
  • The retry and schedule lists are considered - if you schedule a long-running task, the process will not scale-down.
  • If background jobs trigger jobs in other scaled processes, please note you'll need config.client_middleware in your Sidekiq.configure_server block in order to scale-up.
  • Exceptions while calling the Heroku API are caught and printed by default. See HerokuPlatformScaler#exception_handler to override

Experimental

Strategies

You can pass a scaling strategy object instead of the timeout to the server middleware. The object (or lambda) should respond to #call(system, idle_time) and return the desired number of workers. See lib/autoscaler/binary_scaling_strategy.rb for an example.

Initial Workers

Client#set_initial_workers to start workers on main process startup; typically:

Autoscaler::Sidekiq::Client.add_to_chain(chain, 'default' => heroku).set_initial_workers

Working caching

scaler.counter_cache = Autoscaler::CounterCacheRedis.new(Sidekiq.method(:redis))

Tests

The project is setup to run RSpec with Guard. It expects a redis instance on a custom port, which is started by the Guardfile.

The HerokuPlatformScaler is not tested by default because it makes live API requests. Specify AUTOSCALER_HEROKU_APP and AUTOSCALER_HEROKU_ACCESS_TOKEN on the command line, and then watch your app's logs.

AUTOSCALER_HEROKU_APP=... AUTOSCALER_HEROKU_ACCESS_TOKEN=... guard
heroku logs --app ...

Authors

Justin Love, @wondible, https://github.com/JustinLove

Contributors

Licence

Released under the MIT license.

More Repositories

1

onelife-client-patches

Wondible's One Hour One Life Client Patches
7
star
2

schedule-from-videos

Builds a historical Twitch streaming schedule by looking at recent video times and durations.
Elm
7
star
3

guard-ruby

Guard for running simple Ruby files, such as benchmarks.
Ruby
6
star
4

cacert

Packages a snapshort of the Curl cacert.pem
Ruby
4
star
5

obs-mic-check

Checks that mic state matches visible sources ("BRB", "Starting Soon", etc)
Elm
4
star
6

ruby_metaclass_examples

Examples and experiments for Justin Love's presentation on Ruby object, classes, and metaclasses
Ruby
4
star
7

obs-script-dropped-frames

OBS Lua Script to sound an alarm if you start dropping frames
Lua
3
star
8

farmers_market_cal

Take data from the USDA Farmers Market API and present it as an iCalendar feed
Ruby
3
star
9

ruby_closure_examples

Code examples from my ChicagoRuby talk on lamdas, blocks, Procs, and company.
Ruby
3
star
10

family-tree-viewer

Web frontend to search and view family trees from One Hour One Life lifelog data (on a separate server)
JavaScript
2
star
11

bondage

class Binding stinks. module Bondage stinks less.
Ruby
2
star
12

bundler_version_autoload_test_case

Minimal implementation to show a situation where Bundler loads version.rb and prevents an autoloader from loading the main module.
Ruby
2
star
13

ohol-docker

Docker setup to build and run a One Hour One Life Server
Shell
2
star
14

connect_buttons

Mod for Planetary Annihilation which adds buttons to connect to local or private servers.
JavaScript
2
star
15

beecarbonize_graphs

Graphviz files for making online tech and event graphs for Beecarbonize by Charles Games
HTML
2
star
16

stream-credits

Elm application which shows hosts as a credit roll
Elm
2
star
17

requirejs-no-path-error-test

Bug reproduction test case
Ruby
2
star
18

Module

Experimental javascript loader, based on exception-reload
JavaScript
2
star
19

unit_conversion_shootout

Ruby Unit Conversion Shootout. Rubyists love writing unit conversion libraries. This provides a brief summary and superficial performance audit.
Ruby
2
star
20

gross_economy

A status bar mod for Planetary Annihilation that emphasises income and expense so that they can be visually compared.
JavaScript
2
star
21

conwayslife

Conway's Game of Life, from a two hour pair programming exercise with Justin Love and Dave Giunta
Ruby
2
star
22

amp_rugged

Experimental Git support for Amp using libgit2/rugged
Ruby
1
star
23

gsl

Board game domain specific language embedded in Ruby. Very experimental at present.
Ruby
1
star
24

ohol-family-trees

Display One Hour One Life family trees using graphviz
Ruby
1
star
25

ohol-data-server

Ruby/Rails project to import and server One Hour One Life lifelog data
Ruby
1
star
26

two_color_icons

Mod for Planetary Annihilation that uses secondary color for icon outlines
GLSL
1
star
27

obs-script-mic-check

Lua Script for OBS Studio that checks that mic mute state is appropriate for BRB/Starting etc.
Lua
1
star
28

junkyard_wars

Server mod for Planetary Annihilation that make reclaim the primary metal source
JavaScript
1
star
29

amp_repo_spec

Generic repository spec which every repository must supply
Ruby
1
star
30

hosting-clips

Display clips from people hosting you on Twitch
Elm
1
star
31

hostable

Manually maintained list of twitch channels that are good enough to host
Elm
1
star
32

ruby_string_examples

Code Examples for "How to Quote a String"
Ruby
1
star
33

jsharmony-cms-sample-rails

Basic integration to use https://github.com/apHarmony/jsharmony-cms as a CMS editor for a Ruby on Rails application.
Ruby
1
star
34

NakedJavascript

Attempt to apply Naked Object to Javascript; really a jQueryUI object browser.
JavaScript
1
star
35

gw_unlock_loadouts

Mod for Planetary Annihilation which unlocks all GW loadouts
1
star
36

hotkeyfix_groups

Mod for Planetary Annihilation that address an issue rapidly switching between groups.
JavaScript
1
star
37

canvaspjs

Implementation of the Canvas API which outputs Postscript
JavaScript
1
star
38

mongoship

mongodb exercise at ChicagoRuby Hack Night 2010-02-11 with Sam and Joe
Ruby
1
star
39

ES5

Shim for using some of EcmaScript (Javascript) 5 in 3
JavaScript
1
star
40

amp_detect_lab

Prototype laboratory for detecting the kind of vcs given it's url
Ruby
1
star
41

Transform

Lightweight reordering and mapping of row,column data such as CSV
Ruby
1
star
42

hades-boons

Interactive web chart of Hades Boons
Elm
1
star
43

amp_dev_setup

Sample setup for OS X 10.6 with rvm 1.9.2 installed.
Shell
1
star
44

bundler_source_order_test_case

Attempt to demonstrate unexpected behavior in bundler 1.1
Ruby
1
star
45

ai_showdown

Mashup multiple PA AI configs so they can be played at the same time
JavaScript
1
star
46

pa_mod_amdclean

Grunt-init template for a Planetary Annihilation mod using RequireJS with Amdclean, and Grunt build.
JavaScript
1
star
47

Code-Mountain

Ruby
1
star
48

puppetmaster_cinematic

Mod for Planetary Annhilation which removes the laser site from Puppetmaster drop pod effect
JavaScript
1
star
49

keybind_extensions

Mod for Planetary Annhiliation which add more functions for key binding
JavaScript
1
star
50

mathviz

Program to calculate intermediate and derived values of a system of equations and generate a dot (Graphviz) file. Currently implemented for Disk Clock internal calculations. Mostly for my own use, but the plain-code equation system has promise.
Ruby
1
star