Method: CobwebModule::Crawl#lock

Defined in:
lib/crawl.rb

#lock(key, &block) ⇒ Object



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/crawl.rb', line 272

def lock(key, &block)
  debug_puts "REQUESTING LOCK [#{key}]"
  set_nx = @redis.setnx("#{key}_lock", "locked")
  debug_puts "LOCK:#{key}:#{set_nx}"
  while !set_nx
    debug_puts "===== WAITING FOR LOCK [#{key}] ====="
    sleep 0.01
    set_nx = @redis.setnx("#{key}_lock", "locked")
  end

  debug_puts "RECEIVED LOCK [#{key}]"
  @redis.expire("#{key}_lock", 30)
  begin
    result = yield
  ensure
    @redis.del("#{key}_lock")
    #debug_puts "LOCK RELEASED [#{key}]"
  end
  result
end