Class: Nodectl::Instance

Inherits:
Process show all
Defined in:
lib/nodectl/instance.rb

Constant Summary

Constants inherited from Process

Process::ENV_BLACKLIST, Process::TIMEOUT

Instance Attribute Summary collapse

Attributes inherited from Process

#exit_status, #pid, #status

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Process

#alive?, #dead!, #oncrash, #onkill, #onstop, #restart, #stop

Constructor Details

#initialize(service, command, options = {}, &blk) ⇒ Instance

Returns a new instance of Instance.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/nodectl/instance.rb', line 8

def initialize(service, command, options = {}, &blk)
  super(options) do
    blk.call if blk
    Process.exec command
  end

  @service = service
  @options = options
  @command = command

  if @options[:started]
    @status = :running
    @pid    = @options[:pid]
    @detach = Process.detach(@pid)

    Nodectl::Instance[@pid] = self
    Nodectl.logger.info("#{@service.name} [#{@pid}] found")
  end

  onkill do
    self.class.delete(pid, reason: @status)
  end
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



6
7
8
# File 'lib/nodectl/instance.rb', line 6

def command
  @command
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/nodectl/instance.rb', line 5

def options
  @options
end

#serviceObject (readonly)

Returns the value of attribute service.



4
5
6
# File 'lib/nodectl/instance.rb', line 4

def service
  @service
end

Class Method Details

.delete(pid, options = {}) ⇒ Object



81
82
83
84
# File 'lib/nodectl/instance.rb', line 81

def delete(pid, options = {})
  super(pid, options)
  save_register
end

.load(dump) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/nodectl/instance.rb', line 62

def load(dump)
  service = Nodectl::Service.find(dump["service_name"])

  if service
    puts "Service found: #{dump['service_name']}"
    new(service, dump["command"], dump["options"].merge(started: true, pid: dump["pid"]))
  else
    puts "Service not found"
    raise "service not found"
  end
end

.load_registerObject



90
91
92
93
94
95
96
97
98
# File 'lib/nodectl/instance.rb', line 90

def load_register
  database = Nodectl.database["instances"] || []

  database.each do |dump|
    load(dump) rescue RuntimeError
  end

  save_register
end

.push(pid, instance) ⇒ Object Also known as: []=



74
75
76
77
# File 'lib/nodectl/instance.rb', line 74

def push(pid, instance)
  super(pid, instance)
  save_register
end

.save_registerObject



86
87
88
# File 'lib/nodectl/instance.rb', line 86

def save_register
  Nodectl.database["instances"] = all.map(&:dump)
end

Instance Method Details

#as_jsonObject



39
40
41
42
43
44
45
46
# File 'lib/nodectl/instance.rb', line 39

def as_json
  {
    "id"         => pid,
    "service_id" => service.name,
    "status"     => status,
    "options"    => options
  }
end

#dumpObject



48
49
50
51
52
53
54
55
# File 'lib/nodectl/instance.rb', line 48

def dump
  {
    "service_name" => service.name,
    "pid"          => pid,
    "options"      => options,
    "command"      => command
  }
end

#pre_fork_blockObject



57
58
59
# File 'lib/nodectl/instance.rb', line 57

def pre_fork_block
  Nodectl.shut_up!
end

#runObject



32
33
34
35
36
37
# File 'lib/nodectl/instance.rb', line 32

def run
  super

  Nodectl::Instance[@pid] = self
  Nodectl.logger.info("#{@service.name} [#{@pid}] started")
end