Class: SpsFileSync

Inherits:
SPSSub
  • Object
show all
Defined in:
lib/sps_filesync.rb

Instance Method Summary collapse

Constructor Details

#initialize(nodes = [], port: '59000', host: nil, log: nil, debug: false) ⇒ SpsFileSync

Returns a new instance of SpsFileSync.



12
13
14
15
16
17
18
19
# File 'lib/sps_filesync.rb', line 12

def initialize(nodes=[], port: '59000', host: nil, log: nil, debug: false)
  
  raise 'SpsFileSync::initialize nodes.empty' if nodes.empty?
  
  @nodes, @debug = nodes, debug
  super(port: port, host: host, log: log)
  
end

Instance Method Details

#subscribe(topic: 'file/*') ⇒ Object



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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/sps_filesync.rb', line 21

def subscribe(topic: 'file/*')
  
  super(topic: topic) do |msg, topic|
    
    
    if @debug then
      puts 'topic: ' + topic.inspect
      puts 'msg: ' + msg.inspect
    end
    
    action = topic.split('/').last.to_sym

    @master_address , path = msg.match(/^dfs:\/\/([^\/]+)(.*)/).captures

    case action
    when :cp

      src, dest = msg.split(/ +/,2)

      file_op do |f, node|
        src_path = "dfs://%s%s" % [node, src[/^dfs:\/\/[^\/]+(.*)/]]
        target_path = "dfs://%s%s" % [node, dest[/^dfs:\/\/[^\/]+(.*)/]]
        f.cp src_path, target_path
      end
      
    when :mkdir
      
      file_op {|f, node| f.mkdir "dfs://%s%s" % [node, path] }

    when :mkdir_p
      
      file_op {|f, node| f.mkdir_p "dfs://%s%s" % [node, path] }            
      
    when :mv

      src, dest = msg.split(/ +/,2)

      file_op do |f, node|
        src_path = "dfs://%s/%s" % [node, src[/^dfs:\/\/[^\/]+(.*)/]]
        target_path = "dfs://%s/%s" % [node, dest[/^dfs:\/\/[^\/]+(.*)/]]
        f.mv src_path, target_path
      end                  
            
    when :write

      master_path = msg

      file_op do |f, node|
        target_path = "dfs://%s%s" % [node, path]
        
        if @debug then
          puts 'master_path: ' + master_path.inspect
          puts 'target_path: ' + target_path.inspect
        end
                  
        DfsFile.cp master_path, target_path

      end
      
    when :rm
      
      file_op {|f, node| f.rm "dfs://%s%s" % [node, path] }      
    
    when :zip

      master_path = msg

      file_op do |f, node|
        target_path = "dfs://%s%s" % [node, path]
        f.cp master_path, target_path
      end 
      
    end
  end
end