From 00eb5b152a198c16e040df794ff3a26e466c0971 Mon Sep 17 00:00:00 2001 From: Pranav Raj S Date: Mon, 18 Dec 2023 17:29:02 -0800 Subject: [PATCH] chore(migration): Re-run database migration job for caching. (#8581) A support request that came to Chatwoot Cloud revealed that the job was timed prematurely. The default timeout for Sidekiq queues was set as 25 minutes in sidekiq.yml file. The cache was not created properly for the accounts with more than 100k conversations. This change removes the cache logic in the previous migration and creates a new migration with a new job which processes conversations in batch. --- .../conversation_batch_cache_label_job.rb | 14 ++++++++++++++ .../migration/conversation_cache_label_job.rb | 11 ++--------- .../20231211010807_add_cached_labels_list.rb | 12 ------------ .../20231219000743_re_run_cache_label_job.rb | 16 ++++++++++++++++ db/schema.rb | 2 +- 5 files changed, 33 insertions(+), 22 deletions(-) create mode 100644 app/jobs/migration/conversation_batch_cache_label_job.rb create mode 100644 db/migrate/20231219000743_re_run_cache_label_job.rb diff --git a/app/jobs/migration/conversation_batch_cache_label_job.rb b/app/jobs/migration/conversation_batch_cache_label_job.rb new file mode 100644 index 000000000..511ee5650 --- /dev/null +++ b/app/jobs/migration/conversation_batch_cache_label_job.rb @@ -0,0 +1,14 @@ +class Migration::ConversationBatchCacheLabelJob < ApplicationJob + queue_as :async_database_migration + + # To cache the label, we simply access it from the object and save it. Anytime the object is + # saved in the future, ActsAsTaggable will automatically recompute it. This process is done + # initially when the user has not performed any action. + # Reference: https://github.com/mbleigh/acts-as-taggable-on/wiki/Caching + def perform(conversation_batch) + conversation_batch.each do |conversation| + conversation.label_list + conversation.save! + end + end +end diff --git a/app/jobs/migration/conversation_cache_label_job.rb b/app/jobs/migration/conversation_cache_label_job.rb index 1ae5a3b70..1cdcf9fee 100644 --- a/app/jobs/migration/conversation_cache_label_job.rb +++ b/app/jobs/migration/conversation_cache_label_job.rb @@ -1,16 +1,9 @@ class Migration::ConversationCacheLabelJob < ApplicationJob queue_as :async_database_migration - # To cache the label, we simply access it from the object and save it. Anytime the object is - # saved in the future, ActsAsTaggable will automatically recompute it. This process is done - # initially when the user has not performed any action. - # Reference: https://github.com/mbleigh/acts-as-taggable-on/wiki/Caching def perform(account) - account.conversations.find_in_batches do |conversation_batch| - conversation_batch.each do |conversation| - conversation.label_list - conversation.save! - end + account.conversations.find_in_batches(batch_size: 100) do |conversation_batch| + Migration::ConversationBatchCacheLabelJob.perform_later(conversation_batch) end end end diff --git a/db/migrate/20231211010807_add_cached_labels_list.rb b/db/migrate/20231211010807_add_cached_labels_list.rb index 02f01a7ea..026bcf7d1 100644 --- a/db/migrate/20231211010807_add_cached_labels_list.rb +++ b/db/migrate/20231211010807_add_cached_labels_list.rb @@ -3,17 +3,5 @@ class AddCachedLabelsList < ActiveRecord::Migration[7.0] add_column :conversations, :cached_label_list, :string Conversation.reset_column_information ActsAsTaggableOn::Taggable::Cache.included(Conversation) - - update_exisiting_conversations - end - - private - - def update_exisiting_conversations - ::Account.find_in_batches do |account_batch| - account_batch.each do |account| - Migration::ConversationCacheLabelJob.perform_later(account) - end - end end end diff --git a/db/migrate/20231219000743_re_run_cache_label_job.rb b/db/migrate/20231219000743_re_run_cache_label_job.rb new file mode 100644 index 000000000..ebe553f1c --- /dev/null +++ b/db/migrate/20231219000743_re_run_cache_label_job.rb @@ -0,0 +1,16 @@ +class ReRunCacheLabelJob < ActiveRecord::Migration[7.0] + def change + update_exisiting_conversations + end + + private + + def update_exisiting_conversations + # Run label migrations on the accounts that are not suspended + ::Account.active.find_in_batches do |account_batch| + account_batch.each do |account| + Migration::ConversationCacheLabelJob.perform_later(account) + end + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 61fd7e0d3..373d81918 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.0].define(version: 2023_12_11_010807) do +ActiveRecord::Schema[7.0].define(version: 2023_12_19_000743) do # These are extensions that must be enabled in order to support this database enable_extension "pg_stat_statements" enable_extension "pg_trgm"