Skip to content

Commit dd427fd

Browse files
authored
Merge pull request #85 from amatsuda/from_s3
New S3 importer
2 parents 2ddef08 + 4f69cbd commit dd427fd

File tree

5 files changed

+28
-45
lines changed

5 files changed

+28
-45
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ From `heroku run bash`
77
```
88
% heroku run bash
99
Running bash on ⬢ blade-ruby-lang... up, run.7782
10-
~ $ ./bin/rails runner import.rb --list ruby-list --from 1001 --to 2000
10+
~ $ ./bin/rails runner bin/import_mails --list ruby-list --from 1001 --to 2000
1111
```

app/models/message.rb

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
BLADE_BUCKET_REGION = 'ap-northeast-1'
2-
BLADE_BUCKET_NAME = 'blade.ruby-lang.org'
2+
BLADE_BUCKET_NAME = 'blade-data-vault'
33

44
require 'kconv'
55

@@ -91,13 +91,10 @@ def from_mail(mail, list, list_seq)
9191
end
9292

9393
class << self
94-
def from_s3(list_name, list_seq, s3_client = Aws::S3::Client.new(region: BLADE_BUCKET_REGION))
95-
obj = s3_client.get_object(bucket: BLADE_BUCKET_NAME, key: "#{list_name}/#{list_seq}")
96-
97-
m = self.from_string(obj.body.read)
98-
m.list_id = List.find_by_name(list_name).id
99-
m.list_seq = list_seq
100-
m
94+
def from_s3(list, list_seq, s3_client = Aws::S3::Client.new(region: BLADE_BUCKET_REGION))
95+
obj = s3_client.get_object(bucket: BLADE_BUCKET_NAME, key: "#{list.name}/#{list_seq}")
96+
mail = Mail.read_from_string obj.body.read.force_encoding(Encoding::BINARY)
97+
Message.from_mail mail, list, list_seq
10198
end
10299

103100
def from_string(str)
@@ -140,7 +137,7 @@ def count_recursively(count = 0)
140137
end
141138

142139
def reload_from_s3(s3_client = Aws::S3::Client.new(region: BLADE_BUCKET_REGION))
143-
m = Message.from_s3(List.find(self.list_id).name, self.list_seq, s3_client)
140+
m = Message.from_s3(List.find(self.list_id), self.list_seq, s3_client)
144141

145142
self.body = m.body
146143
self.subject = m.subject

bin/import_mails

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ BASE_DIR = Rails.root.join('tmp')
88

99
params = {}
1010
OptionParser.new do |opts|
11+
opts.on('--local')
1112
opts.on('--list LIST')
1213
opts.on('--from FROM', Integer)
1314
opts.on('--to TO', Integer)
@@ -22,17 +23,27 @@ Rails.logger.level = Logger::INFO
2223
Message.transaction do
2324
(params[:from]..params[:to]).each do |seq|
2425
begin
25-
filepath = BASE_DIR.join(list.name, seq.to_s)
26-
next unless filepath.exist?
27-
28-
str = File.binread filepath
29-
next if str.blank?
30-
31-
mail = Mail.read_from_string str
32-
message = Message.from_mail mail, list, seq
26+
if params[:local]
27+
filepath = BASE_DIR.join(list.name, seq.to_s)
28+
raise "No #{seq.to_s}" unless filepath.exist?
29+
next
30+
next unless filepath.exist?
31+
32+
str = File.binread filepath
33+
next if str.blank?
34+
35+
mail = Mail.read_from_string str
36+
message = Message.from_mail mail, list, seq
37+
else
38+
message = Message.from_s3(list, seq)
39+
end
40+
41+
p seq if seq % 10 == 0
3342
message.save!
3443
rescue ActiveRecord::RecordNotUnique
35-
STDERR.puts("#{list}:#{seq} already exists in Postgres")
44+
STDERR.puts("#{list.name}:#{seq} already exists in Postgres")
45+
rescue Aws::S3::Errors::NoSuchKey
46+
STDERR.puts("#{list.name}:#{seq} doesn't exist in S3")
3647
rescue StandardError => e
3748
errors << [seq, e]
3849
STDERR.puts("failed to import #{list.name}:#{seq}: #{e}")

import.rb

Lines changed: 0 additions & 25 deletions
This file was deleted.

test/models/message_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class MessageTest < ActiveSupport::TestCase
3838
3939
Hello, world!
4040
END_OF_BODY
41-
Message.from_s3('ruby-list', 1234, s3_client)
41+
Message.from_s3(List.find_by_name('ruby-list'), 1234, s3_client)
4242
end
4343

4444
test 'reload_from_s3' do

0 commit comments

Comments
 (0)