Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Even faster for postgresql

  • Loading branch information...
commit 5de3205f13c55b7911fbbfc528ec499204cbf161 1 parent 940bf92
@scosman scosman authored
Showing with 13 additions and 11 deletions.
  1. +13 −11 lib/delayed/backend/active_record.rb
View
24 lib/delayed/backend/active_record.rb
@@ -68,17 +68,19 @@ def self.reserve(worker, max_run_time = Worker.max_run_time)
now = self.db_time_now
- # This works on any database and uses seperate queries to lock and return the job
- # Databases like PostgreSQL and MySQL that support "SELECT .. FOR UPDATE" (ActiveRecord Pessimistic locking) don't need the second application
- # of 'readyScope' but it doesn't hurt and it ensures that the job being locked still meets ready_to_run criteria.
- count = readyScope.where(:id => nextScope).update_all(:locked_at => now, :locked_by => worker.name)
- return nil if count == 0
- return self.where(:locked_at => now, :locked_by => worker.name).first
-
- # This works on PostgreSQL and uses 1 less query, but uses SQL not supported nativly through ActiveRecord
- #quotedTableName = ::ActiveRecord::Base.connection.quote_column_name(self.table_name)
- #reserved = self.find_by_sql(["UPDATE #{quotedTableName} SET locked_at = ?, locked_by = ? WHERE id IN (#{nextScope.to_sql}) RETURNING *",now,worker.name])
- #return reserved[0]
+ if rails3? && (::ActiveRecord::Base.connection.adapter_name == "PostgreSQL")
+ # This works on PostgreSQL and uses 1 less query, but uses SQL not supported nativly through ActiveRecord
+ quotedTableName = ::ActiveRecord::Base.connection.quote_column_name(self.table_name)
+ reserved = self.find_by_sql(["UPDATE #{quotedTableName} SET locked_at = ?, locked_by = ? WHERE id IN (#{nextScope.to_sql}) RETURNING *",now,worker.name])
+ return reserved[0]
+ else
+ # This works on any database and uses seperate queries to lock and return the job
+ # Databases like PostgreSQL and MySQL that support "SELECT .. FOR UPDATE" (ActiveRecord Pessimistic locking) don't need the second application
+ # of 'readyScope' but it doesn't hurt and it ensures that the job being locked still meets ready_to_run criteria.
+ count = readyScope.where(:id => nextScope).update_all(:locked_at => now, :locked_by => worker.name)
+ return nil if count == 0
+ return self.where(:locked_at => now, :locked_by => worker.name).first
+ end
end
# Lock this job for this worker.
Please sign in to comment.
Something went wrong with that request. Please try again.