LoggingWorker Gem

Posted on 29 Dec 2014 by Eric Oestrich

The last post was about logging workers/jobs as they happened in a manual process. From that I created a small gem that handles most of this for you. LoggingWorker is the gem. It's not up on rubygems yet, so you'll need to install it via git or github in bundler.

gem 'logging_worker', :github => "smartlogic/logging_worker"

Once it's installed you'll need create the migrations.

$ rake logging_worker_engine:install:migrations
$ rake db:migrate db:test:prepare

To have workers start logging, you need to prepend the module in each worker. prepend is very important because the #perform method needs to be the LoggingWorker::Worker#perform.

Once this is done, everything is taken care of for you. You can continue to enhance your workers though. In one project I have it associate job runs to an object. To do this you need to set a custom JobRun class.

class ImportantWorker
  include Sidekiq::Worker
  prepend LoggingWorker::Worker

    :job_run_class => ::JobRun

  def perform(order_id)
    order = Order.find(order_id)
    job_run.order = order
class JobRun < LoggingWorker::JobRun
  belongs_to :order

You can also use the logger as the worker does its work. The log is saved inside the JobRun record.

class ImportantWorker
  include Sidekiq::Worker
  prepend LoggingWorker::Worker

  def perform(order_id)
    order = Order.find(order_id)
    logger.info "Found the order, starting work..."

If an error is raised it is captured, saved, and then reraised to let sidekiq perform it again.

Below is the full schema that comes with the JobRun class from the gem.

create_table "job_runs", force: true do |t|
  t.string   "worker_class"
  t.string   "arguments",       array: true
  t.boolean  "successful"
  t.datetime "completed_at"
  t.text     "log"
  t.text     "error_class"
  t.text     "error_message"
  t.text     "error_backtrace", array: true
  t.datetime "created_at"
  t.datetime "updated_at"
comments powered by Disqus
Eric Oestrich
I am:
All posts
Creative Commons License
This site's content is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License unless otherwise specified. Code on this site is licensed under the MIT License unless otherwise specified.