feat: switch startup task to Sidekiq with Redis cache

- Update schedule tasks to use Sidekiq and Redis
- Improve task scheduling with caching
- Remove unnecessary code and error handling

This change enhances the reliability and performance of the startup task, leveraging the power of Sidekiq and Redis for background job processing and caching. The improvements ensure a smoother and more efficient application startup experience.
This commit is contained in:
songtianlun 2025-01-25 01:49:28 +08:00
parent dd37e2835b
commit 7ebf9cefae

View File

@ -1,10 +1,12 @@
# config/initializers/schedule_tasks.rb
Rails.application.config.after_initialize do
if Rails.env.production? && !ENV["RAILS_BUILD"]
begin
unless Rails.cache.read("startup_task_running")
Rails.cache.write("startup_task_running", true, expires_in: 1.hour)
redis_key = "startup_task_running"
unless Sidekiq.redis { |conn| conn.get(redis_key) }
Sidekiq.redis do |conn|
conn.setex(redis_key, 1.hour.to_i, "1")
end
RefreshSitemapWorker.perform_async
@ -13,7 +15,7 @@ Rails.application.config.after_initialize do
rescue => e
Rails.logger.error "Error scheduling startup task: #{e.message}"
ensure
Rails.cache.delete("startup_task_running")
Sidekiq.redis { |conn| conn.del(redis_key) }
end
end
end
end