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.



79
80
81
# File 'lib/eye/process/scheduler.rb', line 79

def current_scheduled_command
  @current_scheduled_command
end

#last_scheduled_atObject

Returns the value of attribute last_scheduled_at.



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

def last_scheduled_at
  @last_scheduled_at
end

#last_scheduled_commandObject

Returns the value of attribute last_scheduled_command.



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

def last_scheduled_command
  @last_scheduled_command
end

#last_scheduled_reasonObject

Returns the value of attribute last_scheduled_reason.



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

def last_scheduled_reason
  @last_scheduled_reason
end

Class Method Details

.included(base) ⇒ Object



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

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

Instance Method Details

#execute_proc(*_args, &block) ⇒ Object



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

def execute_proc(*_args, &block)
  self.instance_exec(&block)
rescue Object => ex
  log_ex(ex)
end

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

def schedule(command, *args, &block)
  return unless 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 = args.pop if args.present? && args[-1].is_a?(Eye::Reason)

  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: 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: block)
  end
end

#schedule_historyObject



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

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

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



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

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 = {}) ⇒ Object



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

def scheduled_action(command, h = {})
  reason = h[: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], &h[:block])
  @current_scheduled_command = nil
  info "<= #{command}"

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

  if parent = self.try(:parent)
    parent.schedule_history.push("#{command}_child", reason, @last_scheduled_at.to_i)
  end
end

#scheduler_actions_listObject



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

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

#scheduler_clear_pending_listObject



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

def scheduler_clear_pending_list
  scheduler.clear_pending_list
end

#scheduler_freezeObject



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

def scheduler_freeze
  @scheduler_freeze = true
end

#scheduler_freeze?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/eye/process/scheduler.rb', line 94

def scheduler_freeze?
  @scheduler_freeze
end

#scheduler_unfreezeObject



90
91
92
# File 'lib/eye/process/scheduler.rb', line 90

def scheduler_unfreeze
  @scheduler_freeze = nil
end