feat: add startup scheduling for production
- Schedule RefreshSitemapWorker after initialization - Implement error handling for scheduling task - Use Rails cache to prevent multiple tasks running simultaneously This commit introduces a mechanism that schedules the RefreshSitemapWorker to run once after the application starts in production. It ensures that the task does not run multiple times concurrently by using a cache key. Error handling is included to log any failures in scheduling the task, improving overall reliability.
This commit is contained in:
parent
032ff0552a
commit
84c224cf8d
19
config/initializers/schedule_tasks.rb
Normal file
19
config/initializers/schedule_tasks.rb
Normal file
@ -0,0 +1,19 @@
|
||||
# config/initializers/schedule_tasks.rb
|
||||
|
||||
Rails.application.config.after_initialize do
|
||||
if Rails.env.production?
|
||||
begin
|
||||
unless Rails.cache.read("startup_task_running")
|
||||
Rails.cache.write("startup_task_running", true, expires_in: 1.hour)
|
||||
|
||||
RefreshSitemapWorker.perform_async
|
||||
|
||||
Rails.logger.info "Startup task (RefreshSitemapWorker) scheduled successfully"
|
||||
end
|
||||
rescue => e
|
||||
Rails.logger.error "Error scheduling startup task: #{e.message}"
|
||||
ensure
|
||||
Rails.cache.delete("startup_task_running")
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue
Block a user