Active Job Reporter
Monitoring and reporting for ActiveJob.
Features
- Minimalistic approach to ActiveJob monitoring, database based to avoid additional dependencies.
- Filter jobs by status (
enqueued,runningorfinished), user, resource or result (ok,erroror custom). - Messages log by job.
- Automatic basic exception handling during errors.
- Allows to override built-in Job model.
Usage
- Add
ReportableJobconcern to your jobs. You can add toApplicationJobto avoid adding to every job. Jobs will be tracked automatically.
include ActiveJobReporter::ReportableJob
- Define
current_usermethod in your jobs to relate them to users. Useargumentsvariable to retrieveperformcall 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_objectsmethod in your jobs to relate them to other application records.
def
[ arguments.first&.fetch(:order, nil), *arguments.first&.fetch(:items, []) ].compact
end
- Add log messages and result code inside your jobs
performmethods.logmethod allows to specify type of message and complex messages (stored as JSON in database). Useself.resultto store the result of the job (won't be saved until the end of the process). If not specified, result will be:okor:error, when theperformmethod 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
HasJobsconcern 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
Jobmodel is needed (to extend it or avoid using a qualified name), it can be defined using theJobConcernconcern 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.jobs_class_name = "Job"
...
end
Installation
- 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
Changelog
0.1.0
- First version.
Contributing
Issues and PRs are welcomed.
License
The gem is available as open source under the terms of the MIT License.