Skip to content

Instantly share code, notes, and snippets.

@simonfranzen
Created January 16, 2019 11:19
Show Gist options
  • Save simonfranzen/1e96b8dc38a391627555fce77406913e to your computer and use it in GitHub Desktop.
Save simonfranzen/1e96b8dc38a391627555fce77406913e to your computer and use it in GitHub Desktop.
A base class for background tasks, rubyonrails
class BackgroundTaskJob < Struct.new(:background_task_id, :background_task_type)
def enqueue(job)
check_and_update_status
job.delayed_reference_id = background_task_id
job.delayed_reference_type = background_task_type
job.save!
end
def success(job)
set_status('success')
end
def failure(job)
set_errors(job, StandardError.new('Job Status: Failure'))
end
def error(job, exception)
set_errors(exception)
end
def perform
background_task = Tasks::BackgroundTask.find background_task_id
background_task.process!
end
private
def set_status(status)
background_task = Tasks::BackgroundTask.find background_task_id
background_task.status = status
background_task.save!
end
private
def set_errors(exception)
Rollbar.error(exception)
background_task = Tasks::BackgroundTask.find background_task_id
background_task.has_errors = true
background_task.error_message = exception.inspect
background_task.status = 'error'
background_task.save!
end
private
def check_and_update_status
set_status('new')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment