Class: FileMonitoring::FileStat
- Inherits:
-
Object
- Object
- FileMonitoring::FileStat
- Defined in:
- lib/file_monitoring/monitor_path.rb
Overview
This class holds current state of file and methods to control and report changes
Direct Known Subclasses
Constant Summary collapse
- DEFAULT_STABLE_STATE =
10
- @@log =
nil
Instance Attribute Summary collapse
-
#cycles ⇒ Object
readonly
Returns the value of attribute cycles.
-
#modification_time ⇒ Object
Returns the value of attribute modification_time.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#size ⇒ Object
Returns the value of attribute size.
-
#stable_state ⇒ Object
readonly
Returns the value of attribute stable_state.
-
#state ⇒ Object
Returns the value of attribute state.
Class Method Summary collapse
-
.set_log(log) ⇒ Object
Sets a log file to report changes ==== Arguments:.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Checks whether path and state are the same as of the argument.
-
#changed?(file_stats) ⇒ Boolean
Checks that stored file attributes are the same as file attributes taken from file system.
-
#initialize(path, stable_state = DEFAULT_STABLE_STATE) ⇒ FileStat
constructor
Initializes new file monitoring object ==== Arguments:.
-
#monitor ⇒ Object
Checks whether file was changed from the last iteration.
- #set_event_queue(queue) ⇒ Object
- #set_output_queue(event_queue) ⇒ Object
-
#set_state(new_state) ⇒ Object
Sets and writes to the log a new state.
-
#to_s(indent = 0) ⇒ Object
Returns path and state of the file with indentation.
Constructor Details
#initialize(path, stable_state = DEFAULT_STABLE_STATE) ⇒ FileStat
Initializes new file monitoring object
Arguments:
-
path
- File location -
size
- File size [Byte] -
modification_time
- file mod time [seconds] -
cycles
- # number of iterations from the last state change. -
stable_state
- Number of iterations to move unchanged file to stable state
41 42 43 44 45 46 47 |
# File 'lib/file_monitoring/monitor_path.rb', line 41 def initialize(path, stable_state = DEFAULT_STABLE_STATE) @path ||= path @size = nil @modification_time = nil @cycles = 0 @stable_state = stable_state end |
Instance Attribute Details
#cycles ⇒ Object (readonly)
Returns the value of attribute cycles.
26 27 28 |
# File 'lib/file_monitoring/monitor_path.rb', line 26 def cycles @cycles end |
#modification_time ⇒ Object
Returns the value of attribute modification_time.
27 28 29 |
# File 'lib/file_monitoring/monitor_path.rb', line 27 def modification_time @modification_time end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
26 27 28 |
# File 'lib/file_monitoring/monitor_path.rb', line 26 def path @path end |
#size ⇒ Object
Returns the value of attribute size.
27 28 29 |
# File 'lib/file_monitoring/monitor_path.rb', line 27 def size @size end |
#stable_state ⇒ Object (readonly)
Returns the value of attribute stable_state.
26 27 28 |
# File 'lib/file_monitoring/monitor_path.rb', line 26 def stable_state @stable_state end |
#state ⇒ Object
Returns the value of attribute state.
27 28 29 |
# File 'lib/file_monitoring/monitor_path.rb', line 27 def state @state end |
Class Method Details
.set_log(log) ⇒ Object
Sets a log file to report changes
Arguments:
-
log
- already opened ruby File object
57 58 59 |
# File 'lib/file_monitoring/monitor_path.rb', line 57 def self.set_log (log) @@log = log end |
Instance Method Details
#==(other) ⇒ Object
Checks whether path and state are the same as of the argument
123 124 125 |
# File 'lib/file_monitoring/monitor_path.rb', line 123 def == (other) @path == other.path and @stable_state == other.stable_state end |
#changed?(file_stats) ⇒ Boolean
Checks that stored file attributes are the same as file attributes taken from file system.
97 98 99 100 |
# File 'lib/file_monitoring/monitor_path.rb', line 97 def changed?(file_stats) !((file_stats.size == @size) && (file_stats.mtime.to_i == @modification_time)) end |
#monitor ⇒ Object
Checks whether file was changed from the last iteration. For files, size and modification time are checked.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/file_monitoring/monitor_path.rb', line 63 def monitor file_stats = File.lstat(@path) rescue nil new_state = nil if file_stats.nil? new_state = FileStatEnum::NON_EXISTING @size = nil #@creation_time = nil @modification_time = nil @cycles = 0 elsif @size.nil? new_state = FileStatEnum::NEW @size = file_stats.size #@creation_time = file_stats.ctime.utc @modification_time = file_stats.mtime.to_i @cycles = 0 elsif changed?(file_stats) new_state = FileStatEnum::CHANGED @size = file_stats.size #@creation_time = file_stats.ctime.utc @modification_time = file_stats.mtime.to_i @cycles = 0 else new_state = FileStatEnum::UNCHANGED @cycles += 1 if @cycles >= @stable_state new_state = FileStatEnum::STABLE end end # The assignment set_state(new_state) end |
#set_event_queue(queue) ⇒ Object
102 103 104 |
# File 'lib/file_monitoring/monitor_path.rb', line 102 def set_event_queue(queue) @event_queue = queue end |
#set_output_queue(event_queue) ⇒ Object
49 50 51 |
# File 'lib/file_monitoring/monitor_path.rb', line 49 def set_output_queue(event_queue) @event_queue = event_queue end |
#set_state(new_state) ⇒ Object
Sets and writes to the log a new state.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/file_monitoring/monitor_path.rb', line 107 def set_state(new_state) if (@state != new_state or @state == FileStatEnum::CHANGED) @state = new_state if (@@log) @@log.info(state + ": " + path) @@log.outputters[0].flush if Params['log_flush_each_message'] end if @event_queue and FileStatEnum::NEW != @state # NEW state is ignored in indexer Log.debug1("Writing to event queue [%s, %s]", @state, @path) @event_queue.push([@state, self.instance_of?(DirStat), @path, @modification_time, @size]) $process_vars.set('monitor to index queue size', @event_queue.size) end end end |
#to_s(indent = 0) ⇒ Object
Returns path and state of the file with indentation
128 129 130 |
# File 'lib/file_monitoring/monitor_path.rb', line 128 def to_s (indent = 0) (" " * indent) + path.to_s + " : " + state.to_s end |