Monitoring and reporting for ActiveJob.
- Minimalistic approach to ActiveJob monitoring, database based to avoid additional dependencies.
- Filter jobs by status (
enqueued
,running
orfinished
), user, resource or result (ok
,error
or custom). - Messages log by job.
- Automatic basic exception handling during errors.
- Allows to override built-in Job model.
- Add this line to your application's Gemfile:
gem 'active_job_reporter'
- Update bundle
$ bundle
- Run installer
Add jobs
, job_objects
and job_messages
tables to your database and an initializer file for configuration:
$ bundle exec rails generate active_job_reporter:install
$ bundle exec rake db:migrate
- Add
ReportableJob
concern to your jobs. You can add toApplicationJob
to avoid adding to every job. Jobs will be tracked automatically.
include ActiveJobReporter::ReportableJob
- Define
current_user
method in your jobs to relate them to users. Usearguments
variable to retrieveperform
call arguments. Using keyword arguments with the same name would allow you to define atApplicationJob
.
def current_user
arguments.first&.fetch(:admin_user, nil)
end
- Define
related_objects
method in your jobs to relate them to other application records.
def related_objects
[
arguments.first&.fetch(:order, nil),
*arguments.first&.fetch(:items, [])
].compact
end
- Add log messages and result code inside your jobs
perform
methods.log
method allows to specify type of message and complex messages (stored as JSON in database). Useself.result
to store the result of the job (won't be saved until the end of the process). If not specified, result will be:ok
or:error
, when theperform
method raises an exception.
def perform(**params)
if has_issues?
log :issues, raw: "raw test message"
self.result = :issues
end
if params[:raise]
a = 1 / 0
end
log :user, key: "test.user_message.#{result}", params: { user_id: 1, number: 12 }
end
- Application models related to jobs can use the
HasJobs
concern to simplify access to them.
class Resource < ApplicationRecord
include ActiveJobReporter::HasJobs
end
Then, access to jobs can be made from jobs
association method.
2.4.1 :001 > Resource.first.jobs.count
=> 1
2.4.1 :002 > Resource.first.jobs.running.count
=> 0
- If an application
Job
model is needed (to extend it or avoid using a qualified name), it can be defined using theJobConcern
concern and specifying the class name in the initializer file.
# in app/models/job.rb
class Job < ActiveRecord::Base
include ActiveJobReporter::JobConcern
end
# in config/initializers/active_job_reporter.rb
ActiveJobReporter.configure do |config|
...
# The class name for jobs
config.job_class_name = "Job"
...
end
- Fixes to
JobConcern
. - Tests fixes.
- Fixed deprecated use of class instead of class name in
belongs_to
.
- First version.
Issues and PRs are welcomed.
The gem is available as open source under the terms of the MIT License.