forked from hirefire/hirefire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
heroku.rb
56 lines (47 loc) · 1.73 KB
/
heroku.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# encoding: utf-8
require 'heroku'
# added the patch from https://gist.github.com/1082673 to get
# hirefire running on cedar
module HireFire
module Environment
class Heroku < Base
private
##
# Either retrieves the amount of currently running workers,
# or set the amount of workers to a specific amount by providing a value
#
# @overload workers(amount = nil)
# @param [Fixnum] amount will tell heroku to run N workers
# @return [nil]
# @overload workers(amount = nil)
# @param [nil] amount
# @return [Fixnum] will request the amount of currently running workers from Heroku
def workers(amount = nil)
#
# Returns the amount of Delayed Job
# workers that are currently running on Heroku
if amount.nil?
# return client.info(ENV['APP_NAME'])[:workers].to_i
return client.ps(ENV['APP_NAME']).select {|p| p['process'] =~ /worker.[0-9]+/}.length
end
##
# Sets the amount of Delayed Job
# workers that need to be running on Heroku
#client.set_workers(ENV['APP_NAME'], amount)
return client.ps_scale(ENV['APP_NAME'], {"type" => "worker", "qty" => amount})
rescue RestClient::Exception
# Heroku library uses rest-client, currently, and it is quite
# possible to receive RestClient exceptions through the client.
HireFire::Logger.message("Worker query request failed with #{ $!.class.name } #{ $!.message }")
nil
end
##
# @return [Heroku::Client] instance of the heroku client
def client
@client ||= ::Heroku::Client.new(
ENV['HIREFIRE_EMAIL'], ENV['HIREFIRE_PASSWORD']
)
end
end
end
end