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'

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.



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

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



153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/filbunke/client.rb', line 153

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



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/filbunke/client.rb', line 124

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



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/filbunke/client.rb', line 106

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



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

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



102
103
104
# File 'lib/filbunke/client.rb', line 102

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

#with_updated_files(last_checkpoint) ⇒ Object



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
96
97
98
99
100
# File 'lib/filbunke/client.rb', line 25

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

  @async_requests = []
  callbacks_on_update = []
  callbacks_on_no_change = []
  callbacks_on_delete = []
  has_update_file_failure = false
  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 fetch file #{file.url} ==> #{file.path}!"
          has_update_file_failure = true
          break;
        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

  if has_update_file_failure
    @logger.error "FAILED to fetch files for #{@repository.name}  last_checkpoint = #{last_checkpoint}"
    return last_checkpoint
  end
  @logger.info "Done setting up async requests for #{@repository.name}, starting fetch..."

  has_fetch_files_failure = begin
    @hydra.run
    @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
  rescue RuntimeError, SystemCallError, StandardError => e
    msg = ["#{e.class} - #{e.message}", *e.backtrace].join("\n\t")
    @logger.error "FAILED to fetch files for #{@repository.name}  last_checkpoint = #{last_checkpoint}; #{msg}"
    true
  end

  if has_fetch_files_failure
    @logger.error "FAILED to update files for #{@repository.name}  last_checkpoint = #{last_checkpoint}"
    return last_checkpoint
  end

  @logger.info "Done fetching files for #{@repository.name}, processing callbacks..."
  new_or_last_checkpoint = 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 = ["#{e.class} - #{e.message}", *e.backtrace].join("\n\t")
    @logger.error "FAILED to process callbacks for #{@repository.name}  last_checkpoint = #{last_checkpoint}; #{msg}"
    last_checkpoint
  end

  new_or_last_checkpoint
end