1
0
Fork 0
mirror of https://github.com/mperham/sidekiq.git synced 2022-11-09 13:52:34 -05:00

A bit more polish for the API, #5291

This commit is contained in:
Mike Perham 2022-05-06 13:52:38 -07:00
parent c03680fa4d
commit fdfb7a5211
6 changed files with 38 additions and 11 deletions

View file

@ -2,6 +2,19 @@
[Sidekiq Changes](https://github.com/mperham/sidekiq/blob/main/Changes.md) | [Sidekiq Pro Changes](https://github.com/mperham/sidekiq/blob/main/Pro-Changes.md) | [Sidekiq Enterprise Changes](https://github.com/mperham/sidekiq/blob/main/Ent-Changes.md)
HEAD
---------
- Add beta support for transaction-aware client [#5291]
Add this line to your initializer and any jobs pushed during a transaction
will only be pushed after the transaction commits. You will need to add the
`after_commit_everywhere` gem to your Gemfile.
```ruby
Sidekiq.transactional_push!
```
- Fix regression with middleware arguments
6.4.2
---------

View file

@ -4,7 +4,8 @@ require "time"
module Sidekiq
module JobUtil
# These functions encapsulate various job utilities.
# They must be simple and free from side effects.
TRANSIENT_ATTRIBUTES = %w[]
def validate(item)
raise(ArgumentError, "Job must be a Hash with 'class' and 'args' keys: `#{item}`") unless item.is_a?(Hash) && item.key?("class") && item.key?("args")
@ -42,6 +43,9 @@ module Sidekiq
raise(ArgumentError, "Job must include a valid queue name") if item["queue"].nil? || item["queue"] == ""
# remove job attributes which aren't necessary to persist into Redis
TRANSIENT_ATTRIBUTES.each { |key| item.delete(key) }
item["jid"] ||= SecureRandom.hex(12)
item["class"] = item["class"].to_s
item["queue"] = item["queue"].to_s

View file

@ -1,12 +1,6 @@
# frozen_string_literal: true
begin
require "after_commit_everywhere"
rescue LoadError
Sidekiq.logger.error("You need to add after_commit_everywhere to your Gemfile for this to work")
exit(-127)
end
require "securerandom"
require "sidekiq/client"
module Sidekiq
@ -16,7 +10,11 @@ module Sidekiq
end
def push(item)
# pre-allocate the JID so we can return it immediately and
# save it to the database as part of the transaction.
item["jid"] ||= SecureRandom.hex(12)
AfterCommitEverywhere.after_commit { @redis_client.push(item) }
item["jid"]
end
##
@ -33,6 +31,15 @@ end
# Use `Sidekiq.transactional_push!` in your sidekiq.rb initializer
module Sidekiq
def self.transactional_push!
begin
require "after_commit_everywhere"
rescue LoadError
Sidekiq.logger.error("You need to add after_commit_everywhere to your Gemfile to use Sidekiq's transactional client")
raise
end
default_job_options["client_class"] = Sidekiq::TransactionAwareClient
Sidekiq::JobUtil::TRANSIENT_ATTRIBUTES << "client_class"
true
end
end

View file

@ -1,5 +1,5 @@
# frozen_string_literal: true
module Sidekiq
VERSION = "6.4.2"
VERSION = "6.5.0"
end

View file

@ -9,3 +9,5 @@ gem 'puma'
platforms :ruby do
gem 'sqlite3'
end
gem 'after_commit_everywhere'

View file

@ -1,4 +1,3 @@
Sidekiq.default_worker_options = { queue: "something" }
Sidekiq.configure_client do |config|
config.redis = { :size => 2 }
end
@ -42,4 +41,6 @@ module Myapp
end
require "sidekiq/middleware/current_attributes"
Sidekiq::CurrentAttributes.persist(Myapp::Current) # Your AS::CurrentAttributes singleton
Sidekiq::CurrentAttributes.persist(Myapp::Current) # Your AS::CurrentAttributes singleton
# Sidekiq.transactional_push!