Class: Takeoff::Stage::Heroku::ScaleDownWorkers

Inherits:
Base
  • Object
show all
Defined in:
lib/takeoff/stage/heroku/scale_down_workers.rb

Instance Method Summary collapse

Methods inherited from Base

#initialize

Methods included from Helpers

#branches_up_to_date?, #diff, #execute, #file_has_changed_locally?, #files_have_changed?, #latest_commit, #log

Constructor Details

This class inherits a constructor from Takeoff::Stage::Base

Instance Method Details

#call(env) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/takeoff/stage/heroku/scale_down_workers.rb', line 7

def call(env)
  unless env[:dangerous]
    log "Skipping scaling down workers because nothing dangerous is going on"
    
    return @app.call(env) 
  end
  
  number_of_workers = execute(
    "heroku ps --remote #{env[:server_remote]} | grep '^worker.' | wc -l | tr -d ' '"
  ).to_i

  return @app.call(env) if number_of_workers == 0

  log     "Scaling down workers"
  execute "heroku scale worker=0 --remote #{env[:server_remote]}"

  begin
    @app.call(env)
  ensure
    log     "Scaling up workers"
    execute "heroku scale worker=#{number_of_workers || 0} --remote #{env[:server_remote]}"
  end
end