@@ -16,50 +16,48 @@ class Monitor < Base::Monitor
1616 # unit: 'daily', 'hourly', or 'singular'
1717 # timezone: '+09:00'
1818 # span_in_days: 32
19- def initialize ( connection , resource_uri_prefix , resource )
19+ # @param [Hash] last_modification_times for a resource
20+ def initialize ( connection , resource_uri_prefix , resource , last_modification_times )
2021 @connection = connection
2122 @resource_uri_prefix = resource_uri_prefix
2223 @resource = resource
2324 @status = Triglav ::Agent ::Status . new ( resource_uri_prefix , resource . uri )
24- @last_modification_times = get_last_modification_times
25+ @last_modification_times = get_last_modification_times ( last_modification_times )
2526 end
2627
2728 def process
2829 unless resource_valid?
2930 $logger. warn { "Broken resource: #{ resource . to_s } " }
30- return nil
31+ return [ nil , nil ]
3132 end
32- $logger. debug { "Start process #{ resource . uri } " }
33+ started = Time . now
34+ $logger. debug { "Start Monitor#process #{ resource . uri } " }
3335
3436 events , new_last_modification_times = get_events
3537
36- $logger. debug { "Finish process #{ resource . uri } " }
38+ elapsed = Time . now - started
39+ $logger. debug { "Finish Monitor#process #{ resource . uri } elapsed:#{ elapsed . to_f } " }
3740
38- return nil if events . nil? || events . empty?
39- yield ( events ) if block_given? # send_message
40- update_status_file ( new_last_modification_times )
41- true
41+ return [ nil , nil ] if events . nil? or events . empty?
42+ [ events , new_last_modification_times ]
4243 end
4344
4445 private
4546
4647 def get_events
4748 new_last_modification_times = get_new_last_modification_times
4849 latest_files = select_latest_files ( new_last_modification_times )
50+ new_last_modification_times [ :max ] = new_last_modification_times . values . max
4951 events = build_events ( latest_files )
5052 [ events , new_last_modification_times ]
5153 rescue => e
5254 $logger. warn { "#{ e . class } #{ e . message } #{ e . backtrace . join ( "\n " ) } " }
5355 nil
5456 end
5557
56- def update_status_file ( last_modification_times )
57- last_modification_times [ :max ] = last_modification_times . values . max
58- @status . set ( last_modification_times )
59- end
60-
61- def get_last_modification_times
62- last_modification_times = @status . get || { }
58+ def get_last_modification_times ( last_modification_times )
59+ last_modification_times ||= { }
60+ # ToDo: want to remove accessing Status in Monitor class
6361 max_last_modification_time = last_modification_times [ :max ] || @status . getsetnx ( [ :max ] , $setting. debug? ? 0 : get_current_time )
6462 removes = last_modification_times . keys - paths . keys
6563 appends = paths . keys - last_modification_times . keys
0 commit comments