• Stars
    star
    270
  • Rank 146,558 (Top 3 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 1 year ago
  • Updated 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Dashboard and Active Job extensions to operate and troubleshoot background jobs

Mission Control — Jobs

This gem provides a Rails-based frontend to Active Job adapters. It currently supports Resque and Solid Queue. Its features depend on those offered by the adapter itself. At a minimum, it allows you to inspect job queues and jobs currently waiting in those queues and inspect and retry or discard failed jobs.

Installation

Add this line to your application's Gemfile:

gem "mission_control-jobs"

And then execute:

$ bundle install

Basic configuration

Mount Mission Control Job's engine where you wish to have it accessible from your app, in your routes.rb file:

Rails.application.routes.draw do
  # ...
  mount MissionControl::Jobs::Engine, at: "/jobs"

And that's it. With this alone, you should be able to access Mission Control Job's UI, where you can browse the existing queues, jobs pending in these queues, jobs in different statuses, and discard and retry failed jobs:

Queues tab in a simple app

Failed jobs tab in a simple app

Authentication and base controller class

By default, Mission Control's controllers will extend the host app's ApplicationController. If no authentication is enforced, /jobs will be available to everyone. You might want to implement some kind of authentication for this in your app. To make this easier, you can specify a different controller as the base class for Mission Control's controllers:

Rails.application.configure do
  MissionControl::Jobs.base_controller_class = "AdminController"
end

Or, in your environment config or application.rb:

config.mission_control.jobs.base_controller_class = "AdminController"

Other configuration settings

Besides base_controller_class, you can also set the following for MissionControl::Jobs or config.mission_control.jobs:

  • logger: the logger you want Mission Control Jobs to use. Defaults to ActiveSupport::Logger.new(nil) (no logging). Notice that this is different from Active Job's logger or Active Job's backend's configured logger.
  • delay_between_bulk_operation_batches: how long to wait between batches when performing bulk operations, such as discard all or retry all jobs—defaults to 0
  • adapters: a list of adapters that you want Mission Control to use and extend. By default this will be the adapter you have set for active_job.queue_adapter.
  • internal_query_count_limit: in count queries, the maximum number of records that will be counted if the adapter needs to limit these queries. True counts above this number will be returned as INFINITY. This keeps count queries fast—defaults to 500,000

This library extends Active Job with a querying interface and the following setting:

  • config.active_job.default_page_size: the internal batch size that Active Job will use when sending queries to the underlying adapter and the batch size for the bulk operations defined above—defaults to 1000.

Advanced configuration

When we built Mission Control Jobs, we did it with the idea of managing multiple apps' job backends from a single, centralized app that we used for monitoring, alerts and other tools that related to all our apps. Some of our apps run in more than one datacenter, and we run different Resque instances with different Redis configurations in each. Because of this, we added support for multiple apps and multiple adapters per app. Even when running Mission Control Job within the app it manages, and a single DC, as we migrated from Resque to Solid Queue, we needed to manage both adapters from Mission Control.

Without adding any additional configuration to the one described before, Mission Control will be configured with one single app and a single server for your configured active_job.queue_adapter.

If you want to support multiple adapters, you need to add them to Mission Control configuration via the adapters setting mentioned above. For example:

config.mission_control.jobs.adapters = [ :resque, :solid_queue ]

Then, to configure the different apps and/or different servers, you can do so in an initializer like this (taken from our dummy app for testing purposes):

require "resque"
require "resque_pause_helper"

require "solid_queue"

Resque.redis = Redis::Namespace.new "#{Rails.env}", redis: Redis.new(host: "localhost", port: 6379, thread_safe: true)

SERVERS_BY_APP = {
  BC4: %w[ resque_ashburn resque_chicago ],
  HEY: %w[ resque solid_queue ]
}

def redis_connection_for(app, server)
  redis_namespace = Redis::Namespace.new "#{app}:#{server}", redis: Resque.redis.instance_variable_get("@redis")
  Resque::DataStore.new redis_namespace
end

SERVERS_BY_APP.each do |app, servers|
  queue_adapters_by_name = servers.collect do |server|
    queue_adapter = if server.start_with?("resque")
      ActiveJob::QueueAdapters::ResqueAdapter.new(redis_connection_for(app, server))
    else
      ActiveJob::QueueAdapters::SolidQueueAdapter.new
    end

    [ server, queue_adapter ]
  end.to_h

  MissionControl::Jobs.applications.add(app, queue_adapters_by_name)
end

This is an example for two different apps, BC4 and HEY, each one with two servers. BC4 has two Resque servers with two different configurations, and HEY has one Resque server and one Solid Queue server.

Currently, only one Solid Queue configuration is supported, but support for several Solid Queue backends (with different databases) is planned.

This is how we set Resque and Solid Queue together when we migrated from one to the other:

queue_adapters_by_name = {
  resque: ActiveJob::QueueAdapters.lookup(:resque).new, # This will use Resque.redis as the redis client
  solid_queue: ActiveJob::QueueAdapters.lookup(:solid_queue).new
}

MissionControl::Jobs.applications.add("hey", queue_adapters_by_name)

When you have multiple apps and servers configured, you can choose between them with select and toggle menus:

Queues tab with multiple apps and servers

Basic UI usage

As mentioned, the features available in Mission Control depend on the adapter you're using, as each adapter supports different features. Besides inspecting the queues and the jobs in them, and discarding and retrying failed jobs, you can inspect jobs in different statuses supported by each adapter, filter them by queue name and job class name (with the idea of adding more filters in the future), pause and un-pause queues (if the adapter allows that), inspect workers, know which jobs are being run by what worker, checking a specific job or a specific worker...

Default queue tab

In-progress jobs tab

Workers tab

Single job

Single worker

Console helpers, scripting and dealing with big sets of jobs

Besides the UI, Mission Control provides a light console helper to switch between applications and adapters. Some potentially destructive actions aren't exposed via the UI (for example, discarding jobs that aren't failed, although this might change in the future), but you can always perform these from the console if you know very well what you're doing.

It's also possible that you need to deal with very big sets of jobs that are unmanageable via the UI or that you wish to write a script to deal with an incident, some cleanup or some data migration. The console helpers and the querying API with which we've extended Active Job come in handy here.

First, when connecting to the Rails console, you'll see this new message:

 bin/rails c


Type 'jobs_help' to see how to connect to the available job servers to manage jobs

Typing jobs_help, you'll get clear instructions about how to switch between applications and adapters:

>> jobs_help
You can connect to a job server with
  connect_to "<app_id>:<server_id>"

Available job servers:
  * bc4:resque_ashburn
  * bc4:resque_chicago
  * hey:resque
  * hey:solid_queue

And then:

>> connect_to "hey:solid_queue"
Connected to hey:solid_queue

Now you're ready to query and operate over jobs for this adapter via the API. Some examples of queries:

# All jobs
ActiveJob.jobs

# All failed jobs
ActiveJob.jobs.failed

# All pending jobs in some queue
ActiveJob.jobs.pending.where(queue_name: "some_queue")

# All failed jobs of a given class
ActiveJob.jobs.failed.where(job_class_name: "SomeJob")

# All pending jobs of a given class with limit and offset
ActiveJob.jobs.pending.where(job_class_name: "SomeJob").limit(10).offset(5)

# For adapters that support these statuses:
# All scheduled/in-progress/finished jobs of a given class
ActiveJob.jobs.scheduled.where(job_class_name: "SomeJob")
ActiveJob.jobs.in_progress.where(job_class_name: "SomeJob")
ActiveJob.jobs.finished.where(job_class_name: "SomeJob")

# For adapters that support filtering by worker:
# All jobs in progress being run by a given worker
ActiveJob.jobs.in_progress.where(worker_id: 42)

Some examples of bulk operations:

# Retry all the jobs (only possible for failed jobs)
ActiveJob.jobs.failed.retry_all

# Retry all the jobs of a given class (only possible for failed jobs)
ActiveJob.jobs.failed.where(job_class_name: "SomeJob").retry_all

# Discard all failed jobs
ActiveJob.jobs.failed.discard_all

# Discard all pending jobs of a given class
ActiveJob.jobs.pending.where(job_class_name: "SomeJob").discard_all
# Or all pending jobs in a given queue:
ActiveJob.jobs.pending.where(queue_name: "some-queue").discard_all

When performing these bulk operations in the console, a delay of 2 seconds between batches processed will be introduced, set via delay_between_bulk_operation_batches. You can modify it as

MissionControl::Jobs.delay_between_bulk_operation_batches = 5.seconds

Contributing

Thanks for your interest in contributing! To get the app running locally, just run:

bin/setup

This will load a bunch of jobs as seeds.

We have both unit and functional tests and system tests. If you want to run system tests, you'd need to install ChromeDriver. Then, you'll be able to run the tests as:

bin/rails test test/system

License

The gem is available as open source under the terms of the MIT License.

More Repositories

1

trix

A rich text editor for everyday writing
JavaScript
17,847
star
2

kamal

Deploy web apps anywhere.
Ruby
8,744
star
3

handbook

Basecamp Employee Handbook
6,165
star
4

pow

Zero-configuration Rack server for Mac OS X
CoffeeScript
3,423
star
5

policies

37signals policies, terms, and legal. Share them; reuse them; contribute to them.
1,863
star
6

local_time

Rails engine for cache-friendly, client-side local time
CoffeeScript
1,791
star
7

marginalia

Attach comments to ActiveRecord's SQL queries
Ruby
1,676
star
8

mail_view

Visual email testing
Ruby
1,341
star
9

xip-pdns

PowerDNS pipe backend adapter powering xip.io
Shell
1,159
star
10

geared_pagination

Paginate Active Record sets at variable speeds
Ruby
758
star
11

wysihat

A WYSIWYG JavaScript framework
JavaScript
681
star
12

bcx-api

API documentation and wrappers for Basecamp 2
672
star
13

name_of_person

Presenting names of people in full, familiar, abbreviated, and initialized forms (but without titulation etc)
Ruby
647
star
14

console1984

The Rails console you love, 1984 style
Ruby
548
star
15

google_sign_in

Sign in (or up) with Google for Rails applications
Ruby
494
star
16

bc3-api

API documentation for Basecamp 4
472
star
17

intermission

intermission helps you perform zero down time application maintenance
Lua
364
star
18

snapback_cache

A client side page cache for jquery.
JavaScript
316
star
19

audits1984

Auditing tool for Rails console sessions
Ruby
309
star
20

full_request_logger

Make full request logs accessible via web UI
Ruby
305
star
21

mysql_role_swap

(Nearly) Zero interruption mysql maintenance script.
Ruby
282
star
22

concerning

Bite-sized separation of concerns
Ruby
201
star
23

api

API integration and more for Basecamp products (Basecamp, Highrise, Campfire, Backpack)
192
star
24

easymon

Easy Monitoring
Ruby
191
star
25

trashed

Tell StatsD about request time, GC, objects and more. Latest Rails 4 and Ruby 2.1 support, and ancient Rails 2 and Ruby 1.8 support.
Ruby
189
star
26

highrise-api

Official API documentation for Highrise
130
star
27

fast_remote_cache

A faster version of Capistrano's remote_cache deployment strategy
Ruby
125
star
28

mass_encryption

Ruby
104
star
29

platform_agent

Parse user agent to deduce the platform
Ruby
103
star
30

cached_externals

Symlink to external dependencies, rather than bloating your repositories with them
Ruby
100
star
31

campfire-api

Official API documentation for Campfire
97
star
32

basecamp-classic-api

Official API documentation for Basecamp Classic
87
star
33

lufo

Tracks the most recent options chosen on a `<select>` element and displays them at the top of the list
JavaScript
87
star
34

powprox

Pow .dev sites, meet SSL and HTTP/2
Shell
83
star
35

libmemcached_store

ActiveSupport::Cache wrapper for libmemcached
Ruby
81
star
36

action_profiler

Profile Rails requests on a live app
Ruby
75
star
37

bc3-integrations

Ruby
73
star
38

project_search

Rails plugin that adds a script/find command for searching your project
Ruby
71
star
39

activestorage-office-previewer

Active Storage previewer for Microsoft Office files based on LibreOffice
Ruby
67
star
40

dumpsterfire-2020

Code that runs the dumpster
HTML
47
star
41

turbo-8-morphing-demo

Ruby
43
star
42

cognition

Match text; run commands. Works great for building a chatbot!
Ruby
37
star
43

snapshot

A rails plugin that provides tasks for creating and restoring snapshots of development data.
Ruby
34
star
44

backpack-api

Official API documentation for Backpack
Ruby
20
star
45

ruby-dev

Old Rubies on new Macs
15
star
46

orc

Orc(hestrator) - A really bad pow.cx clone for linux
Shell
10
star
47

cleversafe

Ruby
7
star
48

memcached_bench

Ruby
6
star
49

duo-api

Ruby Gem for communicating with the Duo Api
Ruby
6
star
50

accessibility

Guidelines and tools we use at 37signals to make sure our apps are accessible
5
star
51

Xamarin.iOS.OnePasswordExtension

1Password bindings for Xamarin.iOS
C#
5
star
52

mail

Ruby
4
star
53

composed_of_ipaddr

Compact IPv4 attributes for Active Record. Presents an unsigned int (4 bytes) as an IPAddr.
Ruby
4
star
54

house-style

37signals house style
Ruby
3
star
55

deep_hash_transform

Re-key a nested Hash to all-Symbol or -String keys. Rails 4+ backport.
Ruby
3
star
56

github-issues

Github Issue query CLI
Go
2
star
57

homebrew-dev

Old software to build old stuff on new Macs
Ruby
1
star
58

nsone

A stupid simple API client for NS1
Ruby
1
star