Skip to content

Commit

Permalink
Job scopes and auto expiration (#1184)
Browse files Browse the repository at this point in the history
* Job scopes and auto expiration

* Tweak log levels

* Consolidate Notify key to application config

* lint
  • Loading branch information
peterdavidhamilton authored May 17, 2024
1 parent 4a27d05 commit 1e65d38
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 52 deletions.
13 changes: 9 additions & 4 deletions app/jobs/application_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def run(*)
log 'running'

if duplicate_job_queued?
log 'already queued'
log 'already queued', :warn
raise DuplicateJobError, 'already queued'
elsif block_given?
yield
Expand All @@ -29,13 +29,18 @@ def duplicate_job_queued?
# @param error [Error]
# @return [void]
def handle_error(error)
log("failed with '#{error.message}'")
log "failed with '#{error.message}'", :warn
case error
when DuplicateJobError then expire
end
end

# @param message [String]
# @param level [Symbol]
# @return [String]
def log(message)
def log(message, level = :info)
message = "#{ENV['DOMAIN']} - #{self.class.name} #{message}"
Sentry.capture_message(message, level: :info) if Rails.env.production?
Sentry.capture_message(message, level: level) if Rails.env.production?

if ENV['RAILS_LOG_TO_STDOUT'].present?
Rails.logger.info(message)
Expand Down
2 changes: 1 addition & 1 deletion app/jobs/content_check_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def valid?
log Training::Module.cache.size # should be larger than 0

unless check.valid?
log "ContentCheckJob: #{mod.name} in '#{env}' via '#{api}' is not valid"
log "ContentCheckJob: #{mod.name} in '#{env}' via '#{api}' is not valid", :warn
end

check.valid?
Expand Down
15 changes: 0 additions & 15 deletions app/jobs/fill_page_views_job.rb

This file was deleted.

2 changes: 1 addition & 1 deletion app/jobs/mail_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def self.recipients
end

def run(*)
log("#{self.class.name}: #{self.class.recipients.count} recipients")
log "#{self.class.name}: #{self.class.recipients.count} recipients"

super
end
Expand Down
2 changes: 2 additions & 0 deletions app/jobs/new_module_mail_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#
# Notify users of the latest published module and record the release timestamp
class NewModuleMailJob < MailJob
self.maximum_retry_count = 0

# @param release_id [Integer]
def run(release_id)
super do
Expand Down
24 changes: 24 additions & 0 deletions app/models/job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'que/active_record/model'

# by_args
# by_job_class
# by_queue
# by_tag
#
# errored
# expired
# finished
# ready
# scheduled
#
# not_errored
# not_expired
# not_finished
# not_ready
# not_scheduled
#
class Job < Que::ActiveRecord::Model
def self.start_training
by_job_class('StartTrainingMailJob')
end
end
5 changes: 4 additions & 1 deletion config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ class Application < Rails::Application

config.google_cloud_bucket = ENV.fetch('GOOGLE_CLOUD_BUCKET', '#GOOGLE_CLOUD_BUCKET_env_var_missing')
config.dashboard_update_interval = ENV.fetch('DASHBOARD_UPDATE_INTERVAL', '0 0 * * *') # Midnight daily
config.mail_job_interval = ENV.fetch('MAIL_JOB_INTERVAL', '0 12 * * *') # Noon daily

# Notify
config.notify_token = ENV.fetch('GOVUK_NOTIFY_API_KEY', credentials.notify_api_key)
config.mail_job_interval = ENV.fetch('MAIL_JOB_INTERVAL', '0 12 * * *') # Noon daily

config.user_password = ENV.fetch('USER_PASSWORD', 'Str0ngPa$$w0rd12')
config.bot_token = ENV['BOT_TOKEN']
Expand Down
7 changes: 3 additions & 4 deletions config/initializers/govuk_notify_rails.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
class NotifyDelivery < GovukNotifyRails::Delivery
# Ensure delivery errors do not impede bulk mailout
# @param message [Mail::Message]
# @return [Symbol, Array<Notifications::Client::ResponseNotification>]
def deliver!(message)
super
rescue StandardError
Sentry.capture_message "#{self.class.name} failed: '#{message.display}'", level: :info
Sentry.capture_message "#{self.class.name} failed: '#{message.display}'", level: :error
:skipped_due_to_error
end
end

ActionMailer::Base.add_delivery_method(
:govuk_notify,
NotifyDelivery,
api_key: ENV.fetch('GOVUK_NOTIFY_API_KEY', Rails.application.credentials.notify_api_key),
:govuk_notify, NotifyDelivery, api_key: Rails.application.config.notify_token
)
31 changes: 5 additions & 26 deletions config/initializers/que.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# @note
# Que::Scheduler::Migrations.reenqueue_scheduler_if_missing
#
Que::Scheduler.configure do |config|
config.schedule = {
DashboardJob: { cron: Rails.application.config.dashboard_update_interval },
Expand All @@ -7,31 +10,7 @@
}
end

# Que.logger = QUE_LOGGER

# https://github.com/que-rb/que/tree/master/docs#error-notifications
# @see https://github.com/que-rb/que/tree/master/docs#error-notifications
#
# Que.error_notifier = proc do |error, job|
# Do whatever you want with the error object or job row here. Note that the
# job passed is not the actual job object, but the hash representing the job
# row in the database, which looks like:

# {
# :priority => 100,
# :run_at => "2017-09-15T20:18:52.018101Z",
# :id => 172340879,
# :job_class => "TestJob",
# :error_count => 0,
# :last_error_message => nil,
# :queue => "default",
# :last_error_backtrace => nil,
# :finished_at => nil,
# :expired_at => nil,
# :args => [],
# :data => {}
# }

# This is done because the job may not have been able to be deserialized
# properly, if the name of the job class was changed or the job class isn't
# loaded for some reason. The job argument may also be nil, if there was a
# connection failure or something similar.
# end

0 comments on commit 1e65d38

Please sign in to comment.