docbrown/config/initializers/sidekiq.rb
Daniel Uber 4b4d8a7234
Ensure arguments to perform_async are json safe (#16285)
* Convert symbol hash keys to strings when calling .perform_async

Fixes a warning from Sidekiq 6.4.0+ about perform_async arguments
which are not equal when passed to perform (`JSON.parse(JSON.dump(arg))`
should equal arg).

This is a safety measure to prevent passing objects (like classes, or
model instances) rather than their representations (like a class name,
or a model's attributes hash).

This warning will be an error in sidekiq 7

* Turn warning into an error in non-production environments

* Use string keys for reaction notification and article fetched

Missed these two on the first pass

* Update example argument hashes for #enqueues_on_correct_queue

Since this calls perform_async under the hood we need to pass json
safe hashes in the test cases as well.

https://github.com/forem/forem/blob/main/spec/workers/shared_examples/enqueues_on_correct_queue.rb

* Convert keys from FollowData#to_h to string before perform_async

I'm not sure enough where else (outside of notification) to_h is being
called, so I'm converting here when building args, rather than in
FollowData#to_h, which might be my next step.

* Let FollowData#to_h return a hash with string keys

Update spec to use string keys as well.

* Make to_h return string keys for ReactionData

Like FollowData, the #to_h method is only used to call
notifications (this is used to enqueue sidekiq jobs).

* Remove a key that was in the hash

Since reaction_data calls to_h, it gets string and not symbol,
keys. Call Hash#except with a key that was actually there.
2022-01-25 18:07:40 -06:00

67 lines
2.4 KiB
Ruby

module Sidekiq
module Cron
class Job
def not_past_scheduled_time?(current_time)
last_cron_time = parsed_cron.previous_time(current_time).utc
# @mstruve/@sre: method monkey patched to increase time we look back for
# unrun scheduled jobs from 60 to 120. Heroku takes 60 seconds to restart
# sidekiq workers, we need to ensure any job scheduled during that down time
# is run once Sidekiq boots back up
# https://github.com/ondrejbartas/sidekiq-cron/blob/074a87546f16122c1f508bb2805b9951588f2510/lib/sidekiq/cron/job.rb#L593
return false if (current_time.to_i - last_cron_time.to_i) > (ENV["CRON_LOOKBACK_TIME"] || 60).to_i
true
end
end
end
end
Rails.application.config.to_prepare do
Dir.glob(Rails.root.join("lib/sidekiq/*.rb")).each do |filename|
require_dependency filename
end
end
Sidekiq.configure_server do |config|
schedule_file = "config/schedule.yml"
# @mstruve/@sre: sidekiq-cron still uses the removed poll_interval
# to determine how often to poll for jobs so we should manually set it
# https://github.com/ondrejbartas/sidekiq-cron/issues/254
# Sidekiq default is 5, we don't need it quite that often but would like it more than
# every 30 seconds which the gem defaults to
Sidekiq.options[:poll_interval] = 10
if File.exist?(schedule_file)
Sidekiq::Cron::Job.load_from_hash!(YAML.load_file(schedule_file))
end
sidekiq_url = ApplicationConfig["REDIS_SIDEKIQ_URL"] || ApplicationConfig["REDIS_URL"]
# On Heroku this configuration is overridden and Sidekiq will point at the redis
# instance given by the ENV variable REDIS_PROVIDER
config.redis = { url: sidekiq_url }
config.server_middleware do |chain|
chain.add Sidekiq::HoneycombMiddleware
chain.add SidekiqUniqueJobs::Middleware::Client
end
config.server_middleware do |chain|
chain.add SidekiqUniqueJobs::Middleware::Server
end
SidekiqUniqueJobs::Server.configure(config)
# This allows us to define custom logic to handle when a worker has exhausted all
# of it's retries. For more details: https://github.com/mperham/sidekiq/wiki/Error-Handling#death-notification
config.death_handlers << lambda do |job, _ex|
Sidekiq::WorkerRetriesExhaustedReporter.report_final_failure(job)
end
end
Sidekiq.configure_client do |config|
config.client_middleware do |chain|
chain.add SidekiqUniqueJobs::Middleware::Client
end
end
Sidekiq.strict_args! unless Rails.env.production?