Module: Supervisor

Defined in:
lib/supervisor.rb,
lib/supervisor/job.rb,
lib/supervisor/server.rb,
lib/supervisor/worker.rb,
lib/supervisor/version.rb,
lib/supervisor/application/app.rb

Defined Under Namespace

Classes: App, Job, Server, Worker

Constant Summary collapse

VERSION =
"0.0.96"

Class Method Summary collapse

Class Method Details

.[](key) ⇒ Object



8
9
10
11
12
13
# File 'lib/supervisor.rb', line 8

def self.[](key)
  unless @config
    @config = YAML.load_file("config.yml").symbolize_keys
  end
  @config[key.to_sym]
end

.[]=(key, value) ⇒ Object



15
16
17
# File 'lib/supervisor.rb', line 15

def self.[]=(key, value)
  @config[key.to_sym] = value
end

.app_mode?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/supervisor.rb', line 19

def self.app_mode?
  return !ENV["RAILS_ENV"].present?
end

.connected?Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
29
# File 'lib/supervisor.rb', line 23

def self.connected?
  if defined? Supervisor.connection
    return true
  else
    return false
  end
end

.delayed_job_running_locally?Boolean

Returns:

  • (Boolean)


52
53
54
55
56
57
58
59
# File 'lib/supervisor.rb', line 52

def self.delayed_job_running_locally?
  local_delayed_jobs = %x{ps aux}.split(/\n/).map{|x| x.split(/\s+/) if x.split(/\s+/).last.match("job")}.compact
  if local_delayed_jobs.empty?
    return false
  else
    return true
  end
end

.establish_connectionObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/supervisor.rb', line 31

def self.establish_connection
  if Supervisor[:database]["host"].nil?
    raise "No job database is configured. Please add one in the config.yml file."
  else
    if defined? connection
      p "There is already a DB connection locally...closing"
      Supervisor.connection.disconnect!
      Supervisor.connection.connection.close
      ActiveRecord::Base.clear_active_connections!
      ActiveRecord::Base.clear_reloadable_connections!
    end
    begin
      db = Supervisor[:database]
      connection =  ActiveRecord::Base.establish_connection(:adapter => db["adapter"],:database => db["database"],:host=>db["host"],:port=>db["port"],:username=>db["username"],:password=>db["password"],:encoding => "utf8",:template=>"template0")
      return connection
    rescue
      raise "Could not connect to the job database"
    end
  end
end

.initialize_serversObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/supervisor.rb', line 61

def self.initialize_servers
  if Supervisor[:hosts].first["host"].nil? && !delayed_job_running_locally?
    raise "No job worker machines (host) configured and no workers running locally. Please add host in config.yml"
  elsif Supervisor[:hosts].first["host"].nil? && delayed_job_running_locally?
    Supervisor::Server.new
  else
    Supervisor[:hosts].each do |host|
      Supervisor::Server.new(host["name"],host["host"],host["rails_path"],host["username"],host["password"])
    end
    if delayed_job_running_locally?
      Supervisor::Server.new #init a default local server
    end
  end
  return Supervisor::Server.servers
end

.start!Object



77
78
79
80
81
82
83
84
85
# File 'lib/supervisor.rb', line 77

def self.start!
  begin
    Supervisor.establish_connection
    Supervisor.initialize_servers
    Supervisor::App.run!
  rescue
    raise "Could not start Supervisor App: #{$!}"
  end
end