Class: Procodile::ControlSession

Inherits:
Object
  • Object
show all
Defined in:
lib/procodile/control_session.rb

Instance Method Summary collapse

Constructor Details

#initialize(supervisor, client) ⇒ ControlSession

Returns a new instance of ControlSession.



7
8
9
10
# File 'lib/procodile/control_session.rb', line 7

def initialize(supervisor, client)
  @supervisor = supervisor
  @client = client
end

Instance Method Details

#check_concurrency(options) ⇒ Object



54
55
56
57
58
# File 'lib/procodile/control_session.rb', line 54

def check_concurrency(options)
  result = @supervisor.check_concurrency(:reload => options['reload'])
  result = result.each_with_object({}) { |(type, instances), hash| hash[type] = instances.map(&:to_hash) }
  "200 #{result.to_json}"
end

#receive_data(data) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/procodile/control_session.rb', line 12

def receive_data(data)
  command, options = data.split(/\s+/, 2)
  options = JSON.parse(options)
  if self.class.instance_methods(false).include?(command.to_sym) && command != 'receive_data'
    begin
      public_send(command, options)
    rescue Procodile::Error => e
      Procodile.log nil, 'control', "Error: #{e.message}".color(31)
      "500 #{e.message}"
    end
  else
    "404 Invaid command"
  end
end

#reload_config(options) ⇒ Object



49
50
51
52
# File 'lib/procodile/control_session.rb', line 49

def reload_config(options)
  @supervisor.reload_config
  "200"
end

#restart(options) ⇒ Object



44
45
46
47
# File 'lib/procodile/control_session.rb', line 44

def restart(options)
  instances = @supervisor.restart(:processes => options['processes'], :tag => options['tag'])
  "200 " + instances.map { |a| a.map { |i| i ? i.to_hash : nil } }.to_json
end

#start_processes(options) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/procodile/control_session.rb', line 27

def start_processes(options)
  if options['port_allocations']
    if @supervisor.run_options[:port_allocations]
      @supervisor.run_options[:port_allocations].merge!(options['port_allocations'])
    else
      @supervisor.run_options[:port_allocations] = options['port_allocations']
    end
  end
  instances = @supervisor.start_processes(options['processes'], :tag => options['tag'])
  "200 " + instances.map(&:to_hash).to_json
end

#status(options) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/procodile/control_session.rb', line 60

def status(options)
  instances = {}
  @supervisor.processes.each do |process, process_instances|
    instances[process.name] = []
    for instance in process_instances
      instances[process.name] << instance.to_hash
    end
  end

  processes = @supervisor.processes.keys.map(&:to_hash)
  result = {
    :version => Procodile::VERSION,
    :messages => @supervisor.messages,
    :root => @supervisor.config.root,
    :app_name => @supervisor.config.app_name,
    :supervisor => @supervisor.to_hash,
    :instances => instances,
    :processes => processes,
    :environment_variables => @supervisor.config.environment_variables,
    :procfile_path => @supervisor.config.procfile_path,
    :options_path => @supervisor.config.options_path,
    :local_options_path => @supervisor.config.local_options_path,
    :sock_path => @supervisor.config.sock_path,
    :log_root => @supervisor.config.log_root,
    :supervisor_pid_path => @supervisor.config.supervisor_pid_path,
    :pid_root => @supervisor.config.pid_root,
    :loaded_at => @supervisor.config.loaded_at.to_i,
  }
  "200 #{result.to_json}"
end

#stop(options) ⇒ Object



39
40
41
42
# File 'lib/procodile/control_session.rb', line 39

def stop(options)
  instances = @supervisor.stop(:processes => options['processes'], :stop_supervisor => options['stop_supervisor'])
  "200 " + instances.map(&:to_hash).to_json
end