diff --git a/Gemfile b/Gemfile index 63547e4..4893559 100644 --- a/Gemfile +++ b/Gemfile @@ -6,6 +6,8 @@ gem "rails", "~> 8.0.2" gem "pg", "~> 1.1" # Use the Puma web server [https://github.com/puma/puma] gem "puma", ">= 5.0" +# Use GoodJob for Active Job queue adapter +gem "good_job", "~> 4.10", ">= 4.10.2" # Build JSON APIs with ease [https://github.com/rails/jbuilder] # gem "jbuilder" diff --git a/Gemfile.lock b/Gemfile.lock index d6732d9..b559ec1 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -91,8 +91,20 @@ GEM drb (2.2.3) erb (5.0.1) erubi (1.13.1) + et-orbi (1.2.11) + tzinfo + fugit (1.11.1) + et-orbi (~> 1, >= 1.2.11) + raabro (~> 1.4) globalid (1.2.1) activesupport (>= 6.1) + good_job (4.10.2) + activejob (>= 6.1.0) + activerecord (>= 6.1.0) + concurrent-ruby (>= 1.3.1) + fugit (>= 1.11.0) + railties (>= 6.1.0) + thor (>= 1.0.0) i18n (1.14.7) concurrent-ruby (~> 1.0) io-console (0.8.0) @@ -156,6 +168,7 @@ GEM stringio puma (6.6.0) nio4r (~> 2.0) + raabro (1.4.0) racc (1.8.1) rack (3.1.16) rack-session (2.1.1) @@ -264,6 +277,7 @@ DEPENDENCIES bootsnap brakeman debug + good_job (~> 4.10, >= 4.10.2) pg (~> 1.1) puma (>= 5.0) rails (~> 8.0.2) diff --git a/config/application.rb b/config/application.rb index a61c26b..31489f3 100644 --- a/config/application.rb +++ b/config/application.rb @@ -40,5 +40,12 @@ class Application < Rails::Application # Middleware like session, flash, cookies can be added back manually. # Skip views, helpers and assets when generating a new resource. config.api_only = true + + + # Re-enable middleware needed for the Good Job Dashboard + config.middleware.use Rack::MethodOverride + config.middleware.use ActionDispatch::Flash + config.middleware.use ActionDispatch::Cookies + config.middleware.use ActionDispatch::Session::CookieStore end end diff --git a/config/environments/development.rb b/config/environments/development.rb index e332d04..aba0848 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -53,6 +53,9 @@ # Highlight code that enqueued background job in logs. config.active_job.verbose_enqueue_logs = true + # Use GoodJob for Active Job queue adapter + config.active_job.queue_adapter = :good_job + # Raises error for missing translations. # config.i18n.raise_on_missing_translations = true diff --git a/config/environments/production.rb b/config/environments/production.rb index 9959bc7..47d2b06 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -47,7 +47,7 @@ # config.cache_store = :mem_cache_store # Replace the default in-process and non-durable queuing backend for Active Job. - # config.active_job.queue_adapter = :resque + config.active_job.queue_adapter = :good_job # Ignore bad email addresses and do not raise email delivery errors. # Set this to true and configure the email server for immediate delivery to raise delivery errors. diff --git a/config/environments/staging.rb b/config/environments/staging.rb index 9959bc7..47d2b06 100644 --- a/config/environments/staging.rb +++ b/config/environments/staging.rb @@ -47,7 +47,7 @@ # config.cache_store = :mem_cache_store # Replace the default in-process and non-durable queuing backend for Active Job. - # config.active_job.queue_adapter = :resque + config.active_job.queue_adapter = :good_job # Ignore bad email addresses and do not raise email delivery errors. # Set this to true and configure the email server for immediate delivery to raise delivery errors. diff --git a/config/environments/test.rb b/config/environments/test.rb index c2095b1..30cc5cd 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -50,4 +50,7 @@ # Raise error when a before_action's only/except options reference missing actions. config.action_controller.raise_on_missing_callback_actions = true + + # Use inline adapter for Active Job in tests for faster execution + config.active_job.queue_adapter = :inline end diff --git a/config/initializers/admin_constraint.rb b/config/initializers/admin_constraint.rb new file mode 100644 index 0000000..35d1d32 --- /dev/null +++ b/config/initializers/admin_constraint.rb @@ -0,0 +1,5 @@ +class AdminConstraint + def self.matches?(request) + request.env["HTTP_AUTHORIZATION"] == "Basic " + Base64.strict_encode64("#{ENV['ADMIN_LOGIN']}:#{ENV['ADMIN_PASSWORD']}") + end +end diff --git a/config/initializers/good_job.rb b/config/initializers/good_job.rb new file mode 100644 index 0000000..1ef8792 --- /dev/null +++ b/config/initializers/good_job.rb @@ -0,0 +1,25 @@ +# GoodJob configuration +Rails.application.configure do + # Configure GoodJob settings + config.good_job.execution_mode = :external + config.good_job.max_threads = 5 + config.good_job.poll_interval = 30 + config.good_job.shutdown_timeout = 25 + + # Enable cron jobs + config.good_job.enable_cron = true + + # Preserve job records for debugging + config.good_job.preserve_job_records = true + config.good_job.cleanup_preserved_jobs_before_seconds_ago = 14.days + + # Dashboard authentication (customize as needed) + config.good_job.dashboard_default_locale = :en + + # Queue configuration + config.good_job.queues = "*" + + # Smaller batch size for better performance + config.good_job.cleanup_interval_jobs = 1000 + config.good_job.cleanup_interval_seconds = 600 +end diff --git a/config/routes.rb b/config/routes.rb index 77b2c51..0c3ab58 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,6 +1,16 @@ Rails.application.routes.draw do resources :ministers, only: [ :index, :show ] resources :departments, only: [ :index, :show ] + + # GoodJob dashboard + unless Rails.env.development? + GoodJob::Engine.middleware.use Rack::Auth::Basic do |username, password| + username == ENV["ADMIN_LOGIN"] && password == ENV["ADMIN_PASSWORD"] + end + else + end + mount GoodJob::Engine => "/admin/good_job" + # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html # Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500. diff --git a/db/migrate/20250613001948_create_good_jobs.rb b/db/migrate/20250613001948_create_good_jobs.rb new file mode 100644 index 0000000..ade71ce --- /dev/null +++ b/db/migrate/20250613001948_create_good_jobs.rb @@ -0,0 +1,104 @@ +# frozen_string_literal: true + +class CreateGoodJobs < ActiveRecord::Migration[8.0] + def change + # Uncomment for Postgres v12 or earlier to enable gen_random_uuid() support + # enable_extension 'pgcrypto' + + create_table :good_jobs, id: :uuid do |t| + t.text :queue_name + t.integer :priority + t.jsonb :serialized_params + t.datetime :scheduled_at + t.datetime :performed_at + t.datetime :finished_at + t.text :error + + t.timestamps + + t.uuid :active_job_id + t.text :concurrency_key + t.text :cron_key + t.uuid :retried_good_job_id + t.datetime :cron_at + + t.uuid :batch_id + t.uuid :batch_callback_id + + t.boolean :is_discrete + t.integer :executions_count + t.text :job_class + t.integer :error_event, limit: 2 + t.text :labels, array: true + t.uuid :locked_by_id + t.datetime :locked_at + end + + create_table :good_job_batches, id: :uuid do |t| + t.timestamps + t.text :description + t.jsonb :serialized_properties + t.text :on_finish + t.text :on_success + t.text :on_discard + t.text :callback_queue_name + t.integer :callback_priority + t.datetime :enqueued_at + t.datetime :discarded_at + t.datetime :finished_at + t.datetime :jobs_finished_at + end + + create_table :good_job_executions, id: :uuid do |t| + t.timestamps + + t.uuid :active_job_id, null: false + t.text :job_class + t.text :queue_name + t.jsonb :serialized_params + t.datetime :scheduled_at + t.datetime :finished_at + t.text :error + t.integer :error_event, limit: 2 + t.text :error_backtrace, array: true + t.uuid :process_id + t.interval :duration + end + + create_table :good_job_processes, id: :uuid do |t| + t.timestamps + t.jsonb :state + t.integer :lock_type, limit: 2 + end + + create_table :good_job_settings, id: :uuid do |t| + t.timestamps + t.text :key + t.jsonb :value + t.index :key, unique: true + end + + add_index :good_jobs, :scheduled_at, where: "(finished_at IS NULL)", name: :index_good_jobs_on_scheduled_at + add_index :good_jobs, [ :queue_name, :scheduled_at ], where: "(finished_at IS NULL)", name: :index_good_jobs_on_queue_name_and_scheduled_at + add_index :good_jobs, [ :active_job_id, :created_at ], name: :index_good_jobs_on_active_job_id_and_created_at + add_index :good_jobs, :concurrency_key, where: "(finished_at IS NULL)", name: :index_good_jobs_on_concurrency_key_when_unfinished + add_index :good_jobs, [ :concurrency_key, :created_at ], name: :index_good_jobs_on_concurrency_key_and_created_at + add_index :good_jobs, [ :cron_key, :created_at ], where: "(cron_key IS NOT NULL)", name: :index_good_jobs_on_cron_key_and_created_at_cond + add_index :good_jobs, [ :cron_key, :cron_at ], where: "(cron_key IS NOT NULL)", unique: true, name: :index_good_jobs_on_cron_key_and_cron_at_cond + add_index :good_jobs, [ :finished_at ], where: "retried_good_job_id IS NULL AND finished_at IS NOT NULL", name: :index_good_jobs_jobs_on_finished_at + add_index :good_jobs, [ :priority, :created_at ], order: { priority: "DESC NULLS LAST", created_at: :asc }, + where: "finished_at IS NULL", name: :index_good_jobs_jobs_on_priority_created_at_when_unfinished + add_index :good_jobs, [ :priority, :created_at ], order: { priority: "ASC NULLS LAST", created_at: :asc }, + where: "finished_at IS NULL", name: :index_good_job_jobs_for_candidate_lookup + add_index :good_jobs, [ :batch_id ], where: "batch_id IS NOT NULL" + add_index :good_jobs, [ :batch_callback_id ], where: "batch_callback_id IS NOT NULL" + add_index :good_jobs, :labels, using: :gin, where: "(labels IS NOT NULL)", name: :index_good_jobs_on_labels + + add_index :good_job_executions, [ :active_job_id, :created_at ], name: :index_good_job_executions_on_active_job_id_and_created_at + add_index :good_jobs, [ :priority, :scheduled_at ], order: { priority: "ASC NULLS LAST", scheduled_at: :asc }, + where: "finished_at IS NULL AND locked_by_id IS NULL", name: :index_good_jobs_on_priority_scheduled_at_unfinished_unlocked + add_index :good_jobs, :locked_by_id, + where: "locked_by_id IS NOT NULL", name: "index_good_jobs_on_locked_by_id" + add_index :good_job_executions, [ :process_id, :created_at ], name: :index_good_job_executions_on_process_id_and_created_at + end +end diff --git a/db/schema.rb b/db/schema.rb index 517e6fe..9bbc467 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[8.0].define(version: 2025_06_12_225202) do +ActiveRecord::Schema[8.0].define(version: 2025_06_13_001948) do # These are extensions that must be enabled in order to support this database enable_extension "pg_catalog.plpgsql" @@ -26,6 +26,96 @@ t.index ["slug"], name: "index_departments_on_slug" end + create_table "good_job_batches", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.text "description" + t.jsonb "serialized_properties" + t.text "on_finish" + t.text "on_success" + t.text "on_discard" + t.text "callback_queue_name" + t.integer "callback_priority" + t.datetime "enqueued_at" + t.datetime "discarded_at" + t.datetime "finished_at" + t.datetime "jobs_finished_at" + end + + create_table "good_job_executions", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.uuid "active_job_id", null: false + t.text "job_class" + t.text "queue_name" + t.jsonb "serialized_params" + t.datetime "scheduled_at" + t.datetime "finished_at" + t.text "error" + t.integer "error_event", limit: 2 + t.text "error_backtrace", array: true + t.uuid "process_id" + t.interval "duration" + t.index ["active_job_id", "created_at"], name: "index_good_job_executions_on_active_job_id_and_created_at" + t.index ["process_id", "created_at"], name: "index_good_job_executions_on_process_id_and_created_at" + end + + create_table "good_job_processes", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.jsonb "state" + t.integer "lock_type", limit: 2 + end + + create_table "good_job_settings", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.text "key" + t.jsonb "value" + t.index ["key"], name: "index_good_job_settings_on_key", unique: true + end + + create_table "good_jobs", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| + t.text "queue_name" + t.integer "priority" + t.jsonb "serialized_params" + t.datetime "scheduled_at" + t.datetime "performed_at" + t.datetime "finished_at" + t.text "error" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.uuid "active_job_id" + t.text "concurrency_key" + t.text "cron_key" + t.uuid "retried_good_job_id" + t.datetime "cron_at" + t.uuid "batch_id" + t.uuid "batch_callback_id" + t.boolean "is_discrete" + t.integer "executions_count" + t.text "job_class" + t.integer "error_event", limit: 2 + t.text "labels", array: true + t.uuid "locked_by_id" + t.datetime "locked_at" + t.index ["active_job_id", "created_at"], name: "index_good_jobs_on_active_job_id_and_created_at" + t.index ["batch_callback_id"], name: "index_good_jobs_on_batch_callback_id", where: "(batch_callback_id IS NOT NULL)" + t.index ["batch_id"], name: "index_good_jobs_on_batch_id", where: "(batch_id IS NOT NULL)" + t.index ["concurrency_key", "created_at"], name: "index_good_jobs_on_concurrency_key_and_created_at" + t.index ["concurrency_key"], name: "index_good_jobs_on_concurrency_key_when_unfinished", where: "(finished_at IS NULL)" + t.index ["cron_key", "created_at"], name: "index_good_jobs_on_cron_key_and_created_at_cond", where: "(cron_key IS NOT NULL)" + t.index ["cron_key", "cron_at"], name: "index_good_jobs_on_cron_key_and_cron_at_cond", unique: true, where: "(cron_key IS NOT NULL)" + t.index ["finished_at"], name: "index_good_jobs_jobs_on_finished_at", where: "((retried_good_job_id IS NULL) AND (finished_at IS NOT NULL))" + t.index ["labels"], name: "index_good_jobs_on_labels", where: "(labels IS NOT NULL)", using: :gin + t.index ["locked_by_id"], name: "index_good_jobs_on_locked_by_id", where: "(locked_by_id IS NOT NULL)" + t.index ["priority", "created_at"], name: "index_good_job_jobs_for_candidate_lookup", where: "(finished_at IS NULL)" + t.index ["priority", "created_at"], name: "index_good_jobs_jobs_on_priority_created_at_when_unfinished", order: { priority: "DESC NULLS LAST" }, where: "(finished_at IS NULL)" + t.index ["priority", "scheduled_at"], name: "index_good_jobs_on_priority_scheduled_at_unfinished_unlocked", where: "((finished_at IS NULL) AND (locked_by_id IS NULL))" + t.index ["queue_name", "scheduled_at"], name: "index_good_jobs_on_queue_name_and_scheduled_at", where: "(finished_at IS NULL)" + t.index ["scheduled_at"], name: "index_good_jobs_on_scheduled_at", where: "(finished_at IS NULL)" + end + create_table "governments", force: :cascade do |t| t.string "name", null: false t.string "slug", null: false