Module: Resque::Plugins::HerokuScaler

Defined in:
lib/resque/plugins/heroku_scaler.rb,
lib/resque/plugins/heroku_scaler/config.rb,
lib/resque/plugins/heroku_scaler/manager.rb,
lib/resque/plugins/heroku_scaler/version.rb,
lib/resque/plugins/heroku_scaler/manager/local.rb,
lib/resque/plugins/heroku_scaler/manager/heroku.rb

Defined Under Namespace

Modules: Config, Manager

Constant Summary collapse

Version =
VERSION = "0.4.1"

Class Method Summary collapse

Class Method Details

.configure {|Resque::Plugins::HerokuScaler::Config| ... } ⇒ Object



113
114
115
# File 'lib/resque/plugins/heroku_scaler.rb', line 113

def configure
  yield Resque::Plugins::HerokuScaler::Config
end

.lockObject



97
98
99
# File 'lib/resque/plugins/heroku_scaler.rb', line 97

def lock
  Resque.lock
end

.lockedObject



105
106
107
# File 'lib/resque/plugins/heroku_scaler.rb', line 105

def locked
  Resque.info[:locked]
end

.log(message) ⇒ Object



127
128
129
# File 'lib/resque/plugins/heroku_scaler.rb', line 127

def log(message)
  puts "*** #{message}"
end

.offline?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/resque/plugins/heroku_scaler.rb', line 85

def offline?
  workers.zero?
end

.pendingObject



93
94
95
# File 'lib/resque/plugins/heroku_scaler.rb', line 93

def pending
  Resque.info[:pending]
end

.pending?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/resque/plugins/heroku_scaler.rb', line 89

def pending?
  pending > 0
end

.pruneObject



109
110
111
# File 'lib/resque/plugins/heroku_scaler.rb', line 109

def prune
  Resque.prune
end

.runObject



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/resque/plugins/heroku_scaler.rb', line 14

def run
  startup
  loop do
    begin
      scale
    rescue Exception => e
      log "Scale failed with #{e.class.name} #{e.message}"
    end
    wait_for_scale
  end
end

.scaleObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/resque/plugins/heroku_scaler.rb', line 26

def scale
  required = scale_for(pending)
  active = workers

  return if required == active

  if required > active
    log "Scale workers from #{active} to #{required}"
    scale_workers(required)
    return
  end
              
  return if pending?

  scale_down(active)
end

.scale_down(active) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/resque/plugins/heroku_scaler.rb', line 55

def scale_down(active)
  log "Scale #{active} workers down"

  lock

  timeout = Time.now + Resque::Plugins::HerokuScaler::Config.scale_timeout
  until locked == active or Time.now >= timeout
    sleep Resque::Plugins::HerokuScaler::Config.poll_interval
  end

  scale_workers(0)

  timeout = Time.now + Resque::Plugins::HerokuScaler::Config.scale_timeout
  until Time.now >= timeout
    if offline?
      log "#{active} workers scaled down successfully"
      prune
      break
    end
    sleep Resque::Plugins::HerokuScaler::Config.poll_interval
  end

ensure
  unlock
end

.scale_for(pending) ⇒ Object



47
48
49
# File 'lib/resque/plugins/heroku_scaler.rb', line 47

def scale_for(pending)
  Resque::Plugins::HerokuScaler::Config.scale_for(pending)
end

.scale_workers(qty) ⇒ Object



51
52
53
# File 'lib/resque/plugins/heroku_scaler.rb', line 51

def scale_workers(qty)
  Resque::Plugins::HerokuScaler::Manager.workers = qty
end

.startupObject



117
118
119
120
121
122
123
124
125
# File 'lib/resque/plugins/heroku_scaler.rb', line 117

def startup
  STDOUT.sync = true
  trap('TERM') do
    log "Shutting down scaler"
    exit
  end
  log "Starting scaler"
  unlock
end

.unlockObject



101
102
103
# File 'lib/resque/plugins/heroku_scaler.rb', line 101

def unlock
  Resque.unlock
end

.wait_for_scaleObject



43
44
45
# File 'lib/resque/plugins/heroku_scaler.rb', line 43

def wait_for_scale
  sleep Resque::Plugins::HerokuScaler::Config.scale_interval
end

.workersObject



81
82
83
# File 'lib/resque/plugins/heroku_scaler.rb', line 81

def workers
  Resque::Plugins::HerokuScaler::Manager.workers
end