Module: MailRunner::ArchivistBot
- Defined in:
- lib/mail_runner/archive_manager.rb
Class Method Summary collapse
- .add_to_archive_stack(msg, archive) ⇒ Object
- .archive_stack ⇒ Object
- .deliver_to_cloud_archive(msg, archive_opts) ⇒ Object
- .deliver_to_local_archive(msg, archive_opts) ⇒ Object
- .establish_archive_link(archive_opts) ⇒ Object
- .format_options(archive_opts) ⇒ Object
- .name_msg(msg) ⇒ Object
- .new_storage_object(archive_opts) ⇒ Object
- .pop_from_stack ⇒ Object
- .stack_height ⇒ Object
Class Method Details
.add_to_archive_stack(msg, archive) ⇒ Object
4 5 6 7 8 |
# File 'lib/mail_runner/archive_manager.rb', line 4 def self.add_to_archive_stack(msg, archive) que_packet = [msg, archive] $redis.lpush("archive_stack", que_packet.to_json) $logger.info("Archivist") { "#add_to_archive_stack:: email added to queue for processing later." } end |
.archive_stack ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/mail_runner/archive_manager.rb', line 14 def self.archive_stack $logger.info("Archivist") { "Stack Height:: #{stack_height}"} while stack_height > 0 $logger.info("Archivist") { "#archive_stack:: Processing stack item:: #{stack_height}"} msg, archive = pop_from_stack if archive["destination"] == 'local' deliver_to_local_archive(msg, archive) else deliver_to_cloud_archive(msg, archive) end end end |
.deliver_to_cloud_archive(msg, archive_opts) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/mail_runner/archive_manager.rb', line 50 def self.deliver_to_cloud_archive(msg, archive_opts) attempts = 0 begin archive = establish_archive_link(archive_opts) filename = name_msg(msg) mimetype = "application/json" saved = archive.files.create :key => filename, :body => msg, :Content_type => mimetype $logger.info("Archivist") { "#Message archived to cloud"} return saved #explicit for testing rescue => e $logger.error("Archivist") { "#deliver_to_cloud_archive:: attempt #{attempts} failed: #{e.inspect}"} attempts += 1 retry unless attempts > 1 unless saved $logger.info("Archivist") { "Too many failed attempts, restacking msg."} add_to_archive_stack(msg,archive_opts) end end end |
.deliver_to_local_archive(msg, archive_opts) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/mail_runner/archive_manager.rb', line 37 def self.deliver_to_local_archive(msg, archive_opts) begin filename = name_msg(msg) target = File.open("#{archive_opts["local_archive"]}/#{filename}", 'w') target.write(msg) target.close() $logger.info("Archivist") { "#Message archived to local"} rescue => e $logger.error("Archivist") { "#deliver_to_local_archive:: failed: #{e.inspect}"} add_to_archive_stack(msg,archive_opts) end end |
.establish_archive_link(archive_opts) ⇒ Object
76 77 78 79 80 81 |
# File 'lib/mail_runner/archive_manager.rb', line 76 def self.establish_archive_link(archive_opts) service = new_storage_object(archive_opts) dir_name = archive_opts["directory"] archive_directory = service.directories.new :key => dir_name return archive_directory end |
.format_options(archive_opts) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/mail_runner/archive_manager.rb', line 88 def self.(archive_opts) provider = archive_opts["provider"].downcase = { :provider => archive_opts["provider"] } if provider == "rackspace" [:"#{provider}_username"] = archive_opts["username"] [:"#{provider}_api_key"] = archive_opts["api_key"] [:"#{provider}_region"] = archive_opts["region"] elsif provider == "aws" [:"#{provider}_access_key_id"] = archive_opts["api_key"] [:"#{provider}_secret_access_key"] = archive_opts["secret_key"] end return end |
.name_msg(msg) ⇒ Object
71 72 73 74 |
# File 'lib/mail_runner/archive_manager.rb', line 71 def self.name_msg(msg) msg_id = JSON.parse(msg)[0]["msg"]["headers"]["Message-ID"] return "#{msg_id.gsub(/[#<$+%>!&*?=\/:@]/,'_')}.json"#clean illegal charaters end |
.new_storage_object(archive_opts) ⇒ Object
83 84 85 86 |
# File 'lib/mail_runner/archive_manager.rb', line 83 def self.new_storage_object(archive_opts) = (archive_opts) Fog::Storage.new() end |
.pop_from_stack ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/mail_runner/archive_manager.rb', line 28 def self.pop_from_stack key, que_packet = $redis.blpop("archive_stack", :timeout => 5) #timeout needed for MockRedis in Testing Env. $logger.info("Archivist") { "#item popped from stack for processing"} data = JSON::parse(que_packet) msg = data[0] archive = data[1] return msg, archive end |
.stack_height ⇒ Object
10 11 12 |
# File 'lib/mail_runner/archive_manager.rb', line 10 def self.stack_height return $redis.llen("archive_stack") end |