Skip to content

Commit

Permalink
convert recursion to loop
Browse files Browse the repository at this point in the history
  • Loading branch information
ankithads committed Jun 28, 2024
1 parent 8f096af commit 30a2103
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
20 changes: 10 additions & 10 deletions lib/que/adapters/active_record_with_lock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ def execute(command, params=[])
end

def lock_job_with_lock_database(queue, cursor)
result = Que.execute(:find_job_to_lock, [queue, cursor])
return result if result.empty?

if pg_try_advisory_lock?(result.first['job_id'])
return result
end

# continue the recursion to fetch the next available job
lock_job_with_lock_database(queue, result.first['job_id'])
result = []
loop do
result = Que.execute(:find_job_to_lock, [queue, cursor])
break if result.empty?
if pg_try_advisory_lock?(result.first['job_id'])
break
end
end
return result
end

def cleanup!
Expand All @@ -65,7 +65,7 @@ def pg_try_advisory_lock?(job_id)
end

def unlock_job(job_id)
lock_database_connection.execute("SELECT pg_advisory_unlock(#{job_id})")
lock_database_connection.execute("SELECT pg_advisory_unlock(#{job_id})")
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/que/worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def initialize(

def work_loop
return if @stop
Que.adapter.checkout_lock_database_connection if ENV.fetch("YUGABYTE_QUE_WORKER_ENABLED", false)
Que.adapter.checkout_lock_database_connection if Que.adapter.class == Que::Adapters::ActiveRecordWithLock
@tracer.trace(RunningSecondsTotal, queue: @queue, primary_queue: @queue) do
loop do
case event = work
Expand All @@ -161,7 +161,7 @@ def work_loop
end

if @stop
Que.adapter.release_lock_database_connection if ENV.fetch("YUGABYTE_QUE_WORKER_ENABLED", false)
Que.adapter.release_lock_database_connection if Que.adapter.class == Que::Adapters::ActiveRecordWithLock
break
end
end
Expand Down

0 comments on commit 30a2103

Please sign in to comment.