Class: RServiceBus2::CronManager

Inherits:
Object
  • Object
show all
Defined in:
lib/rservicebus2/cron_manager.rb

Overview

Cron Manager

Instance Method Summary collapse

Constructor Details

#initialize(host, msg_names = []) ⇒ CronManager

rubocop:disable Metrics/MethodLength



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rservicebus2/cron_manager.rb', line 51

def initialize(host, msg_names = [])
  @bus = host
  @msg_names = msg_names

  RServiceBus2.rlog 'Load Cron'
  @list = []
  ENV.each do |k, vs|
    if k.start_with?('RSBCRON_')
      add_cron(k.sub('RSBCRON_', ''), vs)
    elsif k.start_with?('RSBCRON')
      vs.split(';').each do |v|
        parts = v.split(' ', 6)
        add_cron(parts.pop, parts.join(' '))
      end
    end
  end
end

Instance Method Details

#add_cron(name, cron_string) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rservicebus2/cron_manager.rb', line 37

def add_cron(name, cron_string)
  get_matching_msg_names(name).each do |n|
    hash = {}
    hash['name'] = n
    hash['last'] = Time.now
    hash['v'] = cron_string
    hash['cron'] = CronParser.new(cron_string)
    hash['next'] = hash['cron'].next(Time.now)
    @list << hash
    @bus.log("Cron set for, #{n}, #{cron_string}, next run, #{hash['next']}")
  end
end

#get_matching_msg_names(name) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/rservicebus2/cron_manager.rb', line 27

def get_matching_msg_names(name)
  list = []
  @msg_names.each do |n|
    list << n if Globber.new(name) =~ n
  end
  raise NoMatchingMsgForCron, name if list.empty?

  list
end

#runObject

rubocop:enable Metrics/MethodLength



70
71
72
73
74
75
76
77
78
79
# File 'lib/rservicebus2/cron_manager.rb', line 70

def run
  now = Time.now
  @list.each_with_index do |v, idx|
    next if now <= v['next']

    RServiceBus2.rlog "CronManager.Send, #{v['name']}"
    @bus.send(RServiceBus2.create_anonymous_class(v['name']))
    @list[idx]['next'] = v['cron'].next(now)
  end
end