Class: Async::Lock
- Inherits:
-
Object
- Object
- Async::Lock
- Defined in:
- lib/async/locked.rb
Class Method Summary collapse
- .claim(lock_name) ⇒ Object
- .create(lock_name) ⇒ Object
- .is_lock_arg?(arg) ⇒ Boolean
- .make_name(*names) ⇒ Object
- .pass_on(lock_name) ⇒ Object
- .thread_fetch_lock(lock_name) ⇒ Object
- .thread_kill_lock(lock_name) ⇒ Object
- .thread_save_lock(lock_name, lock) ⇒ Object
Instance Method Summary collapse
- #as_job_arg ⇒ Object
- #claim ⇒ Object
-
#initialize(lock_name) ⇒ Lock
constructor
A new instance of Lock.
- #lock ⇒ Object
- #pass_on ⇒ Object
- #redis ⇒ Object
- #refresh! ⇒ Object
- #release ⇒ Object
Constructor Details
#initialize(lock_name) ⇒ Lock
Returns a new instance of Lock.
48 49 50 51 |
# File 'lib/async/locked.rb', line 48 def initialize(lock_name) @lock_name = lock_name @passed_on = false end |
Class Method Details
.claim(lock_name) ⇒ Object
124 125 126 127 |
# File 'lib/async/locked.rb', line 124 def self.claim(lock_name) lock = Lock.new(lock_name) lock.claim && thread_save_lock(lock_name, lock) && lock end |
.create(lock_name) ⇒ Object
134 135 136 137 |
# File 'lib/async/locked.rb', line 134 def self.create(lock_name) lock = Lock.new(lock_name) lock.lock && thread_save_lock(lock_name, lock) && lock end |
.is_lock_arg?(arg) ⇒ Boolean
53 54 55 |
# File 'lib/async/locked.rb', line 53 def self.is_lock_arg?(arg) arg.is_a?(Hash) && arg["_lock_arg"] end |
.make_name(*names) ⇒ Object
44 45 46 |
# File 'lib/async/locked.rb', line 44 def self.make_name(*names) "lock:"+names.join(":") end |
.pass_on(lock_name) ⇒ Object
129 130 131 132 |
# File 'lib/async/locked.rb', line 129 def self.pass_on(lock_name) lock = thread_fetch_lock(lock_name) lock && lock.pass_on && lock end |
.thread_fetch_lock(lock_name) ⇒ Object
119 120 121 122 |
# File 'lib/async/locked.rb', line 119 def self.thread_fetch_lock(lock_name) Thread.current["Async::Lock.named"] ||= {} Thread.current["Async::Lock.named"][lock_name] end |
.thread_kill_lock(lock_name) ⇒ Object
109 110 111 112 |
# File 'lib/async/locked.rb', line 109 def self.thread_kill_lock(lock_name) Thread.current["Async::Lock.named"] ||= {} Thread.current["Async::Lock.named"][lock_name] = nil end |
.thread_save_lock(lock_name, lock) ⇒ Object
114 115 116 117 |
# File 'lib/async/locked.rb', line 114 def self.thread_save_lock(lock_name, lock) Thread.current["Async::Lock.named"] ||= {} Thread.current["Async::Lock.named"][lock_name] = lock end |
Instance Method Details
#as_job_arg ⇒ Object
57 58 59 |
# File 'lib/async/locked.rb', line 57 def as_job_arg {"_lock_arg" => true, 'lock_name' => @lock_name} end |
#claim ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'lib/async/locked.rb', line 61 def claim Notifications.notify_lock("claim", @lock_name) if redis.get(@lock_name) #still locked refresh! return true else lock end end |
#lock ⇒ Object
71 72 73 74 75 76 77 78 79 |
# File 'lib/async/locked.rb', line 71 def lock if redis.setnx(@lock_name, "locked") refresh! Notifications.notify_lock("lock", @lock_name) return true else return false end end |
#pass_on ⇒ Object
85 86 87 88 89 90 91 92 93 |
# File 'lib/async/locked.rb', line 85 def pass_on if @passed_on #already passed on, can't pass again false else @passed_on = true true end end |
#redis ⇒ Object
105 106 107 |
# File 'lib/async/locked.rb', line 105 def redis Async::Locked.redis end |
#refresh! ⇒ Object
81 82 83 |
# File 'lib/async/locked.rb', line 81 def refresh! redis.expire(@lock_name, Async::Locked.lock_time || 15) end |
#release ⇒ Object
95 96 97 98 99 100 101 102 103 |
# File 'lib/async/locked.rb', line 95 def release if @passed_on return false end redis.del(@lock_name) Notifications.notify_lock("release", @lock_name) Thread.current["Async::Lock.named"][@lock_name] = nil return true end |