Mission Control — Jobs

repository·main·Indexed 21 days ago

https://github.com/rails/mission_control-jobs

A Rails-based dashboard for managing Active Job queues. It provides a UI to inspect, retry, discard, and monitor jobs across supported adapters such as Resque and Solid Queue. It includes features for HTTP basic authentication, custom base controller integration, and a Rails console API for querying and performing bulk operations on jobs via ActiveJob::JobsRelation.

Tokens
8.4K
Snippets
30
Records
44
Agent score
77%

What's inside mission_control-jobs

  1. Configure multiple apps and adapters (Advanced)

    main

    Mission Control allows managing multiple job backends (different apps or different servers/adapters) from a single centralized UI.

    To support multiple adapters, first add them to the configuration:

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

    Then, use MissionControl::Jobs.applications.add in an initializer to define your apps and their associated servers. The add method expects a mapping where the key is the app name and the value is a hash of servers.

    Each server entry in the hash should be an array where:

    • The first element is a String (the symbolic name for the server in the UI).
    • The second element is an ActiveJob::QueueAdapters::Base instance.

    Supported return formats for server mapping:

    1. [ server_name, adapter_instance ]
    2. [ server_name, [ adapter_instance ] ] (behaves identically to #1)
    3. [ server_name, [ adapter_instance, backtrace_cleaner ] ] (allows overriding the backtrace cleaner for that specific server)
    # Example: Adding a single app with mixed adapters
    queue_adapters_by_name = {
      resque: ActiveJob::QueueAdapters.lookup(:resque).new,
      solid_queue: ActiveJob::QueueAdapters.lookup(:solid_queue).new
    }
    
    MissionControl::Jobs.applications.add("hey", queue_adapters_by_name)
  2. Configure Mission Control for API-only apps or custom asset pipelines

    main

    If you are using an API-only Rails app, vite_rails, or any pipeline other than Sprockets/Propshaft, you must include an asset pipeline to serve the gem's JavaScript and CSS.

    It is recommended to use propshaft:

    1. Add gem "propshaft" to your Gemfile and run bundle install.
    2. Ensure your deployment pipeline runs rails assets:precompile.

    For Docker environments where you might have skipped assets, use SECRET_KEY_BASE_DUMMY=1 to precompile without a master key:

    # Precompiling assets for production without requiring secret RAILS_MASTER_KEY
    RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile
  3. Mount Mission Control — Jobs in your routes

    main

    To make the Mission Control UI accessible, mount the MissionControl::Jobs::Engine in your config/routes.rb file at your preferred path.

    Rails.application.routes.draw do
      # ...
      mount MissionControl::Jobs::Engine, at: "/jobs"
    end
  4. Configure HTTP Basic Authentication

    main

    By default, Mission Control has HTTP basic authentication enabled and closed. You must configure credentials to access the UI.

    Option 1: Using the Generator

    Run the provided generator to set up credentials in your Rails credentials file:

    bin/rails mission_control:jobs:authentication:configure

    To target a specific environment:

    RAILS_ENV=production bin/rails mission_control:jobs:authentication:configure

    Option 2: Manual Configuration

    You can set the user and password directly in your application configuration:

    Rails.application.configure do
      MissionControl::Jobs.http_basic_auth_user = "dev"
      MissionControl::Jobs.http_basic_auth_password = "secret"
    end
  5. Manage jobs via the Rails console

    main

    Mission Control — Jobs provides console helpers to switch between different applications and adapters. This is useful for managing jobs in environments where the UI is insufficient or for performing destructive actions not exposed in the UI.

    To see available job servers and connection instructions, run bin/rails c and type jobs_help.

    >> 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
  6. Configure bulk operation batch delay

    main

    To change the default 2-second delay between batches during bulk operations in the console, configure the delay_between_bulk_operation_batches setting on MissionControl::Jobs.

    MissionControl::Jobs.delay_between_bulk_operation_batches = 5.seconds
  7. Use custom authentication for Mission Control

    main

    If you want to use your application's existing authentication (e.g., an AdminController), you can specify a custom base controller class. When doing this, you should disable the default HTTP Basic Authentication.

    Configure this in application.rb or an environment config:

    Rails.application.configure do
      # Set the base controller
      MissionControl::Jobs.base_controller_class = "AdminController"
      
      # Disable default HTTP Basic Auth
      config.mission_control.jobs.http_basic_auth_enabled = false
    end
    Rails.application.configure do
      MissionControl::Jobs.base_controller_class = "AdminController"
    end
    
    # Or via config object:
    config.mission_control.jobs.base_controller_class = "AdminController"
    config.mission_control.jobs.http_basic_auth_enabled = false
  8. Resque Adapter Extension for Mission Control

    main

    The ActiveJob::QueueAdapters::ResqueExt module extends the standard Resque adapter to provide compatibility with Mission Control — Jobs features. It enables advanced job management capabilities such as queue pausing, job retries, and job discarding directly through the Mission Control interface.

    Key capabilities provided by this extension include:

    • Queue Management: Checking queue status (active/paused) and size, clearing queues, and pausing/resuming queues.
    • Job Operations: Finding, retrying, and discarding individual jobs or batches of jobs.
    • Filtering: Support for filtering jobs by queue_name when they are in a pending state.
  9. Queue Pausing Support in Resque

    main

    Mission Control can pause and resume Resque queues if the ResquePauseHelper is defined in your application. This allows you to temporarily stop processing jobs from a specific queue without removing them.

    To check if your Resque setup supports pausing, you can use the supports_queue_pausing? method. If supported, the following operations are available:

    • pause_queue(queue_name)
    • resume_queue(queue_name)
    • queue_paused?(queue_name)
  10. Capabilities of the Async queue adapter in Mission Control

    main

    When using the Async queue adapter, Mission Control — Jobs operates with limited native capabilities because the Async adapter is an in-memory implementation.

    Key limitations include:

    • Filtering: No filters are supported natively; non-supported filters are performed in-memory.
    • Queue Pausing: The adapter does not support queue pausing (supports_queue_pausing? returns false).
    • Queue Management: queues returns an empty list, and queue_size always returns 0.
    • Job Operations: While methods like retry_all_jobs, discard_all_jobs, and fetch_jobs are defined to satisfy the Mission Control interface, they provide minimal or no functional support for managing a persistent queue.
  11. Query and paginate worker collections with WorkersRelation

    main

    The MissionControl::Jobs::WorkersRelation class provides an interface for querying and filtering collections of workers. It behaves like an Enumerable object, allowing you to iterate over workers, but it is designed to support pagination via limit and offset to avoid loading massive datasets into memory.

    Pagination

    • limit(limit): Sets the maximum number of workers to retrieve.
    • offset(offset): Sets the starting point for the worker collection.

    Performance Warning

    Because WorkersRelation includes Enumerable, you can use methods like map, select, or to_a. However, calling these methods will force the entire relation to load into memory, which can cause significant performance issues if the worker set is large. Always prefer using limit and offset for large datasets.

    # Example of paginating workers
    relation = MissionControl::Jobs::WorkersRelation.new(queue_adapter: my_adapter)
    
    # Get the first 50 workers
    first_batch = relation.limit(50).offset(0)
    
    # Get the next 50 workers
    second_batch = relation.limit(50).offset(50)