Class: Librr::DirMonitor

Inherits:
Object
  • Object
show all
Includes:
Logger::ClassLogger
Defined in:
lib/librr/dir_monitor.rb

Defined Under Namespace

Classes: DirWatcher

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logger::ClassLogger

#debug, #info, #logger

Instance Attribute Details

#dirsObject

Returns the value of attribute dirs.



12
13
14
# File 'lib/librr/dir_monitor.rb', line 12

def dirs
  @dirs
end

#indexerObject

Returns the value of attribute indexer.



12
13
14
# File 'lib/librr/dir_monitor.rb', line 12

def indexer
  @indexer
end

Class Method Details

.pid_fileObject



74
75
76
# File 'lib/librr/dir_monitor.rb', line 74

def self.pid_file
  Settings.in_dir('dir_watcher.pid')
end

Instance Method Details

#add_directory(dir) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/librr/dir_monitor.rb', line 31

def add_directory(dir)
  self.debug "add dir: #{dir}"
  @indexer.index_directory(dir)
  self.dirs.add(dir)
  Configer.save_dir_config(self.dirs)
  self.debug "save dir: #{self.dirs.to_a.to_s}"
  self.start
end

#after_process_stopObject



52
53
54
55
# File 'lib/librr/dir_monitor.rb', line 52

def after_process_stop
  self.start_process if @new_start
  @new_start = false
end

#init(opts) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/librr/dir_monitor.rb', line 14

def init opts
  @pipe = nil
  @new_start = false
  @indexer = opts[:indexer]

  self.dirs = Configer.load_dir_config
  self.debug "init dirs: #{self.dirs.to_a.to_s}"
end

#post_initObject



48
49
50
# File 'lib/librr/dir_monitor.rb', line 48

def post_init
  @after_block.call if @after_block
end

#reindexObject



23
24
25
26
27
28
29
# File 'lib/librr/dir_monitor.rb', line 23

def reindex
  self.debug "reindex"
  @indexer.cleanup
  self.dirs.each do |dir|
    @indexer.index_directory(dir)
  end
end

#remove_directory(dir) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/librr/dir_monitor.rb', line 40

def remove_directory(dir)
  self.debug "remove dir: #{dir}"
  @indexer.remove_index_directory(dir)
  self.dirs.delete(dir)
  Configer.save_dir_config(self.dirs)
  self.start
end

#start(&after_block) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/librr/dir_monitor.rb', line 57

def start &after_block
  @after_block = after_block

  if self.dirs.empty?
    self.debug "DIR empty, not start process."
    self.post_init
    return
  end

  if @pipe
    @new_start = true
    @pipe.close_connection
  else
    self.start_process
  end
end

#start_processObject



78
79
80
81
82
83
84
85
# File 'lib/librr/dir_monitor.rb', line 78

def start_process
  kill_process_by_file(self.class.pid_file)

  cmd = [FSEvent.watcher_path] + ["--file-events"] + self.dirs.to_a
  self.debug "start process: #{cmd}"
  @pipe = EM.popen(cmd, DirWatcher, self)
  # TODO: write pid file
end