Module: Watcher

Extended by:
Fallen
Defined in:
lib/filer/watcher.rb

Constant Summary collapse

FILER_LOG =
"filer_watcher.log"

Class Method Summary collapse

Class Method Details

.instance(settings) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/filer/watcher.rb', line 10

def self.instance(settings)
  @settings = settings
  Watcher.pid_file 'filer_watcher.pid'
  Watcher.stderr FILER_LOG
  self.s3
  self
end

.notify(msg) ⇒ Object



27
28
29
30
31
# File 'lib/filer/watcher.rb', line 27

def self.notify(msg)
  Notifier.notify(
    title: "Filer",
    message: msg)
end

.runObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/filer/watcher.rb', line 33

def self.run
  while running?
    begin
      files = @settings[:directories].flat_map {|d| Dir["#{d}/*"]}
      files.each do |f|
        @s3.put_file(f)
        fd = Filer::Filed.new(
          key: @s3.s3_key(f), 
          attachment: Base64.encode64(File.read(f)))
        fd.save
        File.delete(f)
      end
      self.notify("Processed #{files.size} files") unless files.empty?
      sleep 20
    rescue Exception => e
      self.notify("Filer encountered an error. " <<
        "See #{File.dirname(__FILE__)}/#{FILER_LOG}")
    end
  end
end

.s3Object



18
19
20
21
22
23
24
25
# File 'lib/filer/watcher.rb', line 18

def self.s3
  params = [:s3_key, :s3_secret, :s3_bucket].map {|m| @settings[m]}
  unless params.compact.size == 3
    puts "Please run filer configure-s3 first"
    exit
  end
  @s3 ||= Filer::S3.new(*params)
end