From 98ea7e3c9df28c9be87977950ae0f812443b76ce Mon Sep 17 00:00:00 2001 From: 24c02 <163450896+24c02@users.noreply.github.com> Date: Thu, 22 Jan 2026 15:55:04 -0500 Subject: [PATCH] now or later? --- app/controllers/letter/batches_controller.rb | 2 +- app/controllers/letter/queues_controller.rb | 4 ++-- app/controllers/letters_controller.rb | 2 +- app/controllers/public/maps_controller.rb | 7 ++++--- app/controllers/tasks_controller.rb | 7 +++++-- app/jobs/public/update_map_data_job.rb | 5 +++++ app/models/batch.rb | 2 +- 7 files changed, 19 insertions(+), 10 deletions(-) diff --git a/app/controllers/letter/batches_controller.rb b/app/controllers/letter/batches_controller.rb index cf90cee..5e5279b 100644 --- a/app/controllers/letter/batches_controller.rb +++ b/app/controllers/letter/batches_controller.rb @@ -154,7 +154,7 @@ class Letter::BatchesController < BaseBatchesController @batch.letters.each do |letter| letter.mark_mailed! if letter.may_mark_mailed? end - User::UpdateTasksJob.perform_now(current_user) + User::UpdateTasksJob.perform_later(current_user) redirect_to letter_batch_path(@batch), notice: "All letters have been marked as mailed." else redirect_to letter_batch_path(@batch), alert: "Cannot mark letters as mailed. Batch must be processed." diff --git a/app/controllers/letter/queues_controller.rb b/app/controllers/letter/queues_controller.rb index 05fb093..d3ef617 100644 --- a/app/controllers/letter/queues_controller.rb +++ b/app/controllers/letter/queues_controller.rb @@ -69,7 +69,7 @@ class Letter::QueuesController < ApplicationController end limit = params[:limit].presence&.to_i batch = @letter_queue.make_batch(user: current_user, limit:) - User::UpdateTasksJob.perform_now(current_user) + User::UpdateTasksJob.perform_later(current_user) flash[:success] = "now do something with it!" redirect_to process_letter_batch_path(batch, uft: @letter_queue.user_facing_title, template: @letter_queue.template) end @@ -102,7 +102,7 @@ class Letter::QueuesController < ApplicationController end # Update user tasks after marking letters as mailed - User::UpdateTasksJob.perform_now(current_user) if marked_count > 0 + User::UpdateTasksJob.perform_later(current_user) if marked_count > 0 if failed_letters.any? flash[:alert] = "Marked #{marked_count} letters as mailed, but failed to mark #{failed_letters.count} letters: #{failed_letters.join(", ")}" diff --git a/app/controllers/letters_controller.rb b/app/controllers/letters_controller.rb index de2fd99..352562a 100644 --- a/app/controllers/letters_controller.rb +++ b/app/controllers/letters_controller.rb @@ -132,7 +132,7 @@ class LettersController < ApplicationController def mark_mailed authorize @letter, :mark_mailed? if @letter.mark_mailed! - User::UpdateTasksJob.perform_now(current_user) + User::UpdateTasksJob.perform_later(current_user) redirect_to @letter, notice: "Letter has been marked as mailed." else redirect_to @letter, alert: "Could not mark letter as mailed: #{@letter.errors.full_messages.join(", ")}" diff --git a/app/controllers/public/maps_controller.rb b/app/controllers/public/maps_controller.rb index 5d79289..c3f0ee8 100644 --- a/app/controllers/public/maps_controller.rb +++ b/app/controllers/public/maps_controller.rb @@ -7,9 +7,10 @@ module Public def show @return_path = public_root_path - @letters_data = Rails.cache.fetch("map_data") do - # If cache is empty, run the job synchronously as a fallback - Public::UpdateMapDataJob.perform_now + @letters_data = Rails.cache.read("map_data") + if @letters_data.nil? + Public::UpdateMapDataJob.perform_later + @letters_data = [] end end end diff --git a/app/controllers/tasks_controller.rb b/app/controllers/tasks_controller.rb index 987a2f4..08ab194 100644 --- a/app/controllers/tasks_controller.rb +++ b/app/controllers/tasks_controller.rb @@ -11,12 +11,15 @@ class TasksController < ApplicationController end def refresh - User::UpdateTasksJob.perform_now(current_user) + User::UpdateTasksJob.perform_later(current_user) redirect_to tasks_path end def find_tasks @tasks = Rails.cache.read("user_tasks/#{current_user.id}") - @tasks ||= User::UpdateTasksJob.perform_now(current_user) + if @tasks.nil? + User::UpdateTasksJob.perform_later(current_user) + @tasks = [] + end end end diff --git a/app/jobs/public/update_map_data_job.rb b/app/jobs/public/update_map_data_job.rb index 1eb6696..0510dcd 100644 --- a/app/jobs/public/update_map_data_job.rb +++ b/app/jobs/public/update_map_data_job.rb @@ -1,6 +1,11 @@ class Public::UpdateMapDataJob < ApplicationJob queue_as :default + good_job_control_concurrency_with( + total_limit: 1, + key: "update_map_data" + ) + def perform map_data = fetch_recent_letters_data Rails.cache.write("map_data", map_data, expires_in: 1.hour) diff --git a/app/models/batch.rb b/app/models/batch.rb index 6737f3b..81c3cad 100644 --- a/app/models/batch.rb +++ b/app/models/batch.rb @@ -65,7 +65,7 @@ class Batch < ApplicationRecord event :mark_processed do transitions from: :fields_mapped, to: :processed after do - User::UpdateTasksJob.perform_now(user) + User::UpdateTasksJob.perform_later(user) end end end