Class: Filbunke::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/filbunke/client.rb

Constant Summary collapse

UPDATES_ACTION =
'updates'
FILES_ACTION =
'files'
LAST_CHECKPOINT_ACTION =
'last_checkpoint'
TOUCH_ACTION =
'touch'
URL_KEY =
'url'
FROM_CHECKPOINT_KEY =
'from_checkpoint'
HASH_KEY =
'hash'
URI_UNSAFE_CHARACTERS =
'/[^.:\/\w-]/'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repository, logger, callbacks = [], failed_request_log_file_name = nil) ⇒ Client

Returns a new instance of Client.



16
17
18
19
20
21
22
23
24
# File 'lib/filbunke/client.rb', line 16

def initialize(repository, logger, callbacks = [], failed_request_log_file_name = nil)
  @repository = repository
  @logger = logger
  @callbacks = callbacks
  @failed_request_log_file_name = failed_request_log_file_name
  @hydra = Typhoeus::Hydra.new(:max_concurrency => @repository.hydra_concurrency)

  @logger.info "initialized client for repository '#{@repository.name}';  #{@repository.inspect}"
end

Instance Attribute Details

#repositoryObject (readonly)

Returns the value of attribute repository.



4
5
6
# File 'lib/filbunke/client.rb', line 4

def repository
  @repository
end

Instance Method Details

#last_checkpointObject



142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/filbunke/client.rb', line 142

def last_checkpoint
 last_checkpoint_http = Net::HTTP.new(@repository.host, @repository.port)
  last_checkpoint_http.start do |http|
    last_checkpoint_path = "/#{UPDATES_ACTION}/#{@repository.name}/#{LAST_CHECKPOINT_ACTION}"
    request = Net::HTTP::Get.new(last_checkpoint_path)
    response = http.request(request)
    if response.code.to_i != 200
      raise "Failed to get last checkpoint for repository: #{@repository.name}"
    end
    return response.body.chomp.to_i
  end
end

#register_deleted_file!(path) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/filbunke/client.rb', line 113

def register_deleted_file!(path)
  register_path = "/#{FILES_ACTION}/#{@repository.name}/#{path}"
  begin
    register_http = Net::HTTP.new(@repository.host, @repository.port)
    register_http.start do |http|
      request = Net::HTTP::Delete.new(register_path)
      response = http.request(request)
      if response.code.to_i != 204
        raise "Failed to register deleted file: #{path}"
      end
    end
  rescue Exception => e
    log_failed_request(%Q{curl -XDELETE "http://#{@repository.host}:#{@repository.port}#{register_path}"}, e)
    raise e
  end
end

#register_updated_file!(path, url, hash = nil) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/filbunke/client.rb', line 95

def register_updated_file!(path, url, hash = nil)
  register_path = "/#{FILES_ACTION}/#{@repository.name}/#{path}?#{URL_KEY}=#{url}"
  register_path += "&#{HASH_KEY}=#{hash}" if hash
  begin
    register_http = Net::HTTP.new(@repository.host, @repository.port)
    register_http.start do |http|
      request = Net::HTTP::Put.new(register_path)
      response = http.request(request)
      if response.code.to_i != 204
        raise "Failed to register updated file: #{path}"
      end
    end
  rescue Exception => e
    log_failed_request(%Q{curl -XPUT "http://#{@repository.host}:#{@repository.port}#{register_path}"}, e)
    raise e
  end
end

#touch_repository!Object



130
131
132
133
134
135
136
137
138
139
140
# File 'lib/filbunke/client.rb', line 130

def touch_repository!
  touch_http = Net::HTTP.new(@repository.host, @repository.port)
  touch_http.start do |http|
    touch_path = "/#{UPDATES_ACTION}/#{@repository.name}/#{TOUCH_ACTION}"
    request = Net::HTTP::Put.new(touch_path)
    response = http.request(request)
    if response.code.to_i != 204
      raise "Failed to touch repository: #{@repository.name}"
    end
  end
end

#update_files!(last_checkpoint) ⇒ Object



91
92
93
# File 'lib/filbunke/client.rb', line 91

def update_files!(last_checkpoint)
  with_updated_files(last_checkpoint) {}
end

#with_updated_files(last_checkpoint) ⇒ Object



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
# File 'lib/filbunke/client.rb', line 26

def with_updated_files(last_checkpoint)
  updates = get_updated_file_list(last_checkpoint)
  updated_files = updates["files"] || []
  failure = false
  
  new_checkpoint = updates["checkpoint"]
  
  @logger.info "Updating repository: #{@repository.name}: #{updated_files.size} files. Checkpoint: #{last_checkpoint} ==> #{new_checkpoint}" if updated_files.size > 0

  @async_requests = []
  
  callbacks_on_update = []
  callbacks_on_no_change = []
  callbacks_on_delete = []

  updated_files.each do |raw_file|
    file = File.new(raw_file)
    local_file_path = ::File.join(@repository.local_path, file.path)
    if file.state == "DELETED" then
      delete_file!(local_file_path)
      callbacks_on_delete << OpenStruct.new({ :file => file, :local_file_path => local_file_path })
    else
      if file_needs_update?(file, local_file_path)
        if update_file!(file, local_file_path) then
          yield file
          callbacks_on_update << OpenStruct.new({ :file => file, :local_file_path => local_file_path })
        else
          @logger.error "Unable to get file #{file.url} ==> #{file.path}!"
          failure = true
        end
      else
        @logger.debug "File exists with correct hash: #{local_file_path}"
        callbacks_on_no_change << OpenStruct.new({:file => file, :local_file_path => local_file_path})
      end
    end
  end
  @hydra.run

  pfailure = failure || @async_requests.any? do |request| 
    @logger.warn "request did not handle response: #{request.inspect}" if request.response.nil? || request.response.code != 200
    request.response.nil? || request.response.code != 200
  end

  if pfailure == false
    @logger.info "Done fetching files for #{@repository.name}, processing callbacks..."
    begin
      
      run_callbacks_delete(callbacks_on_delete)
      
      run_callbacks(callbacks_on_update)
      
      run_callbacks_no_change(callbacks_on_no_change)

      new_checkpoint || last_checkpoint
    rescue RuntimeError, SystemCallError, StandardError => e
      msg = ["Callbacks failed to run; #{e.class} - #{e.message}", *e.backtrace].join("\n\t")
      @logger.error "FAILED to update files for #{@repository.name}  last_checkpoint = #{last_checkpoint}; #{msg}"
      last_checkpoint
    end
  else
    @logger.error "FAILED to update files for #{@repository.name}  last_checkpoint = #{last_checkpoint}"
    last_checkpoint
  end
end