docbrown/app/workers/data_update_worker.rb
Kirk Haines ed74f2f245
Abstract DatadogStatsClient to ForemStatsClient (#12304)
* This change abstracts the DatadogStatsClient into a ForemStatsClient.
The purpose of this abstraction is to set the foundation for a subsequent PR that will allow one to use New Relic for recording Forem stats, instead of Datadog, if there is a New Relic configuration found.
This specific change creates an abstraction layer that can be built upon, without changing any actual default behavior. All specs still pass.

* Use delegate instead of explicit methods.

* Delegate instead of explicit methods.

* Fix the error.

* Refactor according to the suggestions in the comments.

* Ooops.  Stats work better when all of the code is committed.

* Removing the alias of count to increment since that was done in error.
2021-01-27 11:25:44 -05:00

42 lines
978 B
Ruby

class DataUpdateWorker
include Sidekiq::Worker
sidekiq_options queue: :high_priority, retry: 5
def perform
DataUpdateScript.scripts_to_run.each do |script|
script.mark_as_run!
log_status(script)
run_script(script)
end
end
private
def run_script(script)
require script.file_path
script.file_class.new.run
script.mark_as_finished!
log_status(script)
rescue StandardError => e
script.mark_as_failed!(e)
log_status(script)
Honeybadger.notify(e, context: { script_id: script.id })
end
def log_status(script)
status = script.status
file_name = script.file_name
logger_destination = status.to_sym == :failed ? :error : :info
Rails.logger.public_send(
logger_destination,
"time=#{Time.current.rfc3339}, script=#{file_name}, status=#{status}",
)
ForemStatsClient.increment("data_update_scripts.status", tags: ["status:#{status}", "script_name:#{file_name}"])
end
end