Skip to content

Commit

Permalink
Cherry-picked PRs from upstream repo
Browse files Browse the repository at this point in the history
  • Loading branch information
sauy7 committed Sep 11, 2021
1 parent d65b0f9 commit 99cec25
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ bin
coverage
pkg/*
gemfiles/**/*.gemfile.lock
.idea
8 changes: 7 additions & 1 deletion lib/delayed/backend/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ def self.ready_to_run(worker_name, max_run_time)
)
end

# See: https://github.com/collectiveidea/delayed_job_active_record/pull/163
def self.recover_from(_error)
::ActiveRecord::Base.connection.verify!
end

def self.before_fork
::ActiveRecord::Base.clear_all_connections!
end
Expand Down Expand Up @@ -122,6 +127,7 @@ def self.reserve_with_scope_using_default_sql(ready_scope, worker, now)
end
end

# See: https://github.com/collectiveidea/delayed_job_active_record/pull/169
def self.reserve_with_scope_using_optimized_postgres(ready_scope, worker, now)
# Custom SQL required for PostgreSQL because postgres does not support UPDATE...LIMIT
# This locks the single record 'FOR UPDATE' in the subquery
Expand All @@ -131,7 +137,7 @@ def self.reserve_with_scope_using_optimized_postgres(ready_scope, worker, now)
# use 'FOR UPDATE' and we would have many locking conflicts
quoted_name = connection.quote_table_name(table_name)
subquery = ready_scope.limit(1).lock(true).select("id").to_sql
sql = "UPDATE #{quoted_name} SET locked_at = ?, locked_by = ? WHERE id IN (#{subquery}) RETURNING *"
sql = "UPDATE #{quoted_name} SET locked_at = ?, locked_by = ? WHERE id = (#{subquery}) RETURNING *"
reserved = find_by_sql([sql, now, worker.name])
reserved[0]
end
Expand Down

0 comments on commit 99cec25

Please sign in to comment.