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

Returns a new instance of CronManager.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rservicebus2/cron_manager.rb', line 47

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



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rservicebus2/cron_manager.rb', line 34

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



25
26
27
28
29
30
31
32
# File 'lib/rservicebus2/cron_manager.rb', line 25

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

#runObject



65
66
67
68
69
70
71
72
73
74
# File 'lib/rservicebus2/cron_manager.rb', line 65

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