Class: Nodectl::Instance
Constant Summary
Constants inherited from Process
Process::ENV_BLACKLIST, Process::TIMEOUT
Instance Attribute Summary collapse
-
#command ⇒ Object
readonly
Returns the value of attribute command.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#service ⇒ Object
readonly
Returns the value of attribute service.
Attributes inherited from Process
Class Method Summary collapse
- .delete(pid, options = {}) ⇒ Object
- .load(dump) ⇒ Object
- .load_register ⇒ Object
- .push(pid, instance) ⇒ Object (also: []=)
- .save_register ⇒ Object
Instance Method Summary collapse
- #as_json ⇒ Object
- #dump ⇒ Object
-
#initialize(service, command, options = {}, &blk) ⇒ Instance
constructor
A new instance of Instance.
- #pre_fork_block ⇒ Object
- #run ⇒ Object
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, = {}, &blk) super() do blk.call if blk Process.exec command end @service = service @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
#command ⇒ Object (readonly)
Returns the value of attribute command.
6 7 8 |
# File 'lib/nodectl/instance.rb', line 6 def command @command end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
5 6 7 |
# File 'lib/nodectl/instance.rb', line 5 def @options end |
#service ⇒ Object (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, = {}) super(pid, ) 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_register ⇒ Object
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 |
Instance Method Details
#as_json ⇒ Object
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" => } end |
#dump ⇒ Object
48 49 50 51 52 53 54 55 |
# File 'lib/nodectl/instance.rb', line 48 def dump { "service_name" => service.name, "pid" => pid, "options" => , "command" => command } end |