Class: RServiceBus::Monitor_DirNotifier

Inherits:
Monitor
  • Object
show all
Defined in:
lib/rservicebus/Monitor/DirNotifier.rb

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 RServiceBus::Monitor

Instance Attribute Details

#FilterObject (readonly)

Returns the value of attribute Filter.



7
8
9
# File 'lib/rservicebus/Monitor/DirNotifier.rb', line 7

def Filter
  @Filter
end

#PathObject (readonly)

Returns the value of attribute Path.



7
8
9
# File 'lib/rservicebus/Monitor/DirNotifier.rb', line 7

def Path
  @Path
end

#ProcessingFolderObject (readonly)

Returns the value of attribute ProcessingFolder.



7
8
9
# File 'lib/rservicebus/Monitor/DirNotifier.rb', line 7

def ProcessingFolder
  @ProcessingFolder
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
# File 'lib/rservicebus/Monitor/DirNotifier.rb', line 9

def connect(uri)
    #Pass the path through the Dir object to check syntax on startup
    begin
        self.open_folder uri.path
        unless self.file_writable?(uri.path) then
          puts "***** Directory is not writable, #{uri.path}."
          puts "***** Make the directory, #{uri.path}, writable and try again."
          abort()
        end
        rescue Errno::ENOENT => e
            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 => e
        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.has_key? 'processing' then
            processingUri = URI.parse parts['processing'][0]
            begin
                self.open_folder processingUri.path
                unless self.file_writable?(processingUri.path) then
                  puts "***** Processing Directory is not writable, #{processingUri.path}."
                  puts "***** Make the directory, #{processingUri.path}, writable and try again."
                  abort()
                end
                rescue Errno::ENOENT => e
                    puts "***** Processing Directory does not exist, #{processingUri.path}."
                    puts "***** Create the directory, #{processingUri.path}, and try again."
                    puts "***** eg, mkdir #{processingUri.path}"
                    abort()
                rescue Errno::ENOTDIR => e
                    puts "***** Processing Directory does not point to a directory, #{processingUri.path}."
                    puts "***** Either repoint path to a directory, or remove, #{processingUri.path}, and create it as a directory."
                    puts "***** eg, rm #{processingUri.path} && mkdir #{processingUri.path}"
                    abort()
            end

            @ProcessingFolder = processingUri.path
        end

        @Filter = '*'
        if parts.has_key? 'filter' then
            @Filter = parts['filter'][0]
        end
    end
end

#file_writable?(path) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/rservicebus/Monitor/DirNotifier.rb', line 79

def file_writable? path
    return File.writable? path
end

#get_filesObject



93
94
95
# File 'lib/rservicebus/Monitor/DirNotifier.rb', line 93

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

#LookObject



71
72
73
74
75
76
77
# File 'lib/rservicebus/Monitor/DirNotifier.rb', line 71

def Look
    fileList = self.get_files
    fileList.each do |filePath|
        newPath = self.move_file(filePath, @ProcessingFolder)
        self.send( nil, URI.parse( "file://#{newPath}" ) )
    end
end

#move_file(src, dest) ⇒ Object



87
88
89
90
91
# File 'lib/rservicebus/Monitor/DirNotifier.rb', line 87

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

#open_folder(path) ⇒ Object



83
84
85
# File 'lib/rservicebus/Monitor/DirNotifier.rb', line 83

def open_folder path
    Dir.new path
end