Class: Imap::Backup::Account::Connection
- Inherits:
-
Object
- Object
- Imap::Backup::Account::Connection
- Includes:
- RetryOnError
- Defined in:
- lib/imap/backup/account/connection.rb
Constant Summary collapse
- LOGIN_RETRY_CLASSES =
[EOFError, Errno::ECONNRESET, SocketError].freeze
Instance Attribute Summary collapse
-
#account ⇒ Object
readonly
Returns the value of attribute account.
Instance Method Summary collapse
- #backup_folders ⇒ Object
- #client ⇒ Object
- #disconnect ⇒ Object
- #folder_names ⇒ Object
-
#initialize(account) ⇒ Connection
constructor
A new instance of Connection.
- #local_folders ⇒ Object
- #reconnect ⇒ Object
- #reset ⇒ Object
- #restore ⇒ Object
- #run_backup ⇒ Object
- #server ⇒ Object
- #status ⇒ Object
Methods included from RetryOnError
Constructor Details
#initialize(account) ⇒ Connection
Returns a new instance of Connection.
16 17 18 19 |
# File 'lib/imap/backup/account/connection.rb', line 16 def initialize(account) @account = account reset end |
Instance Attribute Details
#account ⇒ Object (readonly)
Returns the value of attribute account.
14 15 16 |
# File 'lib/imap/backup/account/connection.rb', line 14 def account @account end |
Instance Method Details
#backup_folders ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/imap/backup/account/connection.rb', line 36 def backup_folders @backup_folders ||= begin names = if account.folders&.any? account.folders.map { |af| af[:name] } else folder_names end names.map do |name| Account::Folder.new(self, name) end end end |
#client ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/imap/backup/account/connection.rb', line 119 def client @client ||= retry_on_error(errors: LOGIN_RETRY_CLASSES) do = Imap::Backup::Logger.logger.debug( "Creating IMAP instance: #{server}, options: #{.inspect}" ) client = if provider.is_a?(Email::Provider::AppleMail) Client::AppleMail.new(server, ) else Client::Default.new(server, ) end Imap::Backup::Logger.logger.debug "Logging in: #{account.username}/#{masked_password}" client.login(account.username, account.password) Imap::Backup::Logger.logger.debug "Login complete" client end end |
#disconnect ⇒ Object
101 102 103 104 |
# File 'lib/imap/backup/account/connection.rb', line 101 def disconnect client.disconnect if @client reset end |
#folder_names ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/imap/backup/account/connection.rb', line 21 def folder_names @folder_names ||= begin folder_names = client.list if folder_names.empty? = "Unable to get folder list for account #{account.username}" Imap::Backup::Logger.logger.info raise end folder_names end end |
#local_folders ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/imap/backup/account/connection.rb', line 81 def local_folders return enum_for(:local_folders) if !block_given? ensure_account_folder glob = File.join(account.local_path, "**", "*.imap") base = Pathname.new(account.local_path) Pathname.glob(glob) do |path| name = path.relative_path_from(base).to_s[0..-6] serializer = Serializer::Mbox.new(account.local_path, name) folder = Account::Folder.new(self, name) yield serializer, folder end end |
#reconnect ⇒ Object
106 107 108 |
# File 'lib/imap/backup/account/connection.rb', line 106 def reconnect disconnect end |
#reset ⇒ Object
110 111 112 113 114 115 116 117 |
# File 'lib/imap/backup/account/connection.rb', line 110 def reset @backup_folders = nil @client = nil @config = nil @folder_names = nil @provider = nil @server = nil end |
#restore ⇒ Object
95 96 97 98 99 |
# File 'lib/imap/backup/account/connection.rb', line 95 def restore local_folders do |serializer, folder| restore_folder serializer, folder end end |
#run_backup ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/imap/backup/account/connection.rb', line 60 def run_backup Imap::Backup::Logger.logger.debug "Running backup of account: #{account.username}" # start the connection so we get logging messages in the right order client ensure_account_folder each_folder do |folder, serializer| next if !folder.exist? Imap::Backup::Logger.logger.debug "[#{folder.name}] running backup" serializer.apply_uid_validity(folder.uid_validity) begin Downloader.new( folder, serializer, block_size: config.download_block_size ).run rescue Net::IMAP::ByeResponseError reconnect retry end end end |
#server ⇒ Object
139 140 141 |
# File 'lib/imap/backup/account/connection.rb', line 139 def server @server ||= account.server || provider.host end |
#status ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/imap/backup/account/connection.rb', line 52 def status ensure_account_folder backup_folders.map do |folder| s = Serializer::Mbox.new(account.local_path, folder.name) {name: folder.name, local: s.uids, remote: folder.uids} end end |