Class: ActiveJob::Locking::Adapters::Memory

Inherits:
Base
  • Object
show all
Defined in:
lib/activejob/locking/adapters/memory.rb

Instance Attribute Summary

Attributes inherited from Base

#key, #lock_manager, #lock_token, #options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#initialize, #refresh_lock!

Constructor Details

This class inherits a constructor from ActiveJob::Locking::Adapters::Base

Class Method Details

.lock(key) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/activejob/locking/adapters/memory.rb', line 8

def self.lock(key)
  @mutex.synchronize do
    if @hash[key]
      false
    else
      @hash[key] = Time.now
    end
  end
end

.locked?(key) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
28
# File 'lib/activejob/locking/adapters/memory.rb', line 24

def self.locked?(key)
  @mutex.synchronize do
    @hash.include?(key)
  end
end

.resetObject



30
31
32
33
34
# File 'lib/activejob/locking/adapters/memory.rb', line 30

def self.reset
  @mutex.synchronize do
    @hash = Hash.new
  end
end

.unlock(key) ⇒ Object



18
19
20
21
22
# File 'lib/activejob/locking/adapters/memory.rb', line 18

def self.unlock(key)
  @mutex.synchronize do
    @hash.delete(key)
  end
end

Instance Method Details

#create_lock_managerObject



36
37
# File 'lib/activejob/locking/adapters/memory.rb', line 36

def create_lock_manager
end

#lockObject



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/activejob/locking/adapters/memory.rb', line 39

def lock
  finish = Time.now + self.options.lock_acquire_time
  sleep_time = [5, self.options.lock_acquire_time / 5].min

  begin
    lock = self.class.lock(key)
    return lock if lock
    sleep(sleep_time)
  end while Time.now < finish

  false
end

#unlockObject



52
53
54
# File 'lib/activejob/locking/adapters/memory.rb', line 52

def unlock
  self.class.unlock(self.key)
end