From d15096b3a7a048733f4e36f8ad362b29256707f1 Mon Sep 17 00:00:00 2001 From: Manuel Bustillo Date: Sun, 3 Nov 2024 08:59:56 +0100 Subject: [PATCH 1/6] Install solid queues framework --- Gemfile | 2 + Gemfile.lock | 14 ++ bin/jobs | 6 + config/application.rb | 3 + config/environments/production.rb | 2 - config/queue.yml | 18 +++ config/recurring.yml | 10 ++ .../20241103075705_solid_queue_install.rb | 132 ++++++++++++++++++ db/queue_schema.rb | 2 + db/schema.rb | 129 ++++++++++++++++- 10 files changed, 315 insertions(+), 3 deletions(-) create mode 100755 bin/jobs create mode 100644 config/queue.yml create mode 100644 config/recurring.yml create mode 100644 db/migrate/20241103075705_solid_queue_install.rb create mode 100644 db/queue_schema.rb diff --git a/Gemfile b/Gemfile index 2724b02..f61ed2f 100644 --- a/Gemfile +++ b/Gemfile @@ -32,3 +32,5 @@ group :development do gem 'rubocop' gem 'web-console' end + +gem "solid_queue", "~> 1.0" diff --git a/Gemfile.lock b/Gemfile.lock index 34cdc5f..05575bf 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -96,6 +96,8 @@ GEM diff-lcs (1.5.1) drb (2.2.1) erubi (1.13.0) + et-orbi (1.2.11) + tzinfo execjs (2.9.1) factory_bot (6.4.6) activesupport (>= 5.0.0) @@ -104,6 +106,9 @@ GEM railties (>= 5.0.0) faker (3.5.1) i18n (>= 1.8.11, < 2) + fugit (1.11.1) + et-orbi (~> 1, >= 1.2.11) + raabro (~> 1.4) globalid (1.2.1) activesupport (>= 6.1) i18n (1.14.6) @@ -182,6 +187,7 @@ GEM stringio puma (6.4.3) nio4r (~> 2.0) + raabro (1.4.0) racc (1.8.1) rack (3.1.8) rack-cors (2.0.2) @@ -272,6 +278,13 @@ GEM rubytree (2.1.0) json (~> 2.0, > 2.3.1) securerandom (0.3.1) + solid_queue (1.0.0) + activejob (>= 7.1) + activerecord (>= 7.1) + concurrent-ruby (>= 1.3.1) + fugit (~> 1.11.0) + railties (>= 7.1) + thor (~> 1.3.1) sprockets (4.2.1) concurrent-ruby (~> 1.0) rack (>= 2.2.4, < 4) @@ -331,6 +344,7 @@ DEPENDENCIES rspec-rails (~> 7.0.0) rubocop rubytree + solid_queue (~> 1.0) sprockets-rails stimulus-rails turbo-rails diff --git a/bin/jobs b/bin/jobs new file mode 100755 index 0000000..dcf59f3 --- /dev/null +++ b/bin/jobs @@ -0,0 +1,6 @@ +#!/usr/bin/env ruby + +require_relative "../config/environment" +require "solid_queue/cli" + +SolidQueue::Cli.start(ARGV) diff --git a/config/application.rb b/config/application.rb index 2c1fa72..8233705 100644 --- a/config/application.rb +++ b/config/application.rb @@ -30,6 +30,9 @@ module WeddingPlanner # Common ones are `templates`, `generators`, or `middleware`, for example. config.autoload_lib(ignore: %w[assets tasks]) + # Use a real queuing backend for Active Job (and separate queues per environment). + config.active_job.queue_adapter = :solid_queue + # Configuration for the application, engines, and railties goes here. # # These settings can be overridden in specific environments using the files diff --git a/config/environments/production.rb b/config/environments/production.rb index eb6e7fa..94a2257 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -69,8 +69,6 @@ Rails.application.configure do # Use a different cache store in production. # config.cache_store = :mem_cache_store - # Use a real queuing backend for Active Job (and separate queues per environment). - # config.active_job.queue_adapter = :resque # config.active_job.queue_name_prefix = "wedding_planner_production" config.action_mailer.perform_caching = false diff --git a/config/queue.yml b/config/queue.yml new file mode 100644 index 0000000..9eace59 --- /dev/null +++ b/config/queue.yml @@ -0,0 +1,18 @@ +default: &default + dispatchers: + - polling_interval: 1 + batch_size: 500 + workers: + - queues: "*" + threads: 3 + processes: <%= ENV.fetch("JOB_CONCURRENCY", 1) %> + polling_interval: 0.1 + +development: + <<: *default + +test: + <<: *default + +production: + <<: *default diff --git a/config/recurring.yml b/config/recurring.yml new file mode 100644 index 0000000..d045b19 --- /dev/null +++ b/config/recurring.yml @@ -0,0 +1,10 @@ +# production: +# periodic_cleanup: +# class: CleanSoftDeletedRecordsJob +# queue: background +# args: [ 1000, { batch_size: 500 } ] +# schedule: every hour +# periodic_command: +# command: "SoftDeletedRecord.due.delete_all" +# priority: 2 +# schedule: at 5am every day diff --git a/db/migrate/20241103075705_solid_queue_install.rb b/db/migrate/20241103075705_solid_queue_install.rb new file mode 100644 index 0000000..4dfb4d0 --- /dev/null +++ b/db/migrate/20241103075705_solid_queue_install.rb @@ -0,0 +1,132 @@ +class SolidQueueInstall < ActiveRecord::Migration[7.2] + def change + create_table 'solid_queue_blocked_executions', force: :cascade do |t| + t.bigint 'job_id', null: false + t.string 'queue_name', null: false + t.integer 'priority', default: 0, null: false + t.string 'concurrency_key', null: false + t.datetime 'expires_at', null: false + t.datetime 'created_at', null: false + t.index %w[concurrency_key priority job_id], name: 'index_solid_queue_blocked_executions_for_release' + t.index %w[expires_at concurrency_key], name: 'index_solid_queue_blocked_executions_for_maintenance' + t.index ['job_id'], name: 'index_solid_queue_blocked_executions_on_job_id', unique: true + end + + create_table 'solid_queue_claimed_executions', force: :cascade do |t| + t.bigint 'job_id', null: false + t.bigint 'process_id' + t.datetime 'created_at', null: false + t.index ['job_id'], name: 'index_solid_queue_claimed_executions_on_job_id', unique: true + t.index %w[process_id job_id], name: 'index_solid_queue_claimed_executions_on_process_id_and_job_id' + end + + create_table 'solid_queue_failed_executions', force: :cascade do |t| + t.bigint 'job_id', null: false + t.text 'error' + t.datetime 'created_at', null: false + t.index ['job_id'], name: 'index_solid_queue_failed_executions_on_job_id', unique: true + end + + create_table 'solid_queue_jobs', force: :cascade do |t| + t.string 'queue_name', null: false + t.string 'class_name', null: false + t.text 'arguments' + t.integer 'priority', default: 0, null: false + t.string 'active_job_id' + t.datetime 'scheduled_at' + t.datetime 'finished_at' + t.string 'concurrency_key' + t.datetime 'created_at', null: false + t.datetime 'updated_at', null: false + t.index ['active_job_id'], name: 'index_solid_queue_jobs_on_active_job_id' + t.index ['class_name'], name: 'index_solid_queue_jobs_on_class_name' + t.index ['finished_at'], name: 'index_solid_queue_jobs_on_finished_at' + t.index %w[queue_name finished_at], name: 'index_solid_queue_jobs_for_filtering' + t.index %w[scheduled_at finished_at], name: 'index_solid_queue_jobs_for_alerting' + end + + create_table 'solid_queue_pauses', force: :cascade do |t| + t.string 'queue_name', null: false + t.datetime 'created_at', null: false + t.index ['queue_name'], name: 'index_solid_queue_pauses_on_queue_name', unique: true + end + + create_table 'solid_queue_processes', force: :cascade do |t| + t.string 'kind', null: false + t.datetime 'last_heartbeat_at', null: false + t.bigint 'supervisor_id' + t.integer 'pid', null: false + t.string 'hostname' + t.text 'metadata' + t.datetime 'created_at', null: false + t.string 'name', null: false + t.index ['last_heartbeat_at'], name: 'index_solid_queue_processes_on_last_heartbeat_at' + t.index %w[name supervisor_id], name: 'index_solid_queue_processes_on_name_and_supervisor_id', unique: true + t.index ['supervisor_id'], name: 'index_solid_queue_processes_on_supervisor_id' + end + + create_table 'solid_queue_ready_executions', force: :cascade do |t| + t.bigint 'job_id', null: false + t.string 'queue_name', null: false + t.integer 'priority', default: 0, null: false + t.datetime 'created_at', null: false + t.index ['job_id'], name: 'index_solid_queue_ready_executions_on_job_id', unique: true + t.index %w[priority job_id], name: 'index_solid_queue_poll_all' + t.index %w[queue_name priority job_id], name: 'index_solid_queue_poll_by_queue' + end + + create_table 'solid_queue_recurring_executions', force: :cascade do |t| + t.bigint 'job_id', null: false + t.string 'task_key', null: false + t.datetime 'run_at', null: false + t.datetime 'created_at', null: false + t.index ['job_id'], name: 'index_solid_queue_recurring_executions_on_job_id', unique: true + t.index %w[task_key run_at], name: 'index_solid_queue_recurring_executions_on_task_key_and_run_at', + unique: true + end + + create_table 'solid_queue_recurring_tasks', force: :cascade do |t| + t.string 'key', null: false + t.string 'schedule', null: false + t.string 'command', limit: 2048 + t.string 'class_name' + t.text 'arguments' + t.string 'queue_name' + t.integer 'priority', default: 0 + t.boolean 'static', default: true, null: false + t.text 'description' + t.datetime 'created_at', null: false + t.datetime 'updated_at', null: false + t.index ['key'], name: 'index_solid_queue_recurring_tasks_on_key', unique: true + t.index ['static'], name: 'index_solid_queue_recurring_tasks_on_static' + end + + create_table 'solid_queue_scheduled_executions', force: :cascade do |t| + t.bigint 'job_id', null: false + t.string 'queue_name', null: false + t.integer 'priority', default: 0, null: false + t.datetime 'scheduled_at', null: false + t.datetime 'created_at', null: false + t.index ['job_id'], name: 'index_solid_queue_scheduled_executions_on_job_id', unique: true + t.index %w[scheduled_at priority job_id], name: 'index_solid_queue_dispatch_all' + end + + create_table 'solid_queue_semaphores', force: :cascade do |t| + t.string 'key', null: false + t.integer 'value', default: 1, null: false + t.datetime 'expires_at', null: false + t.datetime 'created_at', null: false + t.datetime 'updated_at', null: false + t.index ['expires_at'], name: 'index_solid_queue_semaphores_on_expires_at' + t.index %w[key value], name: 'index_solid_queue_semaphores_on_key_and_value' + t.index ['key'], name: 'index_solid_queue_semaphores_on_key', unique: true + end + + add_foreign_key 'solid_queue_blocked_executions', 'solid_queue_jobs', column: 'job_id', on_delete: :cascade + add_foreign_key 'solid_queue_claimed_executions', 'solid_queue_jobs', column: 'job_id', on_delete: :cascade + add_foreign_key 'solid_queue_failed_executions', 'solid_queue_jobs', column: 'job_id', on_delete: :cascade + add_foreign_key 'solid_queue_ready_executions', 'solid_queue_jobs', column: 'job_id', on_delete: :cascade + add_foreign_key 'solid_queue_recurring_executions', 'solid_queue_jobs', column: 'job_id', on_delete: :cascade + add_foreign_key 'solid_queue_scheduled_executions', 'solid_queue_jobs', column: 'job_id', on_delete: :cascade + end +end diff --git a/db/queue_schema.rb b/db/queue_schema.rb new file mode 100644 index 0000000..0cc657e --- /dev/null +++ b/db/queue_schema.rb @@ -0,0 +1,2 @@ +ActiveRecord::Schema[7.1].define(version: 1) do + end diff --git a/db/schema.rb b/db/schema.rb index 2655c6c..2c053f9 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.2].define(version: 2024_11_03_072808) do +ActiveRecord::Schema[7.2].define(version: 2024_11_03_075705) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -59,6 +59,127 @@ ActiveRecord::Schema[7.2].define(version: 2024_11_03_072808) do t.index ["tables_arrangement_id"], name: "index_seats_on_tables_arrangement_id" end + create_table "solid_queue_blocked_executions", force: :cascade do |t| + t.bigint "job_id", null: false + t.string "queue_name", null: false + t.integer "priority", default: 0, null: false + t.string "concurrency_key", null: false + t.datetime "expires_at", null: false + t.datetime "created_at", null: false + t.index ["concurrency_key", "priority", "job_id"], name: "index_solid_queue_blocked_executions_for_release" + t.index ["expires_at", "concurrency_key"], name: "index_solid_queue_blocked_executions_for_maintenance" + t.index ["job_id"], name: "index_solid_queue_blocked_executions_on_job_id", unique: true + end + + create_table "solid_queue_claimed_executions", force: :cascade do |t| + t.bigint "job_id", null: false + t.bigint "process_id" + t.datetime "created_at", null: false + t.index ["job_id"], name: "index_solid_queue_claimed_executions_on_job_id", unique: true + t.index ["process_id", "job_id"], name: "index_solid_queue_claimed_executions_on_process_id_and_job_id" + end + + create_table "solid_queue_failed_executions", force: :cascade do |t| + t.bigint "job_id", null: false + t.text "error" + t.datetime "created_at", null: false + t.index ["job_id"], name: "index_solid_queue_failed_executions_on_job_id", unique: true + end + + create_table "solid_queue_jobs", force: :cascade do |t| + t.string "queue_name", null: false + t.string "class_name", null: false + t.text "arguments" + t.integer "priority", default: 0, null: false + t.string "active_job_id" + t.datetime "scheduled_at" + t.datetime "finished_at" + t.string "concurrency_key" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["active_job_id"], name: "index_solid_queue_jobs_on_active_job_id" + t.index ["class_name"], name: "index_solid_queue_jobs_on_class_name" + t.index ["finished_at"], name: "index_solid_queue_jobs_on_finished_at" + t.index ["queue_name", "finished_at"], name: "index_solid_queue_jobs_for_filtering" + t.index ["scheduled_at", "finished_at"], name: "index_solid_queue_jobs_for_alerting" + end + + create_table "solid_queue_pauses", force: :cascade do |t| + t.string "queue_name", null: false + t.datetime "created_at", null: false + t.index ["queue_name"], name: "index_solid_queue_pauses_on_queue_name", unique: true + end + + create_table "solid_queue_processes", force: :cascade do |t| + t.string "kind", null: false + t.datetime "last_heartbeat_at", null: false + t.bigint "supervisor_id" + t.integer "pid", null: false + t.string "hostname" + t.text "metadata" + t.datetime "created_at", null: false + t.string "name", null: false + t.index ["last_heartbeat_at"], name: "index_solid_queue_processes_on_last_heartbeat_at" + t.index ["name", "supervisor_id"], name: "index_solid_queue_processes_on_name_and_supervisor_id", unique: true + t.index ["supervisor_id"], name: "index_solid_queue_processes_on_supervisor_id" + end + + create_table "solid_queue_ready_executions", force: :cascade do |t| + t.bigint "job_id", null: false + t.string "queue_name", null: false + t.integer "priority", default: 0, null: false + t.datetime "created_at", null: false + t.index ["job_id"], name: "index_solid_queue_ready_executions_on_job_id", unique: true + t.index ["priority", "job_id"], name: "index_solid_queue_poll_all" + t.index ["queue_name", "priority", "job_id"], name: "index_solid_queue_poll_by_queue" + end + + create_table "solid_queue_recurring_executions", force: :cascade do |t| + t.bigint "job_id", null: false + t.string "task_key", null: false + t.datetime "run_at", null: false + t.datetime "created_at", null: false + t.index ["job_id"], name: "index_solid_queue_recurring_executions_on_job_id", unique: true + t.index ["task_key", "run_at"], name: "index_solid_queue_recurring_executions_on_task_key_and_run_at", unique: true + end + + create_table "solid_queue_recurring_tasks", force: :cascade do |t| + t.string "key", null: false + t.string "schedule", null: false + t.string "command", limit: 2048 + t.string "class_name" + t.text "arguments" + t.string "queue_name" + t.integer "priority", default: 0 + t.boolean "static", default: true, null: false + t.text "description" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["key"], name: "index_solid_queue_recurring_tasks_on_key", unique: true + t.index ["static"], name: "index_solid_queue_recurring_tasks_on_static" + end + + create_table "solid_queue_scheduled_executions", force: :cascade do |t| + t.bigint "job_id", null: false + t.string "queue_name", null: false + t.integer "priority", default: 0, null: false + t.datetime "scheduled_at", null: false + t.datetime "created_at", null: false + t.index ["job_id"], name: "index_solid_queue_scheduled_executions_on_job_id", unique: true + t.index ["scheduled_at", "priority", "job_id"], name: "index_solid_queue_dispatch_all" + end + + create_table "solid_queue_semaphores", force: :cascade do |t| + t.string "key", null: false + t.integer "value", default: 1, null: false + t.datetime "expires_at", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["expires_at"], name: "index_solid_queue_semaphores_on_expires_at" + t.index ["key", "value"], name: "index_solid_queue_semaphores_on_key_and_value" + t.index ["key"], name: "index_solid_queue_semaphores_on_key", unique: true + end + create_table "tables_arrangements", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| t.integer "discomfort" t.datetime "created_at", null: false @@ -70,4 +191,10 @@ ActiveRecord::Schema[7.2].define(version: 2024_11_03_072808) do add_foreign_key "guests", "groups" add_foreign_key "seats", "guests" add_foreign_key "seats", "tables_arrangements", on_delete: :cascade + add_foreign_key "solid_queue_blocked_executions", "solid_queue_jobs", column: "job_id", on_delete: :cascade + add_foreign_key "solid_queue_claimed_executions", "solid_queue_jobs", column: "job_id", on_delete: :cascade + add_foreign_key "solid_queue_failed_executions", "solid_queue_jobs", column: "job_id", on_delete: :cascade + add_foreign_key "solid_queue_ready_executions", "solid_queue_jobs", column: "job_id", on_delete: :cascade + add_foreign_key "solid_queue_recurring_executions", "solid_queue_jobs", column: "job_id", on_delete: :cascade + add_foreign_key "solid_queue_scheduled_executions", "solid_queue_jobs", column: "job_id", on_delete: :cascade end -- 2.47.1 From 3be71406f85135104a96eda56b3406718b43d437 Mon Sep 17 00:00:00 2001 From: Manuel Bustillo Date: Sun, 3 Nov 2024 08:01:59 +0000 Subject: [PATCH 2/6] Add copyright notice --- db/migrate/20241103075705_solid_queue_install.rb | 2 ++ db/queue_schema.rb | 2 ++ db/schema.rb | 2 ++ 3 files changed, 6 insertions(+) diff --git a/db/migrate/20241103075705_solid_queue_install.rb b/db/migrate/20241103075705_solid_queue_install.rb index 4dfb4d0..3a0b744 100644 --- a/db/migrate/20241103075705_solid_queue_install.rb +++ b/db/migrate/20241103075705_solid_queue_install.rb @@ -1,3 +1,5 @@ +# Copyright (C) 2024 Manuel Bustillo + class SolidQueueInstall < ActiveRecord::Migration[7.2] def change create_table 'solid_queue_blocked_executions', force: :cascade do |t| diff --git a/db/queue_schema.rb b/db/queue_schema.rb index 0cc657e..24748f4 100644 --- a/db/queue_schema.rb +++ b/db/queue_schema.rb @@ -1,2 +1,4 @@ +# Copyright (C) 2024 Manuel Bustillo + ActiveRecord::Schema[7.1].define(version: 1) do end diff --git a/db/schema.rb b/db/schema.rb index 2c053f9..0e3974d 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -1,3 +1,5 @@ +# Copyright (C) 2024 Manuel Bustillo + # This file is auto-generated from the current state of the database. Instead # of editing this file, please use the migrations feature of Active Record to # incrementally modify your database, and then regenerate this schema definition. -- 2.47.1 From 8d09fd733facd8fb7f83a6b33371099782a4ff63 Mon Sep 17 00:00:00 2001 From: Manuel Bustillo Date: Sun, 3 Nov 2024 09:05:51 +0100 Subject: [PATCH 3/6] Define a job to generate table simulations --- app/jobs/table_simulator_job.rb | 20 ++++++++++++++++++++ lib/tasks/vns.rake | 15 +-------------- 2 files changed, 21 insertions(+), 14 deletions(-) create mode 100644 app/jobs/table_simulator_job.rb diff --git a/app/jobs/table_simulator_job.rb b/app/jobs/table_simulator_job.rb new file mode 100644 index 0000000..574c4a5 --- /dev/null +++ b/app/jobs/table_simulator_job.rb @@ -0,0 +1,20 @@ +class TableSimulatorJob < ApplicationJob + queue_as :default + + def perform(*_args) + engine = VNS::Engine.new + + engine.add_perturbation(Tables::Swap) + + initial_solution = Tables::Distribution.new(min_per_table: 8, max_per_table: 10) + initial_solution.random_distribution(Guest.all.shuffle) + + engine.initial_solution = initial_solution + + engine.target_function(&:discomfort) + + best_solution = engine.run + + best_solution.save! + end +end diff --git a/lib/tasks/vns.rake b/lib/tasks/vns.rake index c8e6b50..d3a31d0 100644 --- a/lib/tasks/vns.rake +++ b/lib/tasks/vns.rake @@ -1,18 +1,5 @@ namespace :vns do task distribute_tables: :environment do - engine = VNS::Engine.new - - engine.add_perturbation(Tables::Swap) - - initial_solution = Tables::Distribution.new(min_per_table: 8, max_per_table: 10) - initial_solution.random_distribution(Guest.all.shuffle) - - engine.initial_solution = initial_solution - - engine.target_function(&:discomfort) - - best_solution = engine.run - - best_solution.save! + end end -- 2.47.1 From 50a5c90728f7dda18be80391d22570828173686a Mon Sep 17 00:00:00 2001 From: Manuel Bustillo Date: Sun, 3 Nov 2024 09:15:48 +0100 Subject: [PATCH 4/6] Only include potential guests in the simulation --- app/jobs/table_simulator_job.rb | 2 +- app/models/guest.rb | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/app/jobs/table_simulator_job.rb b/app/jobs/table_simulator_job.rb index 574c4a5..f0e99f0 100644 --- a/app/jobs/table_simulator_job.rb +++ b/app/jobs/table_simulator_job.rb @@ -7,7 +7,7 @@ class TableSimulatorJob < ApplicationJob engine.add_perturbation(Tables::Swap) initial_solution = Tables::Distribution.new(min_per_table: 8, max_per_table: 10) - initial_solution.random_distribution(Guest.all.shuffle) + initial_solution.random_distribution(Guest.potential.shuffle) engine.initial_solution = initial_solution diff --git a/app/models/guest.rb b/app/models/guest.rb index 39d27b3..87680bf 100644 --- a/app/models/guest.rb +++ b/app/models/guest.rb @@ -8,9 +8,11 @@ class Guest < ApplicationRecord invited: 10, confirmed: 20, declined: 30, - tentative: 40, + tentative: 40 } + scope :potential, -> { where.not(status: %i[declined considered]) } + def full_name "#{first_name} #{last_name}" end -- 2.47.1 From 35bf272ac8ca4ded4c6090aaa4a9d8a5e417eae5 Mon Sep 17 00:00:00 2001 From: Manuel Bustillo Date: Sun, 3 Nov 2024 09:25:50 +0100 Subject: [PATCH 5/6] Restart simulations whenever a guest changes their invitation status --- app/controllers/guests_controller.rb | 2 +- app/use_cases/guests/update_use_case.rb | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 app/use_cases/guests/update_use_case.rb diff --git a/app/controllers/guests_controller.rb b/app/controllers/guests_controller.rb index ecf5d34..de6127d 100644 --- a/app/controllers/guests_controller.rb +++ b/app/controllers/guests_controller.rb @@ -26,7 +26,7 @@ class GuestsController < ApplicationController end def bulk_update - Guest.where(id: params[:guest_ids]).update!(params.require(:properties).permit(:status)) + Guests::UpdateUseCase.new(guest_ids: params[:guest_ids], params: params.require(:properties).permit(:status)).call render json: {}, status: :ok end end diff --git a/app/use_cases/guests/update_use_case.rb b/app/use_cases/guests/update_use_case.rb new file mode 100644 index 0000000..bcd4f3e --- /dev/null +++ b/app/use_cases/guests/update_use_case.rb @@ -0,0 +1,12 @@ +module Guests + class UpdateUseCase + def initialize(guest_ids:, params:) + Guest.where(id: guest_ids).update!(params) + + # TODO: Not all status transitions may require a table re-arrangement + TablesArrangement.delete_all + + ActiveJob.perform_all_later(50.times.map { TableSimulatorJob.new }) + end + end +end -- 2.47.1 From 86e982164d3f5a65e5ecc78d79a18e765c6b7f31 Mon Sep 17 00:00:00 2001 From: Manuel Bustillo Date: Sun, 3 Nov 2024 08:31:58 +0000 Subject: [PATCH 6/6] Add copyright notice --- app/jobs/table_simulator_job.rb | 2 ++ app/use_cases/guests/update_use_case.rb | 2 ++ db/migrate/20241103072808_add_name_to_tables_arrangements.rb | 2 ++ 3 files changed, 6 insertions(+) diff --git a/app/jobs/table_simulator_job.rb b/app/jobs/table_simulator_job.rb index f0e99f0..0b90396 100644 --- a/app/jobs/table_simulator_job.rb +++ b/app/jobs/table_simulator_job.rb @@ -1,3 +1,5 @@ +# Copyright (C) 2024 Manuel Bustillo + class TableSimulatorJob < ApplicationJob queue_as :default diff --git a/app/use_cases/guests/update_use_case.rb b/app/use_cases/guests/update_use_case.rb index bcd4f3e..9a5cc66 100644 --- a/app/use_cases/guests/update_use_case.rb +++ b/app/use_cases/guests/update_use_case.rb @@ -1,3 +1,5 @@ +# Copyright (C) 2024 Manuel Bustillo + module Guests class UpdateUseCase def initialize(guest_ids:, params:) diff --git a/db/migrate/20241103072808_add_name_to_tables_arrangements.rb b/db/migrate/20241103072808_add_name_to_tables_arrangements.rb index 7f38f58..53dd4fa 100644 --- a/db/migrate/20241103072808_add_name_to_tables_arrangements.rb +++ b/db/migrate/20241103072808_add_name_to_tables_arrangements.rb @@ -1,3 +1,5 @@ +# Copyright (C) 2024 Manuel Bustillo + class AddNameToTablesArrangements < ActiveRecord::Migration[7.2] def change add_column :tables_arrangements, :name, :string, null: false -- 2.47.1