Files
chatwoot/enterprise/lib/tasks/search.rake
Akshay Borade f590539b9b fix: Search rake task causing Rails boot error (#12416)
## Description

This PR sets up an `Enterprise::Railtie` to correctly register rake
tasks in the `enterprise` namespace.
Previously, rake tasks under `enterprise/lib/tasks` were being eagerly
loaded at Rails boot, causing `undefined method 'namespace'` errors.

With this change, rake tasks are now registered only in the rake
context, avoiding boot-time issues and ensuring they are discoverable
with `bin/rake -T`.

**Tasks added:**

* `search:all` → Reindex messages for all accounts
* `search:account[ID]` → Reindex messages for a specific account

Fixes: #12414


Co-authored-by: Sojan Jose <sojan@pepalo.com>
2025-09-15 22:21:59 +05:30

45 lines
1.2 KiB
Ruby

namespace :search do
desc 'Reindex messages for all accounts'
task all: :environment do
next unless check_opensearch_config
puts 'Starting reindex for all accounts...'
account_count = Account.count
puts "Found #{account_count} accounts"
Account.find_each.with_index(1) do |account, index|
puts "[#{index}/#{account_count}] Reindexing messages for account #{account.id}"
reindex_account(account)
end
puts 'Reindex task queued for all accounts'
end
desc 'Reindex messages for a specific account: rake search:account ACCOUNT_ID=1'
task account: :environment do
next unless check_opensearch_config
account_id = ENV.fetch('ACCOUNT_ID', nil)
account = Account.find_by(id: account_id)
if account.nil?
puts 'Please provide a valid account ID. Account not found'
next
end
puts "Reindexing messages for account #{account.id}"
reindex_account(account)
end
end
def check_opensearch_config
if ENV['OPENSEARCH_URL'].blank?
puts 'Skipping reindex as OPENSEARCH_URL is not configured'
return false
end
true
end
def reindex_account(account)
Messages::ReindexService.new(account: account).perform
puts "Reindex task queued for account #{account.id}"
end