Class: IndexStateS3

Inherits:
Object
  • Object
show all
Defined in:
indexer/app/lib/index_state_s3.rb

Instance Method Summary collapse

Constructor Details

#initialize(state_dir = 'indexer_state') ⇒ IndexStateS3

Returns a new instance of IndexStateS3.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'indexer/app/lib/index_state_s3.rb', line 6

def initialize(state_dir = 'indexer_state')
  Excon.defaults[:ciphers] = 'DEFAULT'
  @connection = Fog::Storage.new({
    :provider                 => 'AWS',
    :region                   => AppConfig[:index_state_s3][:region],
    :aws_access_key_id        => AppConfig[:index_state_s3][:aws_access_key_id],
    :aws_secret_access_key    => AppConfig[:index_state_s3][:aws_secret_access_key],
  })
  prefix = AppConfig[:index_state_s3][:prefix].call

  @bucket    = @connection.directories.get(AppConfig[:index_state_s3][:bucket])
  @state_dir = "#{prefix}#{state_dir}/"
  create_state_dir
end

Instance Method Details

#create_state_dirObject



21
22
23
24
25
26
27
28
29
# File 'indexer/app/lib/index_state_s3.rb', line 21

def create_state_dir
  sd = @bucket.files.get(@state_dir)
  unless sd
    @bucket.files.create(
      key: @state_dir,
      body: nil
    )
  end
end

#get_last_mtime(repository_id, record_type) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'indexer/app/lib/index_state_s3.rb', line 48

def get_last_mtime(repository_id, record_type)
  file = @bucket.files.get(path_for(repository_id, record_type))
  if file
    file.body.to_i
  else
    # If we've never run against this repository_id/type before, just index
    # everything.
    0
  end
end

#path_for(repository_id, record_type) ⇒ Object



31
32
33
# File 'indexer/app/lib/index_state_s3.rb', line 31

def path_for(repository_id, record_type)
  "#{@state_dir}#{repository_id}_#{record_type}"
end

#set_last_mtime(repository_id, record_type, time) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'indexer/app/lib/index_state_s3.rb', line 35

def set_last_mtime(repository_id, record_type, time)
  file = @bucket.files.get(path_for(repository_id, record_type))
  if file
    file.body = StringIO.new("#{time.to_i.to_s}")
    file.save
  else
    @bucket.files.create(
      :key  => path_for(repository_id, record_type),
      :body => StringIO.new("#{time.to_i.to_s}"),
    )
  end
end