Class: RServiceBus2::MonitorDirNotifier

Inherits:
Monitor
  • Object
show all
Defined in:
lib/rservicebus2/monitor/dirnotifier.rb

Overview

Monitor for Directory

Instance Attribute Summary collapse

Attributes inherited from Monitor

#bus

Instance Method Summary collapse

Methods inherited from Monitor

#_connect, #finished, #initialize, #reconnect, #send

Constructor Details

This class inherits a constructor from RServiceBus2::Monitor

Instance Attribute Details

#filterObject (readonly)

Returns the value of attribute filter.



8
9
10
# File 'lib/rservicebus2/monitor/dirnotifier.rb', line 8

def filter
  @filter
end

#pathObject (readonly)

Returns the value of attribute path.



8
9
10
# File 'lib/rservicebus2/monitor/dirnotifier.rb', line 8

def path
  @path
end

#processing_folderObject (readonly)

Returns the value of attribute processing_folder.



8
9
10
# File 'lib/rservicebus2/monitor/dirnotifier.rb', line 8

def processing_folder
  @processing_folder
end

Instance Method Details

#connect(uri) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
# File 'lib/rservicebus2/monitor/dirnotifier.rb', line 9

def connect(uri)
  # Pass the path through the Dir object to check syntax on startup
  begin
    open_folder uri.path
    unless File.writable?(uri.path)
      puts "***** Directory is not writable, #{uri.path}."
      puts "***** Make the directory, #{uri.path}, writable and try again."
      abort
    end
  rescue Errno::ENOENT
    puts "***** Directory does not exist, #{uri.path}."
    puts "***** Create the directory, #{uri.path}, and try again."
    puts "***** eg, mkdir #{uri.path}"
    abort
  rescue Errno::ENOTDIR
    puts "***** The specified path does not point to a directory,
          #{uri.path}."
    puts "***** Either repoint path to a directory, or remove, #{uri.path},
          and create it as a directory."
    puts "***** eg, rm #{uri.path} && mkdir #{uri.path}"
    abort
  end

  @path = uri.path

  if uri.query.nil?
    puts '***** Processing Directory is not specified.'
    puts '***** Specify the Processing Directory as a query string in the
          Path URI'
    puts "***** eg, '/#{uri.path}?processing=*ProcessingDir*"
    abort
  else
    parts = CGI.parse(uri.query)

    if parts.key? 'processing'
      processing_uri = URI.parse parts['processing'][0]
      begin
        open_folder processing_uri.path
        unless File.writable?(processing_uri.path)
          puts "***** Processing Directory is not writable,
                #{processing_uri.path}."
          puts "***** Make the directory, #{processing_uri.path},
                writable and try again."
          abort
        end
      rescue Errno::ENOENT
        puts "***** Processing Directory does not exist,
              #{processing_uri.path}."
        puts "***** Create the directory, #{processing_uri.path}, and try
              again."
        puts "***** eg, mkdir #{processing_uri.path}"
        abort
      rescue Errno::ENOTDIR
        puts "***** Processing Directory does not point to a directory,
              #{processing_uri.path}."
        puts "***** Either repoint path to a directory, or remove,
              #{processing_uri.path}, and create it as a directory."
        puts "***** eg, rm #{processing_uri.path} && mkdir
              #{processing_uri.path}"
        abort
      end

      @processing_folder = processing_uri.path
    end

    @filter = '*'
    @filter = parts['filter'][0] if parts.key? 'filter'
  end
end

#get_filesObject



97
98
99
# File 'lib/rservicebus2/monitor/dirnotifier.rb', line 97

def get_files
  Dir.glob(Pathname.new("#{@Path}").join(@Filter) ).select { |f| File.file?(f) }
end

#lookObject



79
80
81
82
83
84
85
# File 'lib/rservicebus2/monitor/dirnotifier.rb', line 79

def look
  file_list = get_files
  file_list.each do |file_path|
    new_path = move_file(file_path, @processing_folder)
    send(nil, URI.parse("file://#{new_path}"))
  end
end

#move_file(src, dest) ⇒ Object



91
92
93
94
95
# File 'lib/rservicebus2/monitor/dirnotifier.rb', line 91

def move_file(src, dest)
  FileUtils.mv(src, dest)
  filename = Pathname.new(src).basename
  Pathname.new(dest).join(filename)
end

#open_folder(path) ⇒ Object



87
88
89
# File 'lib/rservicebus2/monitor/dirnotifier.rb', line 87

def open_folder(path)
  Dir.new path
end