Giter Site home page Giter Site logo

active_job_reporter's Introduction

Active Job Reporter

Monitoring and reporting for ActiveJob.

Gem Travis Codecov

Features

  • Minimalistic approach to ActiveJob monitoring, database based to avoid additional dependencies.
  • Filter jobs by status (enqueued, running or finished), 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.

Installation

  1. Add this line to your application's Gemfile:
gem 'active_job_reporter'
  1. Update bundle
$ bundle
  1. 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

Usage

  1. Add ReportableJob concern to your jobs. You can add to ApplicationJob to avoid adding to every job. Jobs will be tracked automatically.
include ActiveJobReporter::ReportableJob
  1. Define current_user method in your jobs to relate them to users. Use arguments variable to retrieve perform call arguments. Using keyword arguments with the same name would allow you to define at ApplicationJob.
def current_user
  arguments.first&.fetch(:admin_user, nil)
end
  1. 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
  1. 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). Use self.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 the perform 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
  1. 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
  1. If an application Job model is needed (to extend it or avoid using a qualified name), it can be defined using the JobConcern 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

Changelog

0.1.2

  • Fixes to JobConcern.
  • Tests fixes.

0.1.1

  • Fixed deprecated use of class instead of class name in belongs_to.

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.

active_job_reporter's People

Contributors

leio10 avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

gorifater

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.