[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"project-7893":3},{"id":4,"name":5,"fullName":6,"owner":7,"repo":5,"description":8,"homepage":9,"htmlUrl":10,"language":11,"languages":10,"totalLinesOfCode":10,"stars":12,"forks":13,"watchers":14,"openIssues":15,"contributorsCount":16,"subscribersCount":16,"size":16,"stars1d":16,"stars7d":17,"stars30d":18,"stars90d":16,"forks30d":16,"starsTrendScore":16,"compositeScore":19,"rankGlobal":10,"rankLanguage":10,"license":20,"archived":21,"fork":21,"defaultBranch":22,"hasWiki":21,"hasPages":21,"topics":23,"createdAt":10,"pushedAt":10,"updatedAt":31,"readmeContent":32,"aiSummary":33,"trendingCount":16,"starSnapshotCount":16,"syncStatus":34,"lastSyncTime":35,"discoverSource":36},7893,"good_job","bensheldon\u002Fgood_job","bensheldon","Multithreaded, Postgres-based, Active Job backend for Ruby on Rails.","https:\u002F\u002Fgoodjob-demo.herokuapp.com\u002F",null,"Ruby",2972,243,19,116,0,1,8,29.16,"MIT License",false,"main",[24,25,26,27,28,29,30],"activejob","activejob-backend","hacktoberfest","multithreaded","rails","ruby","ruby-on-rails","2026-06-12 02:01:46","# GoodJob\n\n[![Gem Version](https:\u002F\u002Fbadge.fury.io\u002Frb\u002Fgood_job.svg)](https:\u002F\u002Frubygems.org\u002Fgems\u002Fgood_job)\n[![Test Status](https:\u002F\u002Fgithub.com\u002Fbensheldon\u002Fgood_job\u002Factions\u002Fworkflows\u002Ftest.yml\u002Fbadge.svg?branch=main)](https:\u002F\u002Fgithub.com\u002Fbensheldon\u002Fgood_job\u002Factions\u002Fworkflows\u002Ftest.yml?query=branch%3Amain)\n[![Ruby Toolbox](https:\u002F\u002Fimg.shields.io\u002Fbadge\u002Fdynamic\u002Fjson?color=blue&label=Ruby%20Toolbox&query=%24.projects%5B0%5D.score&url=https%3A%2F%2Fwww.ruby-toolbox.com%2Fapi%2Fprojects%2Fcompare%2Fgood_job&logo=data:image\u002Fsvg+xml;base64,PHN2ZyBhcmlhLWhpZGRlbj0idHJ1ZSIgZm9jdXNhYmxlPSJmYWxzZSIgZGF0YS1wcmVmaXg9ImZhcyIgZGF0YS1pY29uPSJmbGFzayIgY2xhc3M9InN2Zy1pbmxpbmUtLWZhIGZhLWZsYXNrIGZhLXctMTQiIHJvbGU9ImltZyIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB2aWV3Qm94PSIwIDAgNDQ4IDUxMiI+PHBhdGggZmlsbD0id2hpdGUiIGQ9Ik00MzcuMiA0MDMuNUwzMjAgMjE1VjY0aDhjMTMuMyAwIDI0LTEwLjcgMjQtMjRWMjRjMC0xMy4zLTEwLjctMjQtMjQtMjRIMTIwYy0xMy4zIDAtMjQgMTAuNy0yNCAyNHYxNmMwIDEzLjMgMTAuNyAyNCAyNCAyNGg4djE1MUwxMC44IDQwMy41Qy0xOC41IDQ1MC42IDE1LjMgNTEyIDcwLjkgNTEyaDMwNi4yYzU1LjcgMCA4OS40LTYxLjUgNjAuMS0xMDguNXpNMTM3LjkgMzIwbDQ4LjItNzcuNmMzLjctNS4yIDUuOC0xMS42IDUuOC0xOC40VjY0aDY0djE2MGMwIDYuOSAyLjIgMTMuMiA1LjggMTguNGw0OC4yIDc3LjZoLTE3MnoiPjwvcGF0aD48L3N2Zz4=)](https:\u002F\u002Fwww.ruby-toolbox.com\u002Fprojects\u002Fgood_job)\n\nGoodJob is a multithreaded, Postgres-based, Active Job backend for Ruby on Rails.\n\n**Inspired by [Delayed::Job](https:\u002F\u002Fgithub.com\u002Fcollectiveidea\u002Fdelayed_job) and [Que](https:\u002F\u002Fgithub.com\u002Fque-rb\u002Fque), GoodJob is designed for maximum compatibility with Ruby on Rails, Active Job, and Postgres to be simple and performant for most workloads.**\n\n- **Designed for Active Job.** Complete support for [async, queues, delays, priorities, timeouts, and retries](https:\u002F\u002Fedgeguides.rubyonrails.org\u002Factive_job_basics.html) with near-zero configuration.\n- **Built for Rails.** Fully adopts Ruby on Rails [threading and code execution guidelines](https:\u002F\u002Fguides.rubyonrails.org\u002Fthreading_and_code_execution.html) with [Concurrent::Ruby](https:\u002F\u002Fgithub.com\u002Fruby-concurrency\u002Fconcurrent-ruby).\n- **Backed by Postgres.** Relies upon Postgres integrity, session-level Advisory Locks to provide run-once safety and stay within the limits of `schema.rb`, and LISTEN\u002FNOTIFY to reduce queuing latency.\n- **Fully featured.** Includes support for cron-like scheduled jobs, batches, concurrency and throttling controls, and a powerful Web Dashboard (check out the [Demo](https:\u002F\u002Fgoodjob-demo.herokuapp.com\u002F)).\n- **Flexible and lightweight.** Safely runnable within a single existing web process or scaled via an independent CLI process across development, test, and production environments.\n- **For most workloads.** Targets full-stack teams, economy-minded solo developers, and applications that enqueue 1-million jobs\u002Fday and more.\n\nFor more of the story of GoodJob, read the [introductory blog post](https:\u002F\u002Fisland94.org\u002F2020\u002F07\u002Fintroducing-goodjob-1-0).\n\n\u003Cdetails markdown=\"1\">\n\u003Csummary>\u003Cstrong>📊 Comparison of GoodJob with other job queue backends (click to expand)\u003C\u002Fstrong>\u003C\u002Fsummary>\n\n|                 | Queues, priority, retries | Database                              | Concurrency       | Reliability\u002FIntegrity  | Latency                  |\n|-----------------|---------------------------|---------------------------------------|-------------------|------------------------|--------------------------|\n| **GoodJob**     | ✅ Yes                     | ✅ Postgres                            | ✅ Multithreaded   | ✅ ACID, Advisory Locks | ✅ Postgres LISTEN\u002FNOTIFY |\n| **Solid Queue** | ✅ Yes                     | ✅ Postgres and other databases ✨     | 🔶 Multithreaded in forked process   | ✅ ACID, Advisory Locks | 🔶 Polling |\n| **Que**         | ✅ Yes                     | 🔶️ Postgres, requires  `structure.sql` | ✅ Multithreaded   | ✅ ACID, Advisory Locks | ✅ Postgres LISTEN\u002FNOTIFY |\n| **Delayed Job** | ✅ Yes                     | ✅ Postgres                            | 🔴 Single-threaded | ✅ ACID, record-based   | 🔶 Polling                |\n| **Sidekiq**     | ✅ Yes                     | 🔴 Redis                               | ✅ Multithreaded   | 🔴 Crashes lose jobs    | ✅ Redis BRPOP            |\n| **Sidekiq Pro** | ✅ Yes                     | 🔴 Redis                               | ✅ Multithreaded   | ✅ Redis RPOPLPUSH      | ✅ Redis RPOPLPUSH        |\n\n\u003C\u002Fdetails>\n\n## Table of contents\n\n- [Set up](#set-up)\n- [Compatibility](#compatibility)\n- [Configuration](#configuration)\n    - [Command-line options](#command-line-options)\n        - [`good_job start`](#good_job-start)\n        - [`good_job cleanup_preserved_jobs`](#good_job-cleanup_preserved_jobs)\n    - [Configuration options](#configuration-options)\n    - [Global options](#global-options)\n    - [Dashboard](#dashboard)\n        - [API-only Rails applications](#api-only-rails-applications)\n        - [Live polling](#live-polling)\n        - [Extending dashboard views](#extending-dashboard-views)\n    - [Job priority](#job-priority)\n    - [Concurrency controls](#concurrency-controls)\n        - [How concurrency controls work](#how-concurrency-controls-work)\n    - [Cron-style repeating\u002Frecurring jobs](#cron-style-repeatingrecurring-jobs)\n    - [Bulk enqueue](#bulk-enqueue)\n    - [Batches](#batches)\n    - [Updating](#updating)\n        - [Upgrading minor versions](#upgrading-minor-versions)\n        - [Upgrading v3 to v4](#upgrading-v3-to-v4)\n        - [Upgrading v2 to v3](#upgrading-v2-to-v3)\n        - [Upgrading v1 to v2](#upgrading-v1-to-v2)\n- [Go deeper](#go-deeper)\n    - [Exceptions, retries, and reliability](#exceptions-retries-and-reliability)\n        - [Exceptions](#exceptions)\n        - [Retries](#retries)\n        - [Action Mailer retries](#action-mailer-retries)\n        - [Interrupts, graceful shutdown, and SIGKILL](#Interrupts-graceful-shutdown-and-SIGKILL)\n    - [Timeouts](#timeouts)\n    - [Optimize queues, threads, and processes](#optimize-queues-threads-and-processes)\n    - [Database connections](#database-connections)\n    - [Production setup](#production-setup)\n    - [Queue performance with Queue Select Limit](#queue-performance-with-queue-select-limit)\n    - [Execute jobs async \u002F in-process](#execute-jobs-async--in-process)\n    - [Migrate to GoodJob from a different Active Job backend](#migrate-to-goodjob-from-a-different-active-job-backend)\n    - [Monitor and preserve worked jobs](#monitor-and-preserve-worked-jobs)\n    - [Write tests](#write-tests)\n    - [PgBouncer compatibility](#pgbouncer-compatibility)\n    - [CLI HTTP health check probes](#cli-http-health-check-probes)\n    - [Pausing Jobs](#pausing-jobs)\n- [Doing your best job with GoodJob](#doing-your-best-job-with-goodjob)\n    - [Sizing jobs: mice and elephants](#sizing-jobs-mice-and-elephants)\n    - [Isolating by total latency](#isolating-by-total-latency)\n    - [Configuring your queues](#configuring-your-queues)\n    - [Additional observations](#additional-observations)\n- [Contribute](#contribute)\n    - [Gem development](#gem-development)\n        - [Development setup](#development-setup)\n        - [Rails development harness](#rails-development-harness)\n        - [Running tests](#running-tests)\n    - [Release](#release)\n- [License](#license)\n\n## Set up\n\n1. Add `good_job` to your application's Gemfile and install the gem:\n\n    ```sh\n    bundle add good_job\n    ```\n\n1. Run the GoodJob install generator. This will generate a database migration to create a table for GoodJob's job records:\n\n    ```bash\n    bin\u002Frails g good_job:install\n    ```\n\n    Run the migration:\n\n    ```bash\n    bin\u002Frails db:migrate\n    ```\n\n   Optional: If using Rails' multiple databases with the `migrations_paths` configuration option, use the `--database` option:\n\n    ```bash\n    bin\u002Frails g good_job:install --database animals\n    bin\u002Frails db:migrate:animals\n    ```\n\n1. Configure the Active Job adapter:\n\n    ```ruby\n    # config\u002Fapplication.rb or config\u002Fenvironments\u002F{RAILS_ENV}.rb\n    config.active_job.queue_adapter = :good_job\n    ```\n\n1. Inside of your application, queue your job 🎉:\n\n    ```ruby\n    YourJob.perform_later\n    ```\n\n    GoodJob supports all Active Job features:\n\n    ```ruby\n    YourJob.set(queue: :some_queue, wait: 5.minutes, priority: 10).perform_later\n    ```\n\n1. **In Rails' development environment**, by default, GoodJob's Adapter executes jobs `async` in a background thread pool in `rails server`.\n    - Because of Rails deferred autoloading, jobs enqueued via the `rails console` may not begin executing on a separate server process until the Rails application is fully initialized by loading a web page once. To force early initialization (so pre-existing jobs run immediately on boot), add the following to an initializer:\n\n        ```ruby\n        # config\u002Finitializers\u002Fgood_job.rb\n        Rails.configuration.after_initialize do\n          ActiveJob::Base && ActiveRecord::Base\n        end\n        ```\n\n    - Remember, only Active Job's `perform_later` sends jobs to the queue adapter; Active Job's `perform_now` executes the job immediately and does not invoke the queue adapter. GoodJob is not involved in `perform_now` jobs.\n1. **In Rails' test environment**, by default, GoodJob's Adapter executes jobs `inline` immediately in the current thread.\n    - Future-scheduled jobs can be executed with `GoodJob.perform_inline` using a tool like Timecop or `ActiveSupport::Testing::TimeHelpers`.\n    - Note that Active Job's TestAdapter, which powers test helpers (e.g. `assert_enqueued_with`), may override GoodJob's Adapter in [some configurations](https:\u002F\u002Fgithub.com\u002Frails\u002Frails\u002Fissues\u002F37270).\n1. **In Rails' production environment**, by default, GoodJob's Adapter enqueues jobs in `external` mode to be executed by a separate execution process:\n    - By default, GoodJob separates job enqueuing from job execution so that jobs can be scaled independently of the web server.  Use the GoodJob command-line tool to execute jobs:\n\n        ```bash\n        bundle exec good_job start\n        ```\n\n        Ideally the command-line tool should be run on a separate machine or container from the web process. For example, on Heroku:\n\n        ```Procfile\n        web: rails server\n        worker: bundle exec good_job start\n        ```\n\n        The command-line tool supports a variety of options, see the reference below for command-line configuration.\n\n    - GoodJob can also be configured to execute jobs within the web server process to save on resources. This is useful for low-workloads when economy is paramount.\n\n        ```bash\n        GOOD_JOB_EXECUTION_MODE=async rails server\n        ```\n\n        Additional configuration is likely necessary, see the reference below for configuration.\n\n## Compatibility\n\n- **Ruby on Rails:** 6.1+\n- **Ruby:** Ruby 3.0+. JRuby 9.4+\n- **Postgres:** 10.0+\n\n## Configuration\n\n### Command-line options\n\nThere are several top-level commands available through the `good_job` command-line tool.\n\nConfiguration options are available with `help`.\n\n#### `good_job start`\n\n`good_job start` executes queued jobs.\n\n```bash\n$ bundle exec good_job help start\n\nUsage:\n  good_job start\n\nOptions:\n  [--queues=QUEUE_LIST]           # Queues or pools to work from. (env var: GOOD_JOB_QUEUES, default: *)\n  [--max-threads=COUNT]           # Default number of threads per pool to use for working jobs. (env var: GOOD_JOB_MAX_THREADS, default: 5)\n  [--poll-interval=SECONDS]       # Interval between polls for available jobs in seconds (env var: GOOD_JOB_POLL_INTERVAL, default: 10)\n  [--max-cache=COUNT]             # Maximum number of scheduled jobs to cache in memory (env var: GOOD_JOB_MAX_CACHE, default: 10000)\n  [--shutdown-timeout=SECONDS]    # Number of seconds to wait for jobs to finish when shutting down before stopping the thread. (env var: GOOD_JOB_SHUTDOWN_TIMEOUT, default: -1 (forever))\n  [--enable-cron]                 # Whether to run cron process (default: false)\n  [--enable-listen-notify]        # Whether to enqueue and read jobs with Postgres LISTEN\u002FNOTIFY (default: true)\n  [--idle-timeout=SECONDS]        # Exit process when no jobs have been performed for this many seconds (env var: GOOD_JOB_IDLE_TIMEOUT, default: nil)\n  [--daemonize]                   # Run as a background daemon (default: false)\n  [--pidfile=PIDFILE]             # Path to write daemonized Process ID (env var: GOOD_JOB_PIDFILE, default: tmp\u002Fpids\u002Fgood_job.pid)\n  [--probe-port=PORT]             # Port for http health check (env var: GOOD_JOB_PROBE_PORT, default: nil)\n  [--probe-handler=PROBE_HANDLER] # Use 'webrick' to use WEBrick to handle probe server requests which is Rack compliant, otherwise default server that is not Rack compliant is used.\n  [--queue-select-limit=COUNT]    # The number of queued jobs to select when polling for a job to run. (env var: GOOD_JOB_QUEUE_SELECT_LIMIT, default: nil)\"\n\nExecutes queued jobs.\n\nAll options can be configured with environment variables.\nSee option descriptions for the matching environment variable name.\n\n== Configuring queues\n\nSeparate multiple queues with commas; exclude queues with a leading minus;\nseparate isolated execution pools with semicolons and threads with colons.\n```\n\n#### `good_job cleanup_preserved_jobs`\n\n`good_job cleanup_preserved_jobs` destroys preserved job records. See `GoodJob.preserve_job_records` for when this command is useful.\n\n```bash\n$ bundle exec good_job help cleanup_preserved_jobs\n\nUsage:\n  good_job cleanup_preserved_jobs\n\nOptions:\n  [--before-seconds-ago=SECONDS] # Destroy records finished more than this many seconds ago (env var:  GOOD_JOB_CLEANUP_PRESERVED_JOBS_BEFORE_SECONDS_AGO, default: 1209600 (14 days))\n\nManually destroys preserved job records.\n\nBy default, GoodJob automatically destroys job records when the job is performed\nand this command is not required to be used.\n```\n\n### Configuration options\n\nActive Job configuration depends on where the code is placed:\n\n- `config.active_job.queue_adapter = :good_job` within `config\u002Fapplication.rb` or `config\u002Fenvironments\u002F*.rb`.\n- `ActiveJob::Base.queue_adapter = :good_job` within an initializer (e.g. `config\u002Finitializers\u002Factive_job.rb`).\n\nGoodJob configuration can be placed within Rails `config` directory for all environments (`config\u002Fapplication.rb`), within a particular environment (e.g. `config\u002Fenvironments\u002Fdevelopment.rb`), or within an initializer (e.g. `config\u002Finitializers\u002Fgood_job.rb`).\n\nConfiguration examples:\n\n```ruby\n# config\u002Finitializers\u002Fgood_job.rb OR config\u002Fapplication.rb OR config\u002Fenvironments\u002F{RAILS_ENV}.rb\n\nRails.application.configure do\n  # Configure options individually...\n  config.good_job.preserve_job_records = true\n  config.good_job.retry_on_unhandled_error = false\n  config.good_job.on_thread_error = -> (exception) { Rails.error.report(exception) }\n  config.good_job.execution_mode = :async\n  config.good_job.queues = '*'\n  config.good_job.max_threads = 5\n  config.good_job.poll_interval = 30 # seconds\n  config.good_job.shutdown_timeout = 25 # seconds\n  config.good_job.enable_cron = true\n  config.good_job.cron = { example: { cron: '0 * * * *', class: 'ExampleJob'  } }\n  config.good_job.cron_graceful_restart_period = 5.minutes\n  config.good_job.dashboard_default_locale = :en\n\n  # ...or all at once.\n  config.good_job = {\n    preserve_job_records: true,\n    retry_on_unhandled_error: false,\n    on_thread_error: -> (exception) { Rails.error.report(exception) },\n    execution_mode: :async,\n    queues: '*',\n    max_threads: 5,\n    poll_interval: 30,\n    shutdown_timeout: 25,\n    enable_cron: true,\n    cron: {\n      example: {\n        cron: '0 * * * *',\n        class: 'ExampleJob'\n      },\n    },\n    dashboard_default_locale: :en,\n  }\nend\n```\n\nAvailable configuration options are:\n\n- `execution_mode` (symbol) specifies how and where jobs should be executed. You can also set this with the environment variable `GOOD_JOB_EXECUTION_MODE`. It can be any one of:\n    - `:inline` executes jobs immediately in whatever process queued them (usually the web server process). This should only be used in test and development environments.\n    - `:external` causes the adapter to enqueue jobs, but not execute them. When using this option (the default for production environments), you’ll need to use the command-line tool to actually execute your jobs.\n    - `:async` (or `:async_server`) executes jobs in separate threads within the Rails web server process (`bundle exec rails server`). It can be more economical for small workloads because you don’t need a separate machine or environment for running your jobs, but if your web server is under heavy load or your jobs require a lot of resources, you should choose `:external` instead.  When not in the Rails web server, jobs will execute in `:external` mode to ensure jobs are not executed within `rails console`, `rails db:migrate`, `rails assets:prepare`, etc.\n    - `:async_all` executes jobs in separate threads in _any_ Rails process.\n- `queues` (string) sets queues or pools to execute jobs. You can also set this with the environment variable `GOOD_JOB_QUEUES`.\n- `max_threads` (integer) sets the default number of threads per pool to use for working jobs. You can also set this with the environment variable `GOOD_JOB_MAX_THREADS`.\n- `poll_interval` (integer) sets the number of seconds between polls for jobs when `execution_mode` is set to `:async`. You can also set this with the environment variable `GOOD_JOB_POLL_INTERVAL`. A poll interval of `-1` disables polling completely.\n    - production default: 10 seconds (in case of a LISTEN\u002FNOTIFY blip)\n    - development default: -1, disabled (because the application is likely being restarted often and won't be running unobserved). You can enable it by setting a `poll_interval`.\n    - LISTEN\u002FNOTIFY is enabled in both production and development, so polling is not strictly necessary.\n    - If LISTEN\u002FNOTIFY is disabled, you should configure polling for future-scheduled jobs. GoodJob will cache in memory the scheduled time and check for executable jobs at that time. If the cache is exceeded (10k scheduled jobs by default) that's another reason to poll just in case.\n- `max_cache` (integer) sets the maximum number of scheduled jobs that will be stored in memory to reduce execution latency when also polling for scheduled jobs. Caching 10,000 scheduled jobs uses approximately 20MB of memory. You can also set this with the environment variable `GOOD_JOB_MAX_CACHE`.\n- `shutdown_timeout` (integer) number of seconds to wait for jobs to finish when shutting down before stopping the thread. Defaults to forever: `-1`. You can also set this with the environment variable `GOOD_JOB_SHUTDOWN_TIMEOUT`.\n- `enable_cron` (boolean) whether to run cron process. Defaults to `false`. You can also set this with the environment variable `GOOD_JOB_ENABLE_CRON`.\n- `cron_graceful_restart_period` (integer) when restarting cron, attempt to re-enqueue jobs that would have been enqueued by cron within this time period (e.g. `1.minute`). This should match the expected downtime during deploys.\n- `enable_listen_notify` (boolean) whether to enqueue and read jobs with Postgres LISTEN\u002FNOTIFY. Defaults to `true`. You can also set this with the environment variable `GOOD_JOB_ENABLE_LISTEN_NOTIFY`.\n- `cron` (hash) cron configuration. Defaults to `{}`. You can also set this as a JSON string with the environment variable `GOOD_JOB_CRON`\n- `cleanup_discarded_jobs` (boolean) whether to destroy discarded jobs when cleaning up preserved jobs using the `$ good_job cleanup_preserved_jobs` CLI command or calling `GoodJob.cleanup_preserved_jobs`. Defaults to `true`. Can also be set with the environment variable `GOOD_JOB_CLEANUP_DISCARDED_JOBS`.\n- `cleanup_preserved_jobs_before_seconds_ago` (integer) number of seconds to preserve jobs when using the `$ good_job cleanup_preserved_jobs` CLI command or calling `GoodJob.cleanup_preserved_jobs`. Defaults to `1209600` (14 days). Can also be set with  the environment variable `GOOD_JOB_CLEANUP_PRESERVED_JOBS_BEFORE_SECONDS_AGO`.\n- `cleanup_interval_jobs` (integer) Number of jobs a Scheduler will execute before cleaning up preserved jobs. Defaults to `1000`. Disable with `false`. Can also be set with  the environment variable `GOOD_JOB_CLEANUP_INTERVAL_JOBS` and disabled with `0`).\n- `cleanup_interval_seconds` (integer) Number of seconds a Scheduler will wait before cleaning up preserved jobs. Defaults to `600` (10 minutes). Disable with `false`. Can also be set with  the environment variable `GOOD_JOB_CLEANUP_INTERVAL_SECONDS` and disabled with `0`).\n- `inline_execution_respects_schedule` (boolean) Opt-in to future behavior of inline execution respecting scheduled jobs. Defaults to `false`.\n- `logger` ([Rails Logger](https:\u002F\u002Fapi.rubyonrails.org\u002Fclasses\u002FActiveSupport\u002FLogger.html)) lets you set a custom logger for GoodJob. It should be an instance of a Rails `Logger` (Default: `Rails.logger`).\n- `preserve_job_records` (boolean, symbol, or lambda) keeps job records in your database even after jobs are completed. If set to `true`, all job records are preserved. If set to `:on_unhandled_error`, only jobs that finished with an unhandled error are preserved. If set to a lambda, the lambda will be called with the error_event (e.g., `:discarded`, `:retry_stopped`, or `:unhandled`) and should return a boolean indicating whether to preserve the job. (Default: `true`)\n- `advisory_lock_heartbeat` (boolean) whether to use an advisory lock for the purpose of determining whether an execeution process is active. (Default `true` in Development; `false` in other environments)\n- `retry_on_unhandled_error` (boolean) causes jobs to be re-queued and retried if they raise an instance of `StandardError`. Be advised this may lead to jobs being repeated infinitely ([see below for more on retries](#retries)). Instances of `Exception`, like SIGINT, will *always* be retried, regardless of this attribute’s value. (Default: `false`)\n- `on_thread_error` (proc, lambda, or callable) will be called when there is an Exception. It can be useful for logging errors to bug tracking services, like Sentry or Airbrake. Example:\n\n    ```ruby\n    config.good_job.on_thread_error = -> (exception) { Rails.error.report(exception) }\n    ```\n\n- `probe_server_app` (Rack application) allows you to specify a Rack application to be used for the probe server. Defaults to `nil` which uses the default probe server. Example:\n\n    ```ruby\n    config.good_job.probe_app = -> (env) { [200, {}, [\"OK\"]] }\n    ```\n\n- `probe_handler` (string) allows you to use WEBrick, a fully Rack compliant webserver instead of the simple default server. **Note:** You'll need to ensure WEBrick is in your load path as GoodJob doesn't have WEBrick as a dependency. Example:\n\n    ```ruby\n    config.good_job.probe_handler = 'webrick'\n    ```\n\n- `enable_pauses` (boolean) whether job processing can be paused. Defaults to `false`. You can also set this with the environment variable `GOOD_JOB_ENABLE_PAUSES`.\n\nBy default, GoodJob configures the following execution modes per environment:\n\n```ruby\n\n# config\u002Fenvironments\u002Fdevelopment.rb\nconfig.active_job.queue_adapter = :good_job\nconfig.good_job.execution_mode = :async\n\n# config\u002Fenvironments\u002Ftest.rb\nconfig.active_job.queue_adapter = :good_job\nconfig.good_job.execution_mode = :inline\n\n# config\u002Fenvironments\u002Fproduction.rb\nconfig.active_job.queue_adapter = :good_job\nconfig.good_job.execution_mode = :external\n```\n\n### Global options\n\nGood Job’s general behavior can also be configured via attributes directly on the `GoodJob` module:\n\n- **`GoodJob.configure_active_record { ... }`** Inject Active Record configuration into GoodJob's base model, for example, when using [multiple databases with Active Record](https:\u002F\u002Fguides.rubyonrails.org\u002Factive_record_multiple_databases.html) or when other custom configuration is necessary for the Active Record model to connect to the Postgres database. Example:\n\n    ```ruby\n    # config\u002Finitializers\u002Fgood_job.rb\n    GoodJob.configure_active_record do\n      connects_to database: { writing: :special_database }\n      self.table_name_prefix = \"special_application_\"\n    end\n    ```\n\n- **`GoodJob.active_record_parent_class`** (string) Alternatively, modify the Active Record parent class inherited by GoodJob's Active Record model `GoodJob::Job` (defaults to `\"ActiveRecord::Base\"`). Configure this _The value must be a String to avoid premature initialization of Active Record._\n\nYou’ll generally want to configure these in `config\u002Finitializers\u002Fgood_job.rb`, like so:\n\n```ruby\n# config\u002Finitializers\u002Fgood_job.rb\nGoodJob.active_record_parent_class = \"ApplicationRecord\"\n```\n\nThe following options are also configurable via accessors, but you are encouraged to use the configuration attributes instead because these may be deprecated and removed in the future:\n\n- **`GoodJob.logger`** ([Rails Logger](https:\u002F\u002Fapi.rubyonrails.org\u002Fclasses\u002FActiveSupport\u002FLogger.html)) lets you set a custom logger for GoodJob. It should be an instance of a Rails `Logger`.\n- **`GoodJob.preserve_job_records`** (boolean, symbol, or lambda) keeps job records in your database even after jobs are completed. If set to `true`, all job records are preserved. If set to `:on_unhandled_error`, only jobs that finished with an unhandled error are preserved. If set to a lambda, the lambda will be called with Active Job instance, and if it exists, the exception the error_event (e.g., `:discarded`, `:retry_stopped`, or `:unhandled`) and should return a boolean indicating whether to preserve the job (e.g. `-> (active_job, error, error_event) { !(active_job.is_a(Turbo::Streams::BroadcastStreamJob) || error_event == :retry_stopped`). (Default: `true`)\n- **`GoodJob.retry_on_unhandled_error`** (boolean) causes jobs to be re-queued and retried if they raise an instance of `StandardError`. Be advised this may lead to jobs being repeated infinitely ([see below for more on retries](#retries)). Instances of `Exception`, like SIGINT, will *always* be retried, regardless of this attribute’s value. (Default: `false`)\n- **`GoodJob.on_thread_error`** (proc, lambda, or callable) will be called when there is an Exception. It can be useful for logging errors to bug tracking services, like Sentry or Airbrake.\n\n### Dashboard\n\n![Dashboard UI](https:\u002F\u002Fgithub.com\u002Fbensheldon\u002Fgood_job\u002Fraw\u002Fmain\u002FSCREENSHOT.png)\n\n_🚧 GoodJob's dashboard is a work in progress. Please contribute ideas and code on [Github](https:\u002F\u002Fgithub.com\u002Fbensheldon\u002Fgood_job\u002Fissues)._\n\nGoodJob includes a Dashboard as a mountable `Rails::Engine`.\n\n1. Mount the engine in your `config\u002Froutes.rb` file. The following will mount it at `http:\u002F\u002Fexample.com\u002Fgood_job`.\n\n    ```ruby\n    # config\u002Froutes.rb\n    # ...\n    mount GoodJob::Engine => 'good_job'\n    ```\n\n1. Configure authentication. Because jobs can potentially contain sensitive information, you should authorize access. For example, using Devise's `authenticate` helper, that might look like:\n\n    ```ruby\n    # config\u002Froutes.rb\n    # ...\n    authenticate :user, ->(user) { user.admin? } do\n      mount GoodJob::Engine => 'good_job'\n    end\n    ```\n\n    Another option is using basic auth like this:\n\n    ```ruby\n    # config\u002Finitializers\u002Fgood_job.rb\n    GoodJob::Engine.middleware.use(Rack::Auth::Basic) do |username, password|\n      ActiveSupport::SecurityUtils.secure_compare(Rails.application.credentials.good_job_username, username) &\n        ActiveSupport::SecurityUtils.secure_compare(Rails.application.credentials.good_job_password, password)\n    end\n    ```\n\n    To support custom authentication, you can extend GoodJob's `ApplicationController` using the following hook:\n\n    ```ruby\n    # config\u002Finitializers\u002Fgood_job.rb\n\n    ActiveSupport.on_load(:good_job_application_controller) do\n      # context here is GoodJob::ApplicationController\n\n      before_action do\n        raise ActionController::RoutingError.new('Not Found') unless current_user&.admin?\n      end\n\n      def current_user\n        # load current user\n      end\n    end\n    ```\n\n_To view finished jobs (succeeded and discarded) on the Dashboard, GoodJob must be configured to preserve job records. Preservation is enabled by default._\n\n**Troubleshooting the Dashboard:** Some applications are unable to autoload the Goodjob Engine. To work around this, explicitly require the Engine at the top of your `config\u002Fapplication.rb` file, immediately after Rails is required and before Bundler requires the Rails' groups.\n\n```ruby\n# config\u002Fapplication.rb\nrequire_relative 'boot'\nrequire 'rails\u002Fall'\nrequire 'good_job\u002Fengine' # \u003C= Add this line\n# ...\n```\n\n#### API-only Rails applications\n\nAPI-only Rails applications may not have all of the required Rack middleware for the GoodJob Dashboard to function. To re-add the middleware:\n\n```ruby\n# config\u002Fapplication.rb\nmodule MyApp\n  class Application \u003C Rails::Application\n    #...\n    config.middleware.use Rack::MethodOverride\n    config.middleware.use ActionDispatch::Flash\n    config.middleware.use ActionDispatch::Cookies\n    config.middleware.use ActionDispatch::Session::CookieStore\n  end\nend\n```\n\n#### Live polling\n\nThe Dashboard can be set to automatically refresh by checking \"Live Poll\" in the Dashboard header, or by setting `?poll=10` with the interval in seconds (default 30 seconds).\n\n#### Extending dashboard views\n\nGoodJob exposes some views that are intended to be overridden by placing views in your application:\n\n- [`app\u002Fviews\u002Fgood_job\u002F_custom_head.html.erb`](app\u002Fviews\u002Fgood_job\u002F_custom_head.html.erb): content added to this partial will be added at the end of the `\u003Chead>` tag in all GoodJob views. This is ideal for injecting custom scripts or styles.\n- [`app\u002Fviews\u002Fgood_job\u002F_custom_job_details.html.erb`](app\u002Fviews\u002Fgood_job\u002F_custom_job_details.html.erb): content added to this partial will be displayed above the argument list on the good_job\u002Fjobs#show page.\n- [`app\u002Fviews\u002Fgood_job\u002F_custom_execution_details.html.erb`](app\u002Fviews\u002Fgood_job\u002F_custom_execution_details.html.erb): content added to this partial will be displayed above each execution on the good_job\u002Fjobs#show page.\n\n**Warning:** these partials expose classes (such as `GoodJob::Job`) that are considered internal implementation details of GoodJob. You should always test your custom partials after upgrading GoodJob.\n\nFor example, if your app deals with widgets and you want to show a link to the widget a job acted on, you can add the following to `app\u002Fviews\u002Fgood_job\u002F_custom_job_details.html.erb`:\n\n```erb\n\u003C%# file: app\u002Fviews\u002Fgood_job\u002F_custom_job_details.html.erb %>\n\u003C% arguments = job.active_job.arguments rescue [] %>\n\u003C% widgets = arguments.select { |arg| arg.is_a?(Widget) } %>\n\u003C% if widgets.any? %>\n  \u003Cdiv class=\"my-4\">\n    \u003Ch5>Widgets\u003C\u002Fh5>\n    \u003Cul>\n      \u003C% widgets.each do |widget| %>\n        \u003Cli>\u003C%= link_to widget.name, main_app.widget_url(widget) %>\u003C\u002Fli>\n      \u003C% end %>\n    \u003C\u002Ful>\n  \u003C\u002Fdiv>\n\u003C% end %>\n```\n\nAs a second example, you may wish to show a link to a log aggregator next to each job execution. You can do this by adding the following to `app\u002Fviews\u002Fgood_job\u002F_custom_execution_details.html.erb`:\n\n```erb\n\u003C%# file: app\u002Fviews\u002Fgood_job\u002F_custom_execution_details.html.erb %>\n\u003Cdiv class=\"py-3\">\n  \u003C%= link_to \"Logs\", main_app.logs_url(filter: { job_id: job.id }, start_time: execution.performed_at, end_time: execution.finished_at + 1.minute) %>\n\u003C\u002Fdiv>\n```\n\n### Job priority\n\nSmaller `priority` values have higher priority and run first (default: `0`), in accordance with [Active Job's definition of priority](https:\u002F\u002Fgithub.com\u002Frails\u002Frails\u002Fblob\u002Fe17faead4f2aff28da079d50f02ea5b015322d5b\u002Factivejob\u002Flib\u002Factive_job\u002Fcore.rb#L22).\n\nPrior to GoodJob v4, this was reversed: higher priority numbers ran first in all versions of GoodJob v3.x and below. When migrating from v3 to v4, new behavior can be opted into by setting `config.good_job.smaller_number_is_higher_priority = true` in your GoodJob initializer or `application.rb`.\n\n### Labelled jobs\n\nLabels are the recommended way to add context or metadata to specific jobs. For example, all jobs that have a dependency on an email service could be labeled `email`. Using labels requires adding the Active Job extension `GoodJob::ActiveJobExtensions::Labels` to your job class.\n\n```ruby\nclass ApplicationJob \u003C ActiveJob::Base\n  include GoodJob::ActiveJobExtensions::Labels\nend\n\n# Add a default label to every job within the class\nclass WelcomeJob \u003C ApplicationJob\n  self.good_job_labels = [\"email\"]\n\n  def perform\n    # Labels can be inspected from within the job\n    puts good_job_labels # => [\"email\"]\n  end\nend\n\n# Or add to individual jobs when enqueued\nWelcomeJob.set(good_job_labels: [\"email\"]).perform_later\n```\n\nLabels can be used to search jobs in the Dashboard. For example, to find all jobs labeled `email`, search for `email`.\n\n### Concurrency controls\n\nGoodJob can extend Active Job to provide limits on concurrently running jobs, either at time of _enqueue_ or at _perform_. Limiting concurrency can help prevent duplicate, double or unnecessary jobs from being enqueued, or race conditions when performing, for example when interacting with 3rd-party APIs.\n\n```ruby\nclass MyJob \u003C ApplicationJob\n  include GoodJob::ActiveJobExtensions::Concurrency\n\n  # Define one or more concurrency rules. Each rule is scoped to a label,\n  # which is a value derived from the job's arguments at enqueue time and\n  # stored on the job record. Jobs must be enqueued with the matching label\n  # via `good_job_labels:` for the rule to apply.\n  #\n  # Multiple rules can be defined; they are evaluated in order and the first\n  # exceeded rule short-circuits the rest.\n  good_job_concurrency_rule(\n    # A label that scopes this rule. Can be a static String or a Lambda\u002FProc\n    # invoked in the context of the job instance. The rule only applies to jobs\n    # that were enqueued with this label in `good_job_labels`.\n    label: -> { arguments.first[:user_id] },\n\n    # Maximum number of unfinished jobs with this label to allow.\n    # Can be an Integer or Lambda\u002FProc invoked in the context of the job.\n    total_limit: 1,\n\n    # Or, if more control is needed:\n    # Maximum number of jobs with this label to be concurrently enqueued\n    # (excludes performing jobs). Can be an Integer or Lambda\u002FProc.\n    enqueue_limit: 2,\n\n    # Maximum number of jobs with this label to be concurrently performed\n    # (excludes enqueued jobs). Can be an Integer or Lambda\u002FProc.\n    perform_limit: 1,\n\n    # Maximum number of jobs with this label to be enqueued within the time\n    # period, looking backwards from now. Must be [count, period].\n    enqueue_throttle: [10, 1.minute],\n\n    # Maximum number of jobs with this label to be performed within the time\n    # period, looking backwards from now. Must be [count, period].\n    perform_throttle: [100, 1.hour],\n\n    # Note: Under heavy load, the total number of jobs may exceed the\n    # sum of `enqueue_limit` and `perform_limit` because of race conditions\n    # caused by imperfectly disjunctive states. If you need to constrain\n    # the total number of jobs, use `total_limit` instead. See #378.\n  )\n  # Additional rules\n  good_job_concurrency_rule(...)\n  good_job_concurrency_rule(...)\n\n  def perform(user_id:)\n    # do work\n  end\nend\n```\n\nJobs must be enqueued with the matching label for rules to take effect:\n\n```ruby\nMyJob.set(good_job_labels: [current_user.id]).perform_later(user_id: current_user.id)\n```\n\n#### How concurrency controls work\n\nGoodJob's concurrency control strategy for `perform_limit` is \"optimistic retry with an incremental backoff\".  The [code is readable](https:\u002F\u002Fgithub.com\u002Fbensheldon\u002Fgood_job\u002Fblob\u002Fmain\u002Flib\u002Fgood_job\u002Factive_job_extensions\u002Fconcurrency.rb).\n\n- \"Optimistic\" meaning that the implementation's performance trade-off assumes that collisions are atypical (e.g. two users enqueue the same job at the same time) rather than regular (e.g. the system enqueues thousands of colliding jobs at the same time). Depending on your concurrency requirements, you may also want to manage concurrency through the number of GoodJob threads and processes that are performing a given queue.\n- \"Retry with an incremental backoff\" means that when `perform_limit` is exceeded, the job will raise a `GoodJob::ActiveJobExtensions::Concurrency::ConcurrencyExceededError` which is caught by a `retry_on` handler which re-schedules the job to execute in the near future with an incremental backoff.\n- First-in-first-out job execution order is not preserved when a job is retried with incremental back-off.\n- For pessimistic usecases that collisions are expected, use number of threads\u002Fprocesses (e.g., `good_job --queues \"serial:1;-serial:5\"`) to control concurrency. It is also a good idea to use `perform_limit` as backstop.\n\n#### Legacy: `good_job_control_concurrency_with`\n\nThe original concurrency interface uses a single configuration hash and scopes limits to a concurrency _key_ (a string derived from the job) stored on the job record, rather than a label. It remains fully supported.\n\n```ruby\nclass MyJob \u003C ApplicationJob\n  include GoodJob::ActiveJobExtensions::Concurrency\n\n  good_job_control_concurrency_with(\n    # Maximum number of unfinished jobs to allow with the concurrency key\n    # Can be an Integer or Lambda\u002FProc that is invoked in the context of the job\n    total_limit: 1,\n\n    # Or, if more control is needed:\n    # Maximum number of jobs with the concurrency key to be\n    # concurrently enqueued (excludes performing jobs)\n    # Can be an Integer or Lambda\u002FProc that is invoked in the context of the job\n    enqueue_limit: 2,\n\n    # Maximum number of jobs with the concurrency key to be\n    # concurrently performed (excludes enqueued jobs)\n    # Can be an Integer or Lambda\u002FProc that is invoked in the context of the job\n    perform_limit: 1,\n\n    # Maximum number of jobs with the concurrency key to be enqueued within\n    # the time period, looking backwards from the current time. Must be an array\n    # with two elements: the number of jobs and the time period.\n    enqueue_throttle: [10, 1.minute],\n\n    # Maximum number of jobs with the concurrency key to be performed within\n    # the time period, looking backwards from the current time. Must be an array\n    # with two elements: the number of jobs and the time period.\n    perform_throttle: [100, 1.hour],\n\n    # A unique key to be globally locked against.\n    # Can be String or Lambda\u002FProc that is invoked in the context of the job.\n    #\n    # If a key is not provided GoodJob will use the job class name.\n    #\n    # To disable concurrency control, for example in a subclass, set the\n    # key explicitly to nil (e.g. `key: nil` or `key: -> { nil }`)\n    #\n    # If you provide a custom concurrency key (for example, if concurrency is supposed\n    # to be controlled by the first job argument) make sure that it is sufficiently unique across\n    # jobs and queues by adding the job class or queue to the key yourself, if needed.\n    #\n    # Note: When using a model instance as part of your custom concurrency key, make sure\n    # to explicitly use its `id` or `to_global_id` because otherwise it will not stringify as expected.\n    #\n    # Note: Arguments passed to #perform_later can be accessed through Active Job's `arguments` method\n    # which is an array containing positional arguments and, optionally, a kwarg hash.\n    key: -> { \"#{self.class.name}-#{queue_name}-#{arguments.first}-#{arguments.last[:version]}\" } #  MyJob.perform_later(\"Alice\", version: 'v2') => \"MyJob-default-Alice-v2\"\n  )\n\n  def perform(first_name, version:)\n    # do work\n  end\nend\n```\n\nWhen testing, the resulting concurrency key value can be inspected:\n\n```ruby\njob = MyJob.perform_later(\"Alice\", version: 'v1')\njob.good_job_concurrency_key #=> \"MyJob-default-Alice-v1\"\n```\n\n### Cron-style repeating\u002Frecurring jobs\n\nGoodJob can enqueue Active Job jobs on a recurring basis that can be used as a replacement for cron.\n\nCron-style jobs can be enequeued by any GoodJob process (e.g., CLI or `:async` execution mode) that has `config.good_job.enable_cron` set to `true`. Enabling cron on multiple processes will not enqueue duplicate jobs; GoodJob's cron uses unique indexes to ensure that only a single job is enqueued for a given time interval.  In order for this to work, GoodJob must preserve cron-created job records; these records will be automatically deleted like any other preserved record.\n\nCron-format is parsed by the [`fugit`](https:\u002F\u002Fgithub.com\u002Ffloraison\u002Ffugit) gem, which has support for seconds-level resolution (e.g. `* * * * * *`) and natural language parsing (e.g. `every second`).\n\nIf you use the [Dashboard](#dashboard) the scheduled tasks can be viewed in the 'cron' menu. In this view you can also disable a task or run\u002Fenqueue a task immediately.\n\n```ruby\n# config\u002Fenvironments\u002Fapplication.rb or a specific environment e.g. production.rb\n\n# Enable cron enqueuing in this process\nconfig.good_job.enable_cron = true\n\n# Without zero-downtime deploys, re-attempt previous schedules after a deploy\nconfig.good_job.cron_graceful_restart_period = 1.minute\n\n# Configure cron with a hash that has a unique key for each recurring job\nconfig.good_job.cron = {\n  # Every 15 minutes, enqueue `ExampleJob.set(priority: -10).perform_later(42, \"life\", name: \"Alice\")`\n  frequent_task: { # each recurring job must have a unique key\n    cron: \"*\u002F15 * * * *\", # cron-style scheduling format by fugit gem\n    class: \"ExampleJob\", # name of the job class as a String; must reference an Active Job job class\n    args: [42, \"life\"], # positional arguments to pass to the job; can also be a proc e.g. `-> { [Time.now] }`\n    kwargs: { name: \"Alice\" }, # keyword arguments to pass to the job; can also be a proc e.g. `-> { { name: NAMES.sample } }`\n    set: { priority: -10 }, # additional Active Job properties; can also be a lambda\u002Fproc e.g. `-> { { priority: [1,2].sample } }`\n    description: \"Something helpful\", # optional description that appears in Dashboard\n  },\n  production_task: {\n    cron: \"0 0,12 * * *\",\n    class: \"ProductionJob\",\n    enabled_by_default: -> { Rails.env.production? } # Only enable in production, otherwise can be enabled manually through Dashboard\n  },\n  complex_schedule: {\n    class: \"ComplexScheduleJob\",\n    cron: -> (last_ran) { (last_ran.blank? ? Time.now : last_ran + 14.hours).at_beginning_of_minute }\n  }\n  # etc.\n}\n```\n\n### Bulk enqueue\n\nGoodJob's Bulk-enqueue functionality can buffer and enqueue multiple jobs at once, using a single INSERT statement. This can more performant when enqueuing a large number of jobs.\n\n```ruby\n# Capture jobs using `.perform_later`:\nactive_jobs = GoodJob::Bulk.enqueue do\n  MyJob.perform_later\n  AnotherJob.perform_later\n  # If an exception is raised within this block, no jobs will be inserted.\nend\n\n# All Active Job instances are returned from GoodJob::Bulk.enqueue.\n# Jobs that have been successfully enqueued have a `provider_job_id` set.\nactive_jobs.all?(&:provider_job_id)\n\n# Bulk enqueue Active Job instances directly without using `.perform_later`:\nGoodJob::Bulk.enqueue([MyJob.new, AnotherJob.new])\n```\n\n### Batches\n\nBatches track a set of jobs, and enqueue an optional callback job when all of the jobs have finished (succeeded or discarded).\n\n- A simple example that enqueues your `MyBatchCallbackJob` after the two jobs have finished, and passes along the current user as a batch property:\n\n    ```ruby\n    GoodJob::Batch.enqueue(on_finish: MyBatchCallbackJob, user: current_user) do\n      MyJob.perform_later\n      OtherJob.perform_later\n    end\n\n    # When these jobs have finished, it will enqueue your `MyBatchCallbackJob.perform_later(batch, context)`\n    class MyBatchCallbackJob \u003C ApplicationJob\n      # Callback jobs must accept a `batch` and `context` argument\n      def perform(batch, context)\n        # The batch object will contain the Batch's properties, which are mutable\n        batch.properties[:user] # => \u003CUser id: 1, ...>\n\n        # Context is a hash containing additional context (more may be added in the future)\n        context[:event] # => :finish, :success, :discard\n      end\n    end\n    ```\n\n- Jobs can be added to an existing batch. Jobs in a batch are enqueued and performed immediately\u002Fasynchronously. The final callback job will not be enqueued until `GoodJob::Batch#enqueue` is called.\n\n    ```ruby\n    batch = GoodJob::Batch.new\n    batch.add do\n      10.times { MyJob.perform_later }\n    end\n\n    batch.add do\n      10.times { OtherJob.perform_later }\n    end\n    batch.enqueue(on_finish: MyBatchCallbackJob, age: 42)\n    ```\n\n- If you need to access the batch within a job that is part of the batch, include [`GoodJob::ActiveJobExtensions::Batches`](lib\u002Fgood_job\u002Factive_job_extensions\u002Fbatches.rb) in your job class:\n\n  ```ruby\n    class MyJob \u003C ApplicationJob\n      include GoodJob::ActiveJobExtensions::Batches\n\n      def perform\n        self.batch # => \u003CGoodJob::Batch id: 1, ...>\n      end\n    end\n    ```\n\n- [`GoodJob::Batch`](app\u002Fmodels\u002Fgood_job\u002Fbatch.rb) has a number of assignable attributes and methods:\n\n```ruby\nbatch = GoodJob::Batch.new\nbatch.description = \"My batch\"\nbatch.on_finish = \"MyBatchCallbackJob\" # Callback job when all jobs have finished\nbatch.on_success = \"MyBatchCallbackJob\" # Callback job when\u002Fif all jobs have succeeded\nbatch.on_discard = \"MyBatchCallbackJob\" # Callback job when the first job in the batch is discarded\nbatch.callback_queue_name = \"special_queue\" # Optional queue for callback jobs, otherwise will defer to job class\nbatch.callback_priority = 10 # Optional priority name for callback jobs, otherwise will defer to job class\nbatch.properties = { age: 42 } # Custom data and state to attach to the batch\nbatch.add do\n  MyJob.perform_later\nend\nbatch.enqueue\n\nbatch.discarded? # => Boolean\nbatch.discarded_at # => \u003CDateTime>\nbatch.finished? # => Boolean\nbatch.finished_at # => \u003CDateTime>\nbatch.succeeded? # => Boolean\nbatch.active_jobs # => Array of ActiveJob::Base-inherited jobs that are part of the batch\n\nbatch = GoodJob::Batch.find(batch.id)\nbatch.description = \"Updated batch description\"\nbatch.save\nbatch.reload\n```\n\n### Batch callback jobs\n\nBatch callbacks are Active Job jobs that are enqueued at certain events during the execution of jobs within the batch:\n\n- `:finish` - Enqueued when all jobs in the batch have finished, after all retries. Jobs will either be discarded or succeeded.\n- `:success` - Enqueued only when all jobs in the batch have finished and succeeded.\n- `:discard` - Enqueued immediately the first time a job in the batch is discarded.\n\nCallback jobs must accept a `batch` and `context` argument in their `perform` method:\n\n```ruby\nclass MyBatchCallbackJob \u003C ApplicationJob\n  def perform(batch, context)\n    # The batch object will contain the Batch's properties\n    batch.properties[:user] # => \u003CUser id: 1, ...>\n    # Batches are mutable\n    batch.properties[:user] = User.find(2)\n    batch.save\n\n    # Context is a hash containing additional context (more may be added in the future)\n    context[:event] # => :finish, :success, :discard\n  end\nend\n```\n\n#### Complex batches\n\nConsider a multi-stage batch with both parallel and serial job steps:\n\n```mermaid\ngraph TD\n    0{\"BatchJob\\n{ stage: nil }\"}\n    0 --> a[\"WorkJob]\\n{ step: a }\"]\n    0 --> b[\"WorkJob]\\n{ step: b }\"]\n    0 --> c[\"WorkJob]\\n{ step: c }\"]\n    a --> 1\n    b --> 1\n    c --> 1\n    1{\"BatchJob\\n{ stage: 1 }\"}\n    1 --> d[\"WorkJob]\\n{ step: d }\"]\n    1 --> e[\"WorkJob]\\n{ step: e }\"]\n    e --> f[\"WorkJob]\\n{ step: f }\"]\n    d --> 2\n    f --> 2\n    2{\"BatchJob\\n{ stage: 2 }\"}\n```\n\nThis can be implemented with a single, mutable batch job:\n\n```ruby\nclass WorkJob \u003C ApplicationJob\n  include GoodJob::ActiveJobExtensions::Batches\n\n  def perform(step)\n    # ...\n    if step == 'e'\n      batch.add { WorkJob.perform_later('f') }\n    end\n  end\nend\n\nclass BatchJob \u003C ApplicationJob\n  def perform(batch, context)\n    if batch.properties[:stage].nil?\n      batch.enqueue(stage: 1) do\n        WorkJob.perform_later('a')\n        WorkJob.perform_later('b')\n        WorkJob.perform_later('c')\n      end\n    elsif batch.properties[:stage] == 1\n      batch.enqueue(stage: 2) do\n        WorkJob.perform_later('d')\n        WorkJob.perform_later('e')\n      end\n    elsif batch.properties[:stage] == 2\n      # ...\n    end\n  end\nend\n\nGoodJob::Batch.enqueue(on_finish: BatchJob)\n```\n\n#### Other batch details\n\n- Whether to enqueue a callback job is evaluated once the batch is in an `enqueued?`-state by using `GoodJob::Batch.enqueue` or `batch.enqueue`.\n- Callback job enqueueing will be re-triggered if additional jobs are `enqueue`'d to the batch; use `add` to add jobs to the batch without retriggering callback jobs.\n- Callback jobs will be enqueued even if the batch contains no jobs.\n- Callback jobs perform asynchronously. It's possible that `:finish` and `:success` or `:discard` callback jobs perform at the same time. Keep this in mind when updating batch properties.\n- Batch properties are serialized using Active Job serialization. This is flexible, but can lead to deserialization errors if a GlobalID record is directly referenced but is subsequently deleted and thus unloadable.\n- 🚧Batches are a work in progress. Please let us know what would be helpful to improve their functionality and usefulness.\n\n### Updating\n\nGoodJob follows semantic versioning, though updates may be encouraged through deprecation warnings in minor versions.\n\n#### Upgrading minor versions\n\nUpgrading between minor versions (e.g. v1.4 to v1.5) should not introduce breaking changes, but can introduce new deprecation warnings and database migration warnings.\n\nDatabase migrations introduced in minor releases are _not required_ to be applied until the next major release. If you would like to apply newly introduced migrations immediately, assert `GoodJob.migrated?` in your application's test suite.\n\nTo perform upgrades to the GoodJob database tables:\n\n1. Generate new database migration files:\n\n    ```bash\n    bin\u002Frails g good_job:update\n    ```\n\n   Optional: If using Rails' multiple databases with the `migrations_paths` configuration option, use the `--database` option:\n\n    ```bash\n    bin\u002Frails g good_job:update --database animals\n    ```\n\n1. Run the database migration locally\n\n    ```bash\n    bin\u002Frails db:migrate\n    ```\n\n1. Commit the migration files and resulting `db\u002Fschema.rb` changes.\n1. Deploy the code, run the migrations against the production database, and restart server\u002Fworker processes.\n\n#### Upgrading v3 to v4\n\nGoodJob v4 changes how job and job execution records are stored in the database; moving from job and executions being commingled in the `good_jobs` table to separately and discretely storing job executions in `good_job_executions`. To safely upgrade, all unfinished jobs must use the new format. This change was introduced in GoodJob [v3.15.4 (April 2023)](https:\u002F\u002Fgithub.com\u002Fbensheldon\u002Fgood_job\u002Freleases\u002Ftag\u002Fv3.15.4), so your application is likely ready-to-upgrade in this respect if you have kept up with GoodJob updates and applied migrations (`bin\u002Frails g good_job:update`). _Please be sure to doublecheck you are not missing subsequent migrations or deprecations too by following the instructions below._\n\nTo upgrade:\n\n1. Upgrade to v3.99.x, following the minor version upgrade process, running any remaining database migrations (rails g good_job:update) and addressing deprecation warnings.\n1. Check if your application is safe to upgrade to the new job record format by running either:\n    - In a production console, run `GoodJob.v4_ready?` which should return `true` when safely upgradable.\n    - Or, when connected to the production database verify that `SELECT COUNT(*) FROM \"good_jobs\" WHERE finished_at IS NULL AND is_discrete IS NOT TRUE` returns `0`\n\n    If not all unfinished jobs are stored in the new format, either wait to upgrade until those jobs finish or discard them. Not waiting could prevent those jobs from successfully running when upgrading to v4.\n1. Upgrade from v3.99.x to v4.x.\n\nNotable changes:\n\n- Only supports Rails 6.1+, CRuby 3.0+ and JRuby 9.4+. Rails 6.0 is no longer supported. CRuby 2.6 and 2.7 are no longer supported. JRuby 9.3 is no longer supported.\n- Changes job `priority` to give smaller numbers higher priority (default: `0`), in accordance with Active Job's definition of priority.\n- Enqueues and executes jobs via the `GoodJob::Job` model instead of `GoodJob::Execution`\n- Setting `config.good_job.cleanup_interval_jobs`, `GOOD_JOB_CLEANUP_INTERVAL_JOBS`, `config.good_job.cleanup_interval_seconds`, or `GOOD_JOB_CLEANUP_INTERVAL_SECONDS` to `nil` or `\"\"` no longer disables count- or time-based cleanups. Set to `false` to disable, or `-1` to run a cleanup after every job execution.\n\n#### Upgrading v2 to v3\n\nGoodJob v3 is operationally identical to v2; upgrading to GoodJob v3 should be simple. If you are already using `>= v2.9+` no other changes are necessary.\n\n1. Upgrade to `v2.99.x`, following the minor version upgrade process, running any remaining database migrations (`rails g good_job:update`) and addressing deprecation warnings.\n1. Upgrade from `v2.99.x` to `v3.x`\n\nNotable changes:\n\n- Defaults to preserve job records, and automatically delete them after 14 days.\n- Defaults to discarding failed jobs, instead of immediately retrying them.\n- `:inline` execution mode respects job schedules. Tests can invoke  `GoodJob.perform_inline` to execute jobs.\n- `GoodJob::Adapter` can no longer can be initialized with custom execution options (`queues:`, `max_threads:`, `poll_interval:`).\n- Renames `GoodJob::ActiveJobJob` to `GoodJob::Job`.\n- Removes support for Rails 5.2.\n\n#### Upgrading v1 to v2\n\nGoodJob v2 introduces a new Advisory Lock key format that is operationally different than the v1 advisory lock key format; it's therefore necessary to perform a simple, but staged production upgrade. If you are already using `>= v1.12+` no other changes are necessary.\n\n1. Upgrade your production environment to `v1.99.x` following the minor version upgrade process, including database migrations. `v1.99` is a transitional release that is safely compatible with both `v1.x` and `v2.0.0` because it uses both `v1`- and `v2`-formatted advisory locks.\n1. Address any deprecation warnings generated by `v1.99`.\n1. Upgrade your production environment from `v1.99.x` to `v2.0.x` again following the _minor_ upgrade process.\n\nNotable changes:\n\n- Renames `:async_server` execution mode to `:async`; renames prior `:async` execution mode to `:async_all`.\n- Sets default Development environment's execution mode to `:async` with disabled polling.\n- Excludes performing jobs from `enqueue_limit`'s count in `GoodJob::ActiveJobExtensions::Concurrency`.\n- Triggers `GoodJob.on_thread_error` for unhandled Active Job exceptions.\n- Renames `GoodJob.reperform_jobs_on_standard_error` accessor to `GoodJob.retry_on_unhandled_error`.\n- Renames `GoodJob::Adapter.shutdown(wait:)` argument to `GoodJob::Adapter.shutdown(timeout:)`.\n- Changes Advisory Lock key format from `good_jobs[ROW_ID]` to `good_jobs-[ACTIVE_JOB_ID]`.\n- Expects presence of columns `good_jobs.active_job_id`, `good_jobs.concurrency_key`, `good_jobs.concurrency_key`, and `good_jobs.retried_good_job_id`.\n\n## Go deeper\n\n### Exceptions, retries, and reliability\n\nGoodJob guarantees that a completely-performed job will run once and only once. GoodJob fully supports Active Job's built-in functionality for error handling, retries and timeouts.\n\n#### Exceptions\n\nActive Job provides [tools for rescuing and retrying exceptions](https:\u002F\u002Fguides.rubyonrails.org\u002Factive_job_basics.html#exceptions), including `retry_on`, `discard_on`, `rescue_from` that will rescue exceptions before they get to GoodJob.\n\nIf errors do reach GoodJob, you can assign a callable to `GoodJob.on_thread_error` to be notified. For example, to log errors to an exception monitoring service like Sentry (or Bugsnag, Airbrake, Honeybadger, etc.):\n\n```ruby\n# config\u002Finitializers\u002Fgood_job.rb\nGoodJob.on_thread_error = -> (exception) { Rails.error.report(exception) }\n```\n\n#### Retries\n\nBy default, GoodJob relies on Active Job's retry functionality.\n\nActive Job can be configured to retry an infinite number of times, with a polynomial backoff. Using Active Job's `retry_on` prevents exceptions from reaching GoodJob:\n\n```ruby\nclass ApplicationJob \u003C ActiveJob::Base\n  retry_on StandardError, wait: :polynomially_longer, attempts: Float::INFINITY\n  # ...\nend\n```\n\nWhen using `retry_on` with _a limited number of retries_, the final exception will not be rescued and will raise to GoodJob's error handler. To avoid this, pass a block to `retry_on` to handle the final exception instead of raising it to GoodJob:\n\n```ruby\nclass ApplicationJob \u003C ActiveJob::Base\n  retry_on StandardError, attempts: 5 do |_job, _exception|\n    # Log error, do nothing, etc.\n  end\n  # ...\nend\n```\n\nWhen using `retry_on` with an infinite number of retries, exceptions will never be raised to GoodJob, which means `GoodJob.on_thread_error` will never be called. To report log or report exceptions to an exception monitoring service (e.g. Sentry, Bugsnag, Airbrake, Honeybadger, etc), create an explicit exception wrapper. For example:\n\n```ruby\nclass ApplicationJob \u003C ActiveJob::Base\n  retry_on StandardError, wait: :polynomially_longer, attempts: Float::INFINITY\n\n  retry_on SpecialError, attempts: 5 do |_job, exception|\n    Rails.error.report(exception)\n  end\n\n  around_perform do |_job, block|\n    block.call\n  rescue StandardError => e\n    Rails.error.report(e)\n    raise\n  end\n  # ...\nend\n```\n\nBy default, jobs will not be retried unless `retry_on` is configured. This can be overridden by setting `GoodJob.retry_on_unhandled_error` to `true`; GoodJob will then retry the failing job immediately and infinitely, potentially causing high load.\n\n#### Action Mailer retries\n\nAny configuration in `ApplicationJob` will have to be duplicated on `ActionMailer::MailDeliveryJob` because ActionMailer uses that custom class which inherits from `ActiveJob::Base`,  rather than your application's `ApplicationJob`.\n\nYou can use an initializer to configure `ActionMailer::MailDeliveryJob`, for example:\n\n```ruby\n# config\u002Finitializers\u002Fgood_job.rb\nActionMailer::MailDeliveryJob.retry_on StandardError, wait: :polynomially_longer, attempts: Float::INFINITY\n\n# With Sentry (or Bugsnag, Airbrake, Honeybadger, etc.)\nActionMailer::MailDeliveryJob.around_perform do |_job, block|\n  block.call\nrescue StandardError => e\n  Rails.error.report(e)\n  raise\nend\n```\n\nNote, that `ActionMailer::MailDeliveryJob` is a default since Rails 6.0. Be sure that your app is using that class, as it\nmight also be configured to use (deprecated now) `ActionMailer::DeliveryJob`.\n\n### Interrupts, graceful shutdown, and SIGKILL\n\nWhen GoodJob receives an interrupt (SIGINT, SIGTERM) or explicitly with `GoodJob.shutdown`, GoodJob will attempt to gracefully shut down, waiting for all jobs to finish before exiting based on the `shutdown_timeout` configuration.\n\nTo detect the start of a graceful shutdown from within a performing job, for example while looping\u002Fiterating over multiple items, you can call `GoodJob.current_thread_shutting_down?` or `GoodJob.current_thread_running?` from within the job. For example:\n\n```ruby\ndef perform(lots_of_records)\n  lots_of_records.each do |record|\n    break if GoodJob.current_thread_shutting_down? # or `unless GoodJob.current_thread_running?`\n    # process record ...\n  end\nend\n````\n\nNote that when running jobs in `:inline` execution mode, `GoodJob.current_thread_running?` will always be truthy and `GoodJob.current_thread_shutting_down?` will always be falsey.\n\nJobs will be automatically retried if the process is interrupted while performing a job and the job is unable to finish before the timeout or as the result of a `SIGKILL` or power failure. The interrupted execution's error record will show `GoodJob::InterruptedError` to distinguish it from the rescuable `GoodJob::InterruptError` that is raised when the job is retried.\n\nIf you need more control over interrupt-caused retries, include the `GoodJob::ActiveJobExtensions::InterruptErrors` extension in your job class. When an interrupted job is retried, the extension will raise a `GoodJob::InterruptError` exception within the job, which allows you to use Active Job's `retry_on` and `discard_on` to control the behavior of the job.\n\n```ruby\nclass MyJob \u003C ApplicationJob\n  # The extension must be included before other extensions\n  include GoodJob::ActiveJobExtensions::InterruptErrors\n  # Discard the job if it is interrupted\n  discard_on GoodJob::InterruptError\n  # Retry the job if it is interrupted\n  retry_on GoodJob::InterruptError, wait: 0, attempts: Float::INFINITY\nend\n```\n\n### Timeouts\n\nAvoid using Ruby's built-in [Timeout](https:\u002F\u002Fgithub.com\u002Fruby\u002Ftimeout) mechanism\n([1](https:\u002F\u002Fwww.mikeperham.com\u002F2015\u002F05\u002F08\u002Ftimeout-rubys-most-dangerous-api\u002F),\n[2](https:\u002F\u002Fblog.headius.com\u002F2008\u002F02\u002Frubys-threadraise-threadkill-timeoutrb.html)).\nInstead, declare either of Active Job's [discard_on](https:\u002F\u002Fapi.rubyonrails.org\u002Fclasses\u002FActiveJob\u002FExceptions\u002FClassMethods.html#method-i-discard_on) or [retry_on](https:\u002F\u002Fapi.rubyonrails.org\u002Fclasses\u002FActiveJob\u002FExceptions\u002FClassMethods.html#method-i-retry_on) to handle\nthe underlying mechanism's timeout exceptions (when available).\n\nFor example, rescue from `Net::OpenTimeout` or `Net::ReadTimeout` and discard\nthe job:\n\n```ruby\nclass MyJob \u003C ApplicationJob\n  discard_on Net::OpenTimeout, Net::ReadTimeout\n\n  def perform(uri)\n    Net::HTTP.start(uri.host, uri.port, open_timeout: 3, read_timeout: 3) do |http|\n      http.request(...)\n    end\n  end\nend\n```\n\nIf you have no other choice but to use a Ruby Timeout, it can be configured with an `around_perform`:\n\n```ruby\nclass ApplicationJob \u003C ActiveJob::Base\n  JobTimeoutError = Class.new(StandardError)\n\n  around_perform do |_job, block|\n    # Timeout jobs after 10 minutes\n    Timeout.timeout(10.minutes, JobTimeoutError) do\n      block.call\n    end\n  end\nend\n```\n\n### Optimize queues, threads, and processes\n\nBy default, GoodJob creates a single thread execution pool that will execute jobs from any queue. Depending on your application's workload, job types, and service level objectives, you may wish to optimize execution resources. For example, providing dedicated execution resources for transactional emails so they are not delayed by long-running batch jobs. Some options:\n\n- Multiple isolated execution pools within a single process:\n\n    For moderate workloads, multiple isolated thread execution pools offers a good balance between congestion management and economy.\n\n    A pool is configured with the following syntax `\u003Cparticipating_queues>:\u003Cthread_count>`:\n\n    - `\u003Cparticipating_queues>`: either `queue1,queue2` (only those queues), `+queue1,queue2` (only those queues, and processed in order), `*` (all) or `-queue1,queue2` (all except those queues).\n    - `\u003Cthread_count>`: a count overriding for this specific pool the global `max-threads`.\n\n    Pool configurations are separated with a semicolon (;) in the `queues` configuration\n\n    ```bash\n    $ bundle exec good_job \\\n        --queues=\"transactional_messages:2;batch_processing:1;-transactional_messages,batch_processing:2;*\" \\\n        --max-threads=5\n    ```\n\n    This configuration will result in a single process with 4 isolated thread execution pools.\n\n    - `transactional_messages:2`: execute jobs enqueued on `transactional_messages`, with up to 2 threads.\n    - `batch_processing:1` execute jobs enqueued on `batch_processing`, with a single thread.\n    - `-transactional_messages,batch_processing:2`: execute jobs enqueued on _any_ queue _excluding_ `transactional_messages` or `batch_processing`, with up to 2 threads.\n    - `*`: execute jobs on any queue, with up to 5 threads (as configured by `--max-threads=5`).\n\n    When a pool is performing jobs from multiple queues, jobs will be performed from specified queues, ordered by priority and creation time. To perform jobs from queues in the queues' given order, use the `+` modifier. In this example, jobs in `batch_processing` will be performed only when there are no jobs in `transactional_messages`:\n\n    ```bash\n    bundle exec good_job --queues=\"+transactional_messages,batch_processing\"\n    ```\n\n    Configuration can be injected by environment variables too:\n\n    ```bash\n    $ GOOD_JOB_QUEUES=\"transactional_messages:2;batch_processing:1;-transactional_messages,batch_processing:2;*\" \\\n      GOOD_JOB_MAX_THREADS=5 \\\n      bundle exec good_job\n    ```\n\n- Multiple processes:\n\n    While multiple isolated thread execution pools offer a way to provide dedicated execution resources, those resources are bound to a single machine. To scale them independently, define several processes.\n\n    For example, this configuration on Heroku allows to customize the dyno count (instances), or type (CPU\u002FRAM), per process type:\n\n    ```procfile\n    # Procfile\n\n    # Separate process types\n    worker: bundle exec good_job --max-threads=5\n    transactional_worker: bundle exec good_job --queues=\"transactional_messages\" --max-threads=2\n    batch_worker: bundle exec good_job --queues=\"batch_processing\" --max-threads=1\n    ```\n\n    To optimize for CPU performance at the expense of greater memory and system resource usage, while keeping a single process type (and thus a single dyno), combine several processes and wait for them:\n\n    ```procfile\n    # Procfile\n\n    # Combined multi-process\n    combined_worker: bundle exec good_job --max-threads=5 & bundle exec good_job --queues=\"transactional_messages\" --max-threads=2 & bundle exec good_job --queues=\"batch_processing\" --max-threads=1 & wait -n\n    ```\n\nKeep in mind, queue operations and management is an advanced discipline. Thi","GoodJob 是一个基于Postgres的多线程Active Job后端，专为Ruby on Rails设计。其核心功能包括对异步任务、队列、延迟执行、优先级设置、超时和重试的支持，并且几乎无需配置即可使用。GoodJob遵循Rails框架的多线程和代码执行规范，利用Postgres的会话级建议锁来确保任务的一次性安全执行，并通过LISTEN\u002FNOTIFY机制减少排队延迟。它还提供了一个强大的Web控制面板，支持定时任务调度、批处理以及并发和限流控制。GoodJob适用于大多数工作负载场景，特别适合全栈团队、注重成本效益的独立开发者以及每天需要处理上百万个后台任务的应用程序。",2,"2026-06-11 03:14:56","top_language"]