From 358f00147a7bb48560d7f8ff4664b4103bb1103f Mon Sep 17 00:00:00 2001 From: Christoph Marzell Date: Tue, 9 Dec 2025 05:44:11 +0100 Subject: [PATCH] add goodjob --- Gemfile | 2 + Gemfile.lock | 8 ++ app/jobs/daily_dump_job.rb | 27 ++++- app/jobs/training_watch_job.rb | 26 ++++- config/application.rb | 3 + config/initializers/good_job.rb | 28 +++++ db/migrate/20251209043625_create_good_jobs.rb | 105 ++++++++++++++++++ db/schema.rb | 93 +++++++++++++++- 8 files changed, 289 insertions(+), 3 deletions(-) create mode 100644 config/initializers/good_job.rb create mode 100644 db/migrate/20251209043625_create_good_jobs.rb diff --git a/Gemfile b/Gemfile index e92c973..52f80a6 100644 --- a/Gemfile +++ b/Gemfile @@ -62,3 +62,5 @@ gem 'rufus-scheduler' gem 'pghero' gem "faraday" gem "nokogiri" + +gem "good_job", "~> 4.13" diff --git a/Gemfile.lock b/Gemfile.lock index 371f8b6..d94a75d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -123,6 +123,13 @@ GEM raabro (~> 1.4) globalid (1.3.0) activesupport (>= 6.1) + good_job (4.13.0) + 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.1) @@ -303,6 +310,7 @@ DEPENDENCIES bootsnap devise faraday + good_job (~> 4.13) jbuilder jquery-rails jquery-ui-rails diff --git a/app/jobs/daily_dump_job.rb b/app/jobs/daily_dump_job.rb index 059440c..3a1f545 100644 --- a/app/jobs/daily_dump_job.rb +++ b/app/jobs/daily_dump_job.rb @@ -1,6 +1,31 @@ # app/jobs/daily_dump_job.rb class DailyDumpJob < ApplicationJob - queue_as :default + queue_as :daily_dump + include GoodJob::ActiveJobExtensions::Concurrency + + good_job_control_concurrency_with( + # Maximum number of unfinished jobs to allow with the concurrency key + total_limit: 1, + + # Or, if more control is needed: + # Maximum number of jobs with the concurrency key to be + # concurrently enqueued (excludes performing jobs) + enqueue_limit: 1, + + # Maximum number of jobs with the concurrency key to be + # concurrently performed (excludes enqueued jobs) + perform_limit: 1, + + # Note: Under heavy load, the total number of jobs may exceed the + # sum of `enqueue_limit` and `perform_limit` because of race conditions + # caused by imperfectly disjunctive states. If you need to constrain + # the total number of jobs, use `total_limit` instead. See #378. + + # A unique key to be globally locked against. + # Can be String or Lambda/Proc that is invoked in the context of the job. + # Note: Arguments passed to #perform_later must be accessed through `arguments` method. + key: -> { "Unique-DailyDumpJob" } + ) def perform zip_path = DumpService.perform diff --git a/app/jobs/training_watch_job.rb b/app/jobs/training_watch_job.rb index 9121a7e..2b2fedb 100644 --- a/app/jobs/training_watch_job.rb +++ b/app/jobs/training_watch_job.rb @@ -1,7 +1,31 @@ # app/jobs/training_watch_job.rb class TrainingWatchJob < ApplicationJob - queue_as :default + queue_as :training_watch + include GoodJob::ActiveJobExtensions::Concurrency + good_job_control_concurrency_with( + # Maximum number of unfinished jobs to allow with the concurrency key + total_limit: 1, + + # Or, if more control is needed: + # Maximum number of jobs with the concurrency key to be + # concurrently enqueued (excludes performing jobs) + enqueue_limit: 1, + + # Maximum number of jobs with the concurrency key to be + # concurrently performed (excludes enqueued jobs) + perform_limit: 1, + + # Note: Under heavy load, the total number of jobs may exceed the + # sum of `enqueue_limit` and `perform_limit` because of race conditions + # caused by imperfectly disjunctive states. If you need to constrain + # the total number of jobs, use `total_limit` instead. See #378. + + # A unique key to be globally locked against. + # Can be String or Lambda/Proc that is invoked in the context of the job. + # Note: Arguments passed to #perform_later must be accessed through `arguments` method. + key: -> { "Unique-TrainingWatchJob" } + ) def perform results = TrainingWatch::Checker.new.run! return if results.empty? diff --git a/config/application.rb b/config/application.rb index 7b5c8f7..e711ad3 100644 --- a/config/application.rb +++ b/config/application.rb @@ -39,5 +39,8 @@ module Praktikum # Don't generate system test files. config.generators.system_tests = nil + + + end end diff --git a/config/initializers/good_job.rb b/config/initializers/good_job.rb new file mode 100644 index 0000000..f8f9423 --- /dev/null +++ b/config/initializers/good_job.rb @@ -0,0 +1,28 @@ +# config/initializers/good_job.rb +Rails.application.configure do + config.active_job.queue_adapter = :good_job + + config.good_job = { + execution_mode: :async, # läuft im Web-Prozess (kein extra Worker nötig) + queues: "*", + max_threads: 2, # reicht für 2 Jobs locker + preserve_job_records: true, # wichtig für Cron-Unique-Mechanik + enable_cron: true, + cron_graceful_restart_period: 5.minutes, # nach Deploy kurz "nachziehen" + cron: { + daily_dump: { + cron: "0 2 * * * Europe/Vienna", + class: "DailyDumpJob", + description: "Täglichen Dump-Versand starten" + }, + training_watch: { + cron: "0 0 * * * Europe/Vienna", + class: "TrainingWatch", + description: "TrainingWatch starten" + } + } + } + + # deprecation warning sauber fixen/silencen (wie in deiner Logmeldung) + #config.good_job.smaller_number_is_higher_priority = false +end diff --git a/db/migrate/20251209043625_create_good_jobs.rb b/db/migrate/20251209043625_create_good_jobs.rb new file mode 100644 index 0000000..5288735 --- /dev/null +++ b/db/migrate/20251209043625_create_good_jobs.rb @@ -0,0 +1,105 @@ +# frozen_string_literal: true + +class CreateGoodJobs < ActiveRecord::Migration[7.1] + 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, :job_class, name: :index_good_jobs_on_job_class + 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 34c8ff2..db3dc2b 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[7.1].define(version: 2025_12_07_054146) do +ActiveRecord::Schema[7.1].define(version: 2025_12_09_043625) do # These are extensions that must be enabled in order to support this database enable_extension "pg_stat_statements" enable_extension "plpgsql" @@ -43,6 +43,97 @@ ActiveRecord::Schema[7.1].define(version: 2025_12_07_054146) do t.index ["user_id"], name: "index_entries_on_user_id" 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 ["job_class"], name: "index_good_jobs_on_job_class" + 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 "mileage_rates", force: :cascade do |t| t.integer "year", null: false t.decimal "rate_per_km", precision: 5, scale: 2, null: false