Class: ConradFiler::WatchJob

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

Overview

A watch job describes directories to monitor and includes a bitmask describing which actions to monitor

Instance Method Summary collapse

Constructor Details

#initialize(db, a_hash) ⇒ WatchJob

Returns a new instance of WatchJob.



9
10
11
12
13
14
# File 'lib/conrad_filer/watch_job.rb', line 9

def initialize(db, a_hash)
  @db_rec = a_hash
  @bitmask = Inotify::InotifyBitmask.new(@db_rec['BITMASK'])
  @rules = Hash.new {Array.new}
  @inotify_thread = Inotify::InotifyThread.new
end

Instance Method Details

#add_rule(id_type, rule) ⇒ Object



20
21
22
# File 'lib/conrad_filer/watch_job.rb', line 20

def add_rule(id_type, rule)
  @rules[id_type] = @rules[id_type] << rule
end

#bitmaskObject



32
33
34
# File 'lib/conrad_filer/watch_job.rb', line 32

def bitmask
  @bitmask.bitmask
end

#id_typesObject



16
17
18
# File 'lib/conrad_filer/watch_job.rb', line 16

def id_types
  @rules.keys
end

#is_valid?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/conrad_filer/watch_job.rb', line 36

def is_valid?
  @rules.size > 0
end

#pathnameObject



28
29
30
# File 'lib/conrad_filer/watch_job.rb', line 28

def pathname
  @db_rec['PATHNAME']
end

#prepare_inotify_threadObject



40
41
42
43
# File 'lib/conrad_filer/watch_job.rb', line 40

def prepare_inotify_thread
  @inotify_thread.add_watch(self.pathname, self.bitmask, self.recursive?)
  @inotify_thread.register_event(self.bitmask, &(self.method(:process_event)))
end

#process_event(notify_event, path) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/conrad_filer/watch_job.rb', line 55

def process_event(notify_event, path)
  bitmask = Inotify::InotifyBitmask.new(notify_event.mask)
  puts "[ConradWatchJob::process_event] '#{path}/#{notify_event.name}' #{bitmask.as_array_of_symbols}"
  @rules.each_pair do |id_type, rules_for_id_type|
    if id_type.matches_pathname?(File.join(path,notify_event.name.to_s))
      rules_for_id_type.each do |rule|
        rule.on_watch_event(self, id_type, notify_event, path)
      end
    end
  end
end

#recursive?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/conrad_filer/watch_job.rb', line 24

def recursive?
  @db_rec['RECURSIVE'][0,1].upcase == "T"
end

#start_inotify_loopObject



50
51
52
53
# File 'lib/conrad_filer/watch_job.rb', line 50

def start_inotify_loop
  self.prepare_inotify_thread
  @inotify_thread.start_loop
end

#start_inotify_threadObject



45
46
47
48
# File 'lib/conrad_filer/watch_job.rb', line 45

def start_inotify_thread
  self.prepare_inotify_thread
  @inotify_thread.start_thread
end