Class: Cognizant::Process

Inherits:
Object
  • Object
show all
Includes:
Actions, Attributes, Children, Execution, PID, Status
Defined in:
lib/cognizant/process.rb,
lib/cognizant/process/pid.rb,
lib/cognizant/process/status.rb,
lib/cognizant/process/actions.rb,
lib/cognizant/process/children.rb,
lib/cognizant/process/triggers.rb,
lib/cognizant/process/dsl_proxy.rb,
lib/cognizant/process/execution.rb,
lib/cognizant/process/attributes.rb,
lib/cognizant/process/conditions.rb,
lib/cognizant/process/actions/stop.rb,
lib/cognizant/process/actions/start.rb,
lib/cognizant/process/actions/restart.rb,
lib/cognizant/process/trigger_delegate.rb,
lib/cognizant/process/triggers/trigger.rb,
lib/cognizant/process/triggers/flapping.rb,
lib/cognizant/process/condition_delegate.rb,
lib/cognizant/process/triggers/transition.rb,
lib/cognizant/process/conditions/cpu_usage.rb,
lib/cognizant/process/conditions/always_true.rb,
lib/cognizant/process/conditions/memory_usage.rb,
lib/cognizant/process/conditions/poll_condition.rb

Defined Under Namespace

Modules: Actions, Attributes, Children, Conditions, Execution, PID, Status, Triggers Classes: ConditionDelegate, DSLProxy, TriggerDelegate

Instance Attribute Summary

Attributes included from Actions::Restart

#restart_after_command, #restart_before_command, #restart_command, #restart_env, #restart_expect_stopped, #restart_signals, #restart_timeout

Attributes included from Actions::Stop

#stop_after_command, #stop_before_command, #stop_command, #stop_env, #stop_signals, #stop_timeout

Attributes included from Actions::Start

#start_after_command, #start_before_command, #start_command, #start_env, #start_timeout, #start_with_input, #start_with_input_command, #start_with_input_file

Attributes included from Attributes

#autostart, #chdir, #chroot, #daemonize, #env, #errfile, #gid, #group, #groups, #name, #pid_command, #ping_command, #uid, #umask

Instance Method Summary collapse

Methods included from Children

#create_child_process, #refresh_children!

Methods included from Actions::Restart

#_restart_result_handler, #reset_attributes!, #restart_expect_stopped!, #restart_process

Methods included from Actions::Stop

#_stop_result_handler, #reset_attributes!, #stop_process

Methods included from Actions::Start

#_start_result_handler, #reset_attributes!, #start_process

Methods included from Attributes

#autostart!, #daemonize!, #reset_attributes!

Methods included from Execution

#execute

Methods included from Status

#pid_running?, #process_running?, #signal

Methods included from PID

#cached_pid, #read_pid, #unlink_pid, #write_pid

Constructor Details

#initialize(process_name = nil, attributes = {}, &block) ⇒ Process

Returns a new instance of Process.



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/cognizant/process.rb', line 89

def initialize(process_name = nil, attributes = {}, &block)
  reset!

  @name = process_name.to_s if process_name

  set_attributes(attributes)

  handle_initialize_block(&block) if block

  raise "Process name is missing. Aborting." unless self.name
  Log[self].info "Loading process #{self.name}..."

  # Let state_machine initialize as well.
  initialize_state_machines
end

Instance Method Details

#check(check_name, options, &block) ⇒ Object



126
127
128
129
130
131
132
# File 'lib/cognizant/process.rb', line 126

def check(check_name, options, &block)
  if klass = Cognizant::Process::Conditions[check_name]
    @conditions << ConditionDelegate.new(check_name, options.deep_symbolize_keys!, &block)
  elsif klass = Cognizant::Process::Triggers[check_name]
    @triggers << TriggerDelegate.new(check_name, self, options.deep_symbolize_keys!, &block)
  end
end

#dispatch!(action, reason = nil) ⇒ Object



181
182
183
184
185
186
187
188
189
# File 'lib/cognizant/process.rb', line 181

def dispatch!(action, reason = nil)
  @action_mutex.synchronize do
    if action.respond_to?(:call)
      action.call(self)
    else
      self.send("#{action}")
    end
  end
end

#handle_initialize_block(&block) ⇒ Object



105
106
107
108
109
110
111
112
# File 'lib/cognizant/process.rb', line 105

def handle_initialize_block(&block)
  if block.arity == 0
    attributes = Cognizant::Process::DSLProxy.new(self, &block).attributes
    set_attributes(attributes)
  else
    instance_exec(self, &block)
  end
end

#handle_user_command(command) ⇒ Object



174
175
176
177
178
179
# File 'lib/cognizant/process.rb', line 174

def handle_user_command(command)
  # When the user issues a command, reset any
  # triggers so that scheduled events gets cleared.
  @triggers.each { |trigger| trigger.reset! }
  dispatch!(command, "user initiated")
end

#last_transition_timeObject



170
171
172
# File 'lib/cognizant/process.rb', line 170

def last_transition_time
  @last_transition_time || 0
end

#logfileObject



166
167
168
# File 'lib/cognizant/process.rb', line 166

def logfile
  @logfile || File.join(@application.logs_dir, @name + '.log')
end

#monitor_children(child_process_attributes = {}, &child_process_block) ⇒ Object



134
135
136
137
# File 'lib/cognizant/process.rb', line 134

def monitor_children(child_process_attributes = {}, &child_process_block)
  @monitor_children = true
  @child_process_attributes, @child_process_block = child_process_attributes, child_process_block
end

#pidfileObject



162
163
164
# File 'lib/cognizant/process.rb', line 162

def pidfile
  @pidfile || File.join(@application.pids_dir, @name + '.pid')
end

#reset!Object



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/cognizant/process.rb', line 114

def reset!
  reset_attributes!

  @application = nil
  @ticks_to_skip = 0
  @conditions = []
  @triggers = []
  @children = []
  @action_mutex = Monitor.new
  @monitor_children = false
end

#skip_ticks_for(skips) ⇒ Object



156
157
158
159
160
# File 'lib/cognizant/process.rb', line 156

def skip_ticks_for(skips)
  # Accept negative skips with the result being >= 0.
  # +1 so that we don't have to >= and ensure 0 in #skip_tick?.
  @ticks_to_skip = [@ticks_to_skip + (skips.to_i + 1), 0].max
end

#tickObject



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/cognizant/process.rb', line 139

def tick
  return if skip_tick?
  @action_thread.kill if @action_thread # TODO: Ensure if this is really needed.

  # Invoke the state_machine event.
  super

  if self.running? # State method.
    run_conditions

    if @monitor_children
      refresh_children!
      @children.each(&:tick)
    end
  end
end