Module: RudeQ::TokenLock

Defined in:
lib/rude_q.rb

Overview

a crazy hack around database locking that I thought was a good idea turns out we can’t make it use transactions properly without creating a whole table lock which misses the point

also, it doesn’t work on SQLite as it requires “UPDATE … LIMIT 1 ORDER id ASC” and as of RudeQueue2, you’ll need to manually add the “token” column

Class Method Summary collapse

Class Method Details

.fetch_with_lock(klass, qname) ⇒ Object

:nodoc:



195
196
197
198
199
200
201
# File 'lib/rude_q.rb', line 195

def fetch_with_lock(klass, qname) # :nodoc:
  token = get_unique_token
  klass.update_all(["token = ?", token], ["queue_name = ? AND processed = ? AND token IS NULL", qname, false], :limit => 1, :order => "id ASC")
  record = klass.find_by_queue_name_and_token_and_processed(qname, token, false)

  return yield(record)
end

.get_unique_tokenObject

:nodoc:



209
210
211
212
213
214
215
216
# File 'lib/rude_q.rb', line 209

def get_unique_token # :nodoc:
  digest = Digest::SHA1.new
  digest << Time.now.to_s
  digest << Process.pid.to_s
  digest << Socket.gethostname
  digest << self.token_count!.to_s # multiple requests from the same pid in the same second get different token
  return digest.hexdigest
end

.token_count!Object

:nodoc:



203
204
205
206
207
# File 'lib/rude_q.rb', line 203

def token_count! # :nodoc:
  @token_count ||= 0
  @token_count += 1
  return @token_count
end