Get rid of unnecessary touch jobs

touch_all on 10K cards took 6ms in testing
This commit is contained in:
David Heinemeier Hansson
2025-11-02 14:15:53 +01:00
parent 105585c26c
commit 966aa51a37
7 changed files with 15 additions and 47 deletions
-7
View File
@@ -1,7 +0,0 @@
class Card::TouchAllJob < ApplicationJob
queue_as :backend
def perform(container)
container.cards.in_batches(&:touch_all)
end
end
+1 -6
View File
@@ -4,11 +4,6 @@ module Collection::Cards
included do
has_many :cards, dependent: :destroy
after_update_commit :touch_all_cards_later, if: :saved_change_to_name?
after_update_commit -> { cards.touch_all }, if: :saved_change_to_name?
end
private
def touch_all_cards_later
Card::TouchAllJob.perform_later(self)
end
end
+2 -14
View File
@@ -8,23 +8,11 @@ class Column < ApplicationRecord
validates :color, presence: true
before_validation :set_default_color
after_save_commit :touch_all_cards_later, if: :should_invalidate_cards?
after_destroy_commit :touch_all_collection_cards
after_save_commit -> { cards.touch_all }, if: -> { saved_change_to_name? || saved_change_to_color? }
after_destroy_commit -> { collection.cards.touch_all }
private
def set_default_color
self.color ||= Card::DEFAULT_COLOR
end
def touch_all_cards_later
Card::TouchAllJob.perform_later(self)
end
def should_invalidate_cards?
saved_change_to_name? || saved_change_to_color?
end
def touch_all_collection_cards
Card::TouchAllJob.perform_later(collection)
end
end
+1 -6
View File
@@ -1,16 +1,11 @@
class Entropy < ApplicationRecord
belongs_to :container, polymorphic: true
after_commit :touch_all_cards_later
after_commit -> { container.cards.touch_all }
class << self
def default
Account.sole.default_entropy
end
end
private
def touch_all_cards_later
Card::TouchAllJob.perform_later(container)
end
end
+2 -2
View File
@@ -4,11 +4,11 @@ class Collection::CardsTest < ActiveSupport::TestCase
test "touch cards when the name changes" do
collection = collections(:writebook)
assert_enqueued_with(job: Card::TouchAllJob) do
assert_changes -> { collection.cards.first.updated_at } do
collection.update!(name: "New Name")
end
assert_no_enqueued_jobs(only: Card::TouchAllJob) do
assert_no_changes -> { collection.cards.first.updated_at } do
collection.update!(updated_at: 1.hour.from_now)
end
end
+4 -4
View File
@@ -10,15 +10,15 @@ class ColumnTest < ActiveSupport::TestCase
test "touch all the cards when the name or color changes" do
column = columns(:writebook_triage)
assert_enqueued_with(job: Card::TouchAllJob) do
assert_changes -> { column.cards.first.updated_at } do
column.update!(name: "New Name")
end
assert_enqueued_with(job: Card::TouchAllJob) do
assert_changes -> { column.cards.first.updated_at } do
column.update!(color: "#FF0000")
end
assert_no_enqueued_jobs(only: Card::TouchAllJob) do
assert_no_changes -> { column.cards.first.updated_at } do
column.update!(updated_at: 1.hour.from_now)
end
end
@@ -26,7 +26,7 @@ class ColumnTest < ActiveSupport::TestCase
test "touch all collection cards when column is destroyed" do
column = columns(:writebook_triage)
assert_enqueued_with(job: Card::TouchAllJob, args: [ column.collection ]) do
assert_changes -> { column.collection.cards.first.updated_at } do
column.destroy
end
end
+5 -8
View File
@@ -1,20 +1,17 @@
require "test_helper"
class Entropy::Test < ActiveSupport::TestCase
test "touch cards when entropy changes for collection container" do
collection = collections(:writebook)
config = collection.entropy || collection.create_entropy!(auto_postpone_period: 30.days)
assert_enqueued_with(job: Card::TouchAllJob, args: [ collection ]) do
config.update!(auto_postpone_period: 15.days)
test "touch cards when entropy changes for collection" do
assert_changes -> { collections(:writebook).cards.first.updated_at } do
collections(:writebook).entropy.update!(auto_postpone_period: 15.days)
end
end
test "touch cards when entropy changes for account container" do
account = Account.sole
assert_enqueued_with(job: Card::TouchAllJob, args: [ account ]) do
account.default_entropy.update!(auto_postpone_period: 45.days)
assert_changes -> { account.cards.first.updated_at } do
collections(:writebook).entropy.update!(auto_postpone_period: 15.days)
end
end
end