Module: Eye::Process::Scheduler

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

Instance Method Summary collapse

Instance Method Details

#ensure_scheduler_processObject



115
116
117
118
119
120
# File 'lib/eye/process/scheduler.rb', line 115

def ensure_scheduler_process
  unless @scheduler_running
    @scheduler_running = true
    async.scheduler_run
  end
end

#equal_action_call?(call1, call2) ⇒ Boolean

Returns:

  • (Boolean)


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

def equal_action_call?(call1, call2)
  call1 && call2 && (call1[:command] == call2[:command]) && (call1[:args] == call2[:args]) && (call1[:block] == call2[:block])
end

#filter_call(call) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/eye/process/scheduler.rb', line 90

def filter_call(call)
  # for auto reasons, compare call with current @scheduled_call
  return false if call[:by] != :user && equal_action_call?(@scheduled_call, call)

  # check any equal call in queue scheduler_calls
  scheduler_calls.none? { |c| equal_action_call?(c, call) }
end

#internal_schedule(call) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/eye/process/scheduler.rb', line 34

def internal_schedule(call)
  if interval = call[:in]
    debug { "schedule_in #{interval} :#{call[:command]}" }
    after(interval.to_f) do
      debug { "scheduled_in #{interval} :#{call[:command]}" }
      call[:in] = nil
      internal_schedule(call)
    end
    return
  end

  call[:at] ||= Time.now.to_i
  command = call[:command]

  @scheduler_freeze = false if call[:freeze] == false

  if @scheduler_freeze
    warn ":#{command} ignoring to schedule, because scheduler is freezed"
    return
  end

  unless self.respond_to?(command, true)
    warn ":#{command} scheduling is unsupported"
    return
  end

  if filter_call(call)
    info "schedule :#{command} (#{reason_from_call(call)})"
    scheduler_add(call)
  else
    info "not scheduled: #{command} (#{reason_from_call(call)})"
  end

  @scheduler_freeze = true if call[:freeze] == true
end

#schedule(*args) ⇒ Object

2 Forms of schedule: schedule command: ‘bla’, args: [1, 2, 3] schedule :bla, 1, 2, 3



26
27
28
29
30
31
32
# File 'lib/eye/process/scheduler.rb', line 26

def schedule(*args)
  if args[0].is_a?(Hash)
    internal_schedule(args[0])
  else
    internal_schedule(command: args[0], args: args[1..-1])
  end
end

#scheduler_add(call) ⇒ Object



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

def scheduler_add(call)
  scheduler_calls << call
  ensure_scheduler_process
end

#scheduler_callsObject



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

def scheduler_calls
  @scheduler_calls ||= []
end

#scheduler_clear_pending_listObject



135
136
137
# File 'lib/eye/process/scheduler.rb', line 135

def scheduler_clear_pending_list
  scheduler_calls.clear
end

#scheduler_commands_listObject



131
132
133
# File 'lib/eye/process/scheduler.rb', line 131

def scheduler_commands_list
  scheduler_calls.map { |c| c[:command] }
end

#scheduler_consume(call) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/eye/process/scheduler.rb', line 70

def scheduler_consume(call)
  args = call[:args]
  reason_str = reason_from_call(call)
  info "=> #{call[:command]} #{args.present? ? args.join(',') : nil} (#{reason_str})"
  send(call[:command], *args, &call[:block])

  history_cmd = is_a?(Eye::ChildProcess) ? "#{call[:command]}_child" : call[:command]
  scheduler_history.push(history_cmd, reason_str, call[:at])

rescue Object => ex
  raise(ex) if ex.class == Celluloid::TaskTerminated
  log_ex(ex)

ensure
  # signaling to waiter
  call[:signal].try :signal

  info "<= #{call[:command]}"
end

#scheduler_current_commandObject



147
148
149
# File 'lib/eye/process/scheduler.rb', line 147

def scheduler_current_command
  @scheduled_call.try :[], :command
end

#scheduler_historyObject



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

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

#scheduler_last_commandObject



139
140
141
# File 'lib/eye/process/scheduler.rb', line 139

def scheduler_last_command
  @last_scheduled_call.try :[], :command
end

#scheduler_last_reasonObject



143
144
145
# File 'lib/eye/process/scheduler.rb', line 143

def scheduler_last_reason
  reason_from_call(@last_scheduled_call)
end

#scheduler_runObject



122
123
124
125
126
127
128
129
# File 'lib/eye/process/scheduler.rb', line 122

def scheduler_run
  while @scheduled_call = scheduler_calls.shift
    @scheduler_running = true
    @last_scheduled_call = @scheduled_call
    scheduler_consume(@scheduled_call)
  end
  @scheduler_running = false
end

#send_call(call) ⇒ Object

:update_config, :start, :stop, :restart, :unmonitor, :monitor, :break_chain, :delete, :signal, :user_command



14
15
16
# File 'lib/eye/process/scheduler.rb', line 14

def send_call(call)
  user_schedule(call)
end

#user_schedule(call) ⇒ Object



18
19
20
21
# File 'lib/eye/process/scheduler.rb', line 18

def user_schedule(call)
  call[:by] ||= :user
  internal_schedule(call)
end