Class: Nodectl::Action

Inherits:
Process show all
Defined in:
lib/nodectl/action.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, name, options = {}, &blk) ⇒ Action

Returns a new instance of Action.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/nodectl/action.rb', line 9

def initialize(service, name, options = {}, &blk)
  super(options, &blk)

  @id      = Nodectl::Action.next_id
  @service = service
  @name    = name

  onkill do
    result = if @exit_status == 0
                "succeed"
             else
                "failed"
             end

    self.class.onkill_call(self, reason: result)
  end
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#outputObject (readonly)

Returns the value of attribute output.



7
8
9
# File 'lib/nodectl/action.rb', line 7

def output
  @output
end

#serviceObject (readonly)

Returns the value of attribute service.



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

def service
  @service
end

Class Method Details

.onkill(&blk) ⇒ Object



65
66
67
68
# File 'lib/nodectl/action.rb', line 65

def onkill(&blk)
  @onkill ||= []
  @onkill << blk
end

.onkill_call(action, options = {}) ⇒ Object



70
71
72
# File 'lib/nodectl/action.rb', line 70

def onkill_call(action, options = {})
  @onkill.each { |b| b.call(action, options) }
end

.run(service, name, *args, &blk) ⇒ Object



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

def run(service, name, *args, &blk)
  action = new(service, name, *args, &blk)
  action.run
  action
end

Instance Method Details

#as_jsonObject



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/nodectl/action.rb', line 40

def as_json
  {
    "id"          => @id,
    "pid"         => @pid,
    "name"        => @name,
    "service_id"  => @service.name,
    "ws"          => "/actions/#{id}",
    "exit_status" => @exit_status,
    "started_at"  => @started_at.iso8601
  }
end

#runObject



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

def run
  rout, @wout = IO.pipe

  @output = Nodectl::Stream::FileWithMemory.new(rout)

  super

  @started_at = Time.now

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