Module: Eye::Process::Scheduler

Included in:
ChildProcess, Group, Eye::Process
Defined in:
lib/eye/process/scheduler.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#current_scheduled_commandObject

Returns the value of attribute current_scheduled_command.



52
53
54
# File 'lib/eye/process/scheduler.rb', line 52

def current_scheduled_command
  @current_scheduled_command
end

#last_scheduled_atObject

Returns the value of attribute last_scheduled_at.



53
54
55
# File 'lib/eye/process/scheduler.rb', line 53

def last_scheduled_at
  @last_scheduled_at
end

#last_scheduled_commandObject

Returns the value of attribute last_scheduled_command.



53
54
55
# File 'lib/eye/process/scheduler.rb', line 53

def last_scheduled_command
  @last_scheduled_command
end

#last_scheduled_reasonObject

Returns the value of attribute last_scheduled_reason.



53
54
55
# File 'lib/eye/process/scheduler.rb', line 53

def last_scheduled_reason
  @last_scheduled_reason
end

Class Method Details

.included(base) ⇒ Object



48
49
50
# File 'lib/eye/process/scheduler.rb', line 48

def self.included(base)
  base.finalizer :remove_scheduler
end

Instance Method Details

#schedule(command, *args, &block) ⇒ Object

ex: schedule :update_config, config, “reason: update_config”



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/eye/process/scheduler.rb', line 4

def schedule(command, *args, &block)
  if scheduler.alive?
    unless self.respond_to?(command, true)
      warn "object not support :#{command} to schedule"
      return
    end

    reason = if args.present? && [String, Symbol].include?(args[-1].class)
      args.pop
    end

    info "schedule :#{command} #{reason ? "(reason: #{reason})" : nil}"
    scheduler.add_wo_dups(:scheduled_action, command, {:args => args, :reason => reason}, &block)
  end
end

#schedule_historyObject



55
56
57
# File 'lib/eye/process/scheduler.rb', line 55

def schedule_history
  @schedule_history ||= Eye::Process::StatesHistory.new(50)
end

#schedule_in(interval, command, *args, &block) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/eye/process/scheduler.rb', line 20

def schedule_in(interval, command, *args, &block)
  debug "schedule_in #{interval} :#{command} #{args}"
  after(interval.to_f) do
    debug "scheduled_in #{interval} :#{command} #{args}"
    schedule(command, *args, &block)
  end
end

#scheduled_action(command, h = {}, &block) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/eye/process/scheduler.rb', line 28

def scheduled_action(command, h = {}, &block)
  reason = h.delete(:reason)
  info "=> #{command} #{h[:args].present? ? "#{h[:args]*',' }" : nil} #{reason ? "(reason: #{reason})" : nil}"

  @current_scheduled_command = command
  @last_scheduled_command = command
  @last_scheduled_reason = reason
  @last_scheduled_at = Time.now

  send(command, *h[:args], &block)
  @current_scheduled_command = nil
  info "<= #{command}"

  schedule_history.push(command, reason, @last_scheduled_at.to_i)
end

#scheduler_actions_listObject



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

def scheduler_actions_list
  scheduler.list.map{|c| c[:args].first rescue nil }.compact
end