scheduled_statuses_scheduler.rb 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # frozen_string_literal: true
  2. class Scheduler::ScheduledStatusesScheduler
  3. include Sidekiq::Worker
  4. sidekiq_options retry: 0, lock: :until_executed, lock_ttl: 1.hour.to_i
  5. def perform
  6. publish_scheduled_statuses!
  7. publish_scheduled_announcements!
  8. unpublish_expired_announcements!
  9. end
  10. private
  11. def publish_scheduled_statuses!
  12. due_statuses.find_each do |scheduled_status|
  13. PublishScheduledStatusWorker.perform_at(scheduled_status.scheduled_at, scheduled_status.id)
  14. end
  15. end
  16. def due_statuses
  17. ScheduledStatus.where('scheduled_at <= ?', Time.now.utc + PostStatusService::MIN_SCHEDULE_OFFSET)
  18. end
  19. def publish_scheduled_announcements!
  20. due_announcements.find_each do |announcement|
  21. PublishScheduledAnnouncementWorker.perform_at(announcement.scheduled_at, announcement.id)
  22. end
  23. end
  24. def due_announcements
  25. Announcement.unpublished.where('scheduled_at IS NOT NULL AND scheduled_at <= ?', Time.now.utc + PostStatusService::MIN_SCHEDULE_OFFSET)
  26. end
  27. def unpublish_expired_announcements!
  28. expired_announcements.in_batches.update_all(published: false, scheduled_at: nil)
  29. end
  30. def expired_announcements
  31. Announcement.published.where('ends_at IS NOT NULL AND ends_at <= ?', Time.now.utc)
  32. end
  33. end