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


70
71
72
# File 'lib/eye/process/scheduler.rb', line 70

def current_scheduled_command
  @current_scheduled_command
end

#last_scheduled_atObject

Returns the value of attribute last_scheduled_at


71
72
73
# File 'lib/eye/process/scheduler.rb', line 71

def last_scheduled_at
  @last_scheduled_at
end

#last_scheduled_commandObject

Returns the value of attribute last_scheduled_command


71
72
73
# File 'lib/eye/process/scheduler.rb', line 71

def last_scheduled_command
  @last_scheduled_command
end

#last_scheduled_reasonObject

Returns the value of attribute last_scheduled_reason


71
72
73
# File 'lib/eye/process/scheduler.rb', line 71

def last_scheduled_reason
  @last_scheduled_reason
end

Class Method Details

.included(base) ⇒ Object


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

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
28
29
30
31
32
# File 'lib/eye/process/scheduler.rb', line 4

def schedule(command, *args, &block)
  if scheduler.alive?
    if scheduler_freeze?
      warn ":#{command} ignoring to schedule, because scheduler is freeze"
      return
    end

    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


73
74
75
# File 'lib/eye/process/scheduler.rb', line 73

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

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


34
35
36
37
38
39
40
# File 'lib/eye/process/scheduler.rb', line 34

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


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/eye/process/scheduler.rb', line 42

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


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

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

#scheduler_clear_pending_listObject


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

def scheduler_clear_pending_list
  scheduler.clear_pending_list
end

#scheduler_freezeObject


77
78
79
# File 'lib/eye/process/scheduler.rb', line 77

def scheduler_freeze
  @scheduler_freeze = true
end

#scheduler_freeze?Boolean

Returns:

  • (Boolean)

85
86
87
# File 'lib/eye/process/scheduler.rb', line 85

def scheduler_freeze?
  @scheduler_freeze
end

#scheduler_unfreezeObject


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

def scheduler_unfreeze
  @scheduler_freeze = nil
end