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.



65
66
67
# File 'lib/eye/process/scheduler.rb', line 65

def current_scheduled_command
  @current_scheduled_command
end

#last_scheduled_atObject

Returns the value of attribute last_scheduled_at.



66
67
68
# File 'lib/eye/process/scheduler.rb', line 66

def last_scheduled_at
  @last_scheduled_at
end

#last_scheduled_commandObject

Returns the value of attribute last_scheduled_command.



66
67
68
# File 'lib/eye/process/scheduler.rb', line 66

def last_scheduled_command
  @last_scheduled_command
end

#last_scheduled_reasonObject

Returns the value of attribute last_scheduled_reason.



66
67
68
# File 'lib/eye/process/scheduler.rb', line 66

def last_scheduled_reason
  @last_scheduled_reason
end

Class Method Details

.included(base) ⇒ Object



61
62
63
# File 'lib/eye/process/scheduler.rb', line 61

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
19
20
21
22
23
24
25
26
27
# File 'lib/eye/process/scheduler.rb', line 4

def schedule(command, *args, &block)
  if scheduler.alive?
    unless self.respond_to?(command, true)
      warn ":#{command} scheduling is unsupported"
      return
    end

    reason = if args.present? && args[-1].kind_of?(Eye::Reason)
      args.pop
    end

    info "schedule :#{command} #{reason ? "(reason: #{reason})" : nil}"

    if reason.class == Eye::Reason
      # for auto reasons
      # skip already running commands and all in chain
      scheduler.add_wo_dups_current(:scheduled_action, command, {:args => args, :reason => reason}, &block)
    else
      # for manual, or without reason
      # skip only for last in chain
      scheduler.add_wo_dups(:scheduled_action, command, {:args => args, :reason => reason}, &block)
    end
  end
end

#schedule_historyObject



68
69
70
# File 'lib/eye/process/scheduler.rb', line 68

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

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



29
30
31
32
33
34
35
# File 'lib/eye/process/scheduler.rb', line 29

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



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/eye/process/scheduler.rb', line 37

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



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

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

#scheduler_clear_pending_listObject



57
58
59
# File 'lib/eye/process/scheduler.rb', line 57

def scheduler_clear_pending_list
  scheduler.clear_pending_list
end