Class: Eye::Group

Inherits:
Object show all
Includes:
Celluloid, Chain, Process::Scheduler
Defined in:
lib/eye/group.rb

Defined Under Namespace

Modules: Chain

Instance Attribute Summary collapse

Attributes included from Process::Scheduler

#current_scheduled_command, #last_scheduled_at, #last_scheduled_command, #last_scheduled_reason

Instance Method Summary collapse

Methods included from Process::Scheduler

#execute_proc, included, #schedule, #schedule_history, #schedule_in, #scheduled_action, #scheduler_actions_list, #scheduler_clear_pending_list, #scheduler_freeze, #scheduler_freeze?, #scheduler_unfreeze

Constructor Details

#initialize(name, config) ⇒ Group

Returns a new instance of Group.



14
15
16
17
18
19
20
# File 'lib/eye/group.rb', line 14

def initialize(name, config)
  @name = name
  @config = config
  @processes = Eye::Utils::AliveArray.new
  @hidden = (name == '__default__')
  debug { 'created' }
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



12
13
14
# File 'lib/eye/group.rb', line 12

def config
  @config
end

#hiddenObject (readonly)

Returns the value of attribute hidden.



12
13
14
# File 'lib/eye/group.rb', line 12

def hidden
  @hidden
end

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/eye/group.rb', line 12

def name
  @name
end

#processesObject (readonly)

Returns the value of attribute processes.



12
13
14
# File 'lib/eye/group.rb', line 12

def processes
  @processes
end

Instance Method Details

#<=>(other) ⇒ Object

to sort groups



150
151
152
153
154
155
156
157
158
159
160
# File 'lib/eye/group.rb', line 150

def <=>(other)
  if hidden
    1
  else
    if other.hidden
      -1
    else
      name <=> other.name
    end
  end
end

#add_process(process) ⇒ Object



39
40
41
# File 'lib/eye/group.rb', line 39

def add_process(process)
  @processes << process
end

#app_nameObject



26
27
28
# File 'lib/eye/group.rb', line 26

def app_name
  @config[:application]
end

#break_chainObject



131
132
133
134
135
# File 'lib/eye/group.rb', line 131

def break_chain
  info 'break chain'
  scheduler_clear_pending_list
  @chain_breaker = true
end

#clearObject



141
142
143
# File 'lib/eye/group.rb', line 141

def clear
  @processes = Eye::Utils::AliveArray.new
end

#debug_dataObject



81
82
83
# File 'lib/eye/group.rb', line 81

def debug_data
  { queue: scheduler_actions_list, chain: chain_status }
end

#deleteObject



110
111
112
113
# File 'lib/eye/group.rb', line 110

def delete
  async_schedule :delete
  terminate
end

#freezeObject



137
138
139
# File 'lib/eye/group.rb', line 137

def freeze
  async_schedule :freeze
end

#full_nameObject



30
31
32
# File 'lib/eye/group.rb', line 30

def full_name
  @full_name ||= "#{app_name}:#{@name}"
end

#logger_tagObject



22
23
24
# File 'lib/eye/group.rb', line 22

def logger_tag
  full_name
end

#monitorObject



115
116
117
# File 'lib/eye/group.rb', line 115

def monitor
  chain_command :monitor
end

#resort_processesObject

sort processes in name order



44
45
46
# File 'lib/eye/group.rb', line 44

def resort_processes
  @processes = @processes.sort_by(&:name)
end

#restartObject



106
107
108
# File 'lib/eye/group.rb', line 106

def restart
  chain_command :restart
end

#send_command(command, *args) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/eye/group.rb', line 85

def send_command(command, *args)
  info "send_command: #{command}"

  case command
    when :delete
      delete(*args)
    when :break_chain
      break_chain(*args)
    else
      schedule command, *args, Eye::Reason::User.new(command)
  end
end

#signal(sig) ⇒ Object



123
124
125
# File 'lib/eye/group.rb', line 123

def signal(sig)
  async_schedule :signal, sig
end

#startObject



98
99
100
# File 'lib/eye/group.rb', line 98

def start
  chain_command :start
end

#status_data(opts = {}) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/eye/group.rb', line 48

def status_data(opts = {})
  plist = @processes.map { |p| p.status_data(opts) }

  h = { name: name, type: :group, subtree: plist }

  h[:debug] = debug_data if opts[:debug]

  # show current chain
  if current_scheduled_command
    h.update(current_command: current_scheduled_command)

    if (chain_commands = scheduler_actions_list) && chain_commands.present?
      h.update(chain_commands: chain_commands)
    end

    if @chain_processes_current && @chain_processes_count
      h.update(chain_progress: [@chain_processes_current, @chain_processes_count])
    end
  end

  h
end

#status_data_shortObject



71
72
73
74
75
76
77
78
79
# File 'lib/eye/group.rb', line 71

def status_data_short
  h = {}
  @processes.each do |p|
    state = p.state
    h[state] ||= 0
    h[state] += 1
  end
  { name: (@name == '__default__' ? 'default' : @name), type: :group, states: h }
end

#stopObject



102
103
104
# File 'lib/eye/group.rb', line 102

def stop
  async_schedule :stop
end

#sub_object?(obj) ⇒ Boolean

Returns:

  • (Boolean)


145
146
147
# File 'lib/eye/group.rb', line 145

def sub_object?(obj)
  @processes.include?(obj)
end

#unmonitorObject



119
120
121
# File 'lib/eye/group.rb', line 119

def unmonitor
  async_schedule :unmonitor
end

#update_config(cfg) ⇒ Object



34
35
36
37
# File 'lib/eye/group.rb', line 34

def update_config(cfg)
  @config = cfg
  @full_name = nil
end

#user_command(cmd) ⇒ Object



127
128
129
# File 'lib/eye/group.rb', line 127

def user_command(cmd)
  async_schedule :user_command, cmd
end