Class: FileMonitoring::FileMonitoring

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

Overview

Manages file monitoring of number of file system locations

Instance Method Summary collapse

Instance Method Details

#monitor_filesObject

The main method. Loops on all paths, each time span and monitors them.

Algorithm:

There is a loop that performs at every iteration:

1.Pull entry with a minimal time of check from queue
2.Recursively check path taken from entry for changes
  a.Notify subscribed processes on changes
3.Push entry to the queue with new time of next check

This methods controlled by monitoring_paths configuration parameter, that provides path and file monitoring configuration data



29
30
31
32
33
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/file_monitoring/file_monitoring.rb', line 29

def monitor_files
  conf_array = Params['monitoring_paths']

  # Directories states stored in the priority queue,
  # where the key (priority) is a time when it should be checked next time.
  # Priority queue means that all entries arranged by key (time to check) in increasing order.
  pq = Containers::PriorityQueue.new
  conf_array.each { |elem|
    priority = (Time.now + elem['scan_period']).to_i
    dir_stat = DirStat.new(File.expand_path(elem['path']), elem['stable_state'])
    dir_stat.set_event_queue(@event_queue) if @event_queue
    Log.debug1 "File monitoring started for: #{elem}"
    pq.push([priority, elem, dir_stat], -priority)
  }

  #init log4r
  monitoring_log_path = Params['default_monitoring_log_path']
  Log.debug1 'File monitoring log: ' + Params['default_monitoring_log_path']
  monitoring_log_dir = File.dirname(monitoring_log_path)
  FileUtils.mkdir_p(monitoring_log_dir) unless File.exists?(monitoring_log_dir)

  @log4r = Log4r::Logger.new 'BBFS monitoring log'
  @log4r.trace = true
  formatter = Log4r::PatternFormatter.new(:pattern => "[%d] [%m]")
  #file setup
  file_config = {
      "filename" => Params['default_monitoring_log_path'],
      "maxsize" => Params['log_rotation_size'],
      "trunc" => true
  }
  file_outputter = Log4r::RollingFileOutputter.new("monitor_log", file_config)
  file_outputter.level = Log4r::INFO
  file_outputter.formatter = formatter
  @log4r.outputters << file_outputter
  FileStat.set_log(@log4r)

  while true do
    # pull entry that should be checked next,
    # according to it's scan_period
    time, conf, dir_stat = pq.pop

    # time remains to wait before directory should be checked
    time_span = time - Time.now.to_i
    if (time_span > 0)
      sleep(time_span)
    end
    dir_stat.monitor

    # push entry with new a next time it should be checked as a priority key
    priority = (Time.now + conf['scan_period']).to_i
    pq.push([priority, conf, dir_stat], -priority)
  end

  log.close
end

#set_event_queue(queue) ⇒ Object

Set event queue used for communication between different proceses.

Parameters:

  • queue (Queue)


14
15
16
# File 'lib/file_monitoring/file_monitoring.rb', line 14

def set_event_queue(queue)
  @event_queue = queue
end