Class: Imap::Backup::Account::Locker

Inherits:
Object
  • Object
show all
Defined in:
lib/imap/backup/account/locker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(account:) ⇒ Locker

Returns a new instance of Locker.

Parameters:

  • account (Account)

    the account whose backup must be locked



13
14
15
# File 'lib/imap/backup/account/locker.rb', line 13

def initialize(account:)
  @account = 
end

Instance Attribute Details

#accountObject (readonly)

Returns the value of attribute account.



10
11
12
# File 'lib/imap/backup/account/locker.rb', line 10

def 
  @account
end

Instance Method Details

#with_lock(&block) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/imap/backup/account/locker.rb', line 17

def with_lock(&block)
  lockfile = Lockfile.new(path: .lockfile_path)
  if lockfile.exists?
    if !lockfile.stale?
      raise Lockfile::LockfileExistsError,
            "Lockfile '#{account.lockfile_path}' exists and is not stale."
    end

    Logger.logger.info("Stale lockfile '#{account.lockfile_path}' found. Removing it.")
    lockfile.remove
  else
    ::FolderEnsurer.new(account: ).run
  end

  begin
    lockfile.with_lock do
      block.call
    end
  rescue Lockfile::ProcessStartTimeUnavailableError
    block.call
  end
end