From 87b267998bd304d4c2749e3e75af18843b35f9d3 Mon Sep 17 00:00:00 2001
From: Riccardo Graziosi <31478034+riggraz@users.noreply.github.com>
Date: Mon, 25 Nov 2024 18:56:24 +0100
Subject: [PATCH] Fix sidekiq cron SendRecapEmails job scheduling creation
(#442)
---
app/controllers/invitations_controller.rb | 2 +-
app/jobs/send_recap_emails.rb | 9 +++---
app/views/devise/registrations/edit.html.erb | 4 ++-
config/initializers/sidekiq.rb | 29 ++++++++++++++++++--
config/initializers/sidekiq_cron.rb | 12 --------
config/sidekiq_cron_schedule.yml | 9 ------
6 files changed, 35 insertions(+), 30 deletions(-)
delete mode 100644 config/initializers/sidekiq_cron.rb
delete mode 100644 config/sidekiq_cron_schedule.yml
diff --git a/app/controllers/invitations_controller.rb b/app/controllers/invitations_controller.rb
index da218f17..d5b2d2f2 100644
--- a/app/controllers/invitations_controller.rb
+++ b/app/controllers/invitations_controller.rb
@@ -32,7 +32,7 @@ class InvitationsController < ApplicationController
)
)
- InvitationMailer.invite(invitation: invitation, subject: subject, body: body_with_link).deliver_now
+ InvitationMailer.invite(invitation: invitation, subject: subject, body: body_with_link).deliver_later
num_invitations_sent += 1
end
diff --git a/app/jobs/send_recap_emails.rb b/app/jobs/send_recap_emails.rb
index b1fc352d..40dcc675 100644
--- a/app/jobs/send_recap_emails.rb
+++ b/app/jobs/send_recap_emails.rb
@@ -3,10 +3,11 @@ class SendRecapEmails < ActiveJob::Base
def perform(*args)
# Fix times to 15:00 UTC
- time_now = Time.now.utc.change(hour: args[0], min: 0, sec: 0)
- one_day_ago = 1.day.ago.utc.change(hour: args[0], min: 0, sec: 0)
- one_week_ago = 1.week.ago.utc.change(hour: args[0], min: 0, sec: 0)
- one_month_ago = 1.month.ago.utc.change(hour: args[0], min: 0, sec: 0)
+ hour = 15
+ time_now = Time.now.utc.change(hour: hour, min: 0, sec: 0)
+ one_day_ago = 1.day.ago.utc.change(hour: hour, min: 0, sec: 0)
+ one_week_ago = 1.week.ago.utc.change(hour: hour, min: 0, sec: 0)
+ one_month_ago = 1.month.ago.utc.change(hour: hour, min: 0, sec: 0)
# Get tenants with active subscriptions
tbs = TenantBilling.unscoped.all
diff --git a/app/views/devise/registrations/edit.html.erb b/app/views/devise/registrations/edit.html.erb
index f25d3a0c..dcf8c256 100644
--- a/app/views/devise/registrations/edit.html.erb
+++ b/app/views/devise/registrations/edit.html.erb
@@ -77,7 +77,9 @@
<% else %>
-
You have to enable Sidekiq to receive recap notifications.
+ <% if not Rails.application.multi_tenancy? %>
+ You have to enable Sidekiq to receive recap notifications.
+ <% end %>
<% end %>
diff --git a/config/initializers/sidekiq.rb b/config/initializers/sidekiq.rb
index f6811a9a..fbf3ed8a 100644
--- a/config/initializers/sidekiq.rb
+++ b/config/initializers/sidekiq.rb
@@ -1,6 +1,6 @@
-if ENV["ACTIVE_JOB_BACKEND"] == "sidekiq"
+if ENV['ACTIVE_JOB_BACKEND'] == 'sidekiq'
Sidekiq.configure_server do |config|
- redis_url = ENV["REDIS_URL"].dup
+ redis_url = ENV['REDIS_URL'].dup
redis_url.insert(8, ":#{ENV['REDIS_PASSWORD']}@")
config.logger.level = Rails.env.production? ? Logger::INFO : Logger::DEBUG
@@ -8,10 +8,33 @@ if ENV["ACTIVE_JOB_BACKEND"] == "sidekiq"
end
Sidekiq.configure_client do |config|
- redis_url = ENV["REDIS_URL"].dup
+ redis_url = ENV['REDIS_URL'].dup
redis_url.insert(8, ":#{ENV['REDIS_PASSWORD']}@")
config.logger.level = Rails.env.production? ? Logger::INFO : Logger::DEBUG
config.redis = { url: redis_url }
end
+
+ # Sidekiq Cron
+ if ENV['IS_SIDEKIQ'] == 'true'
+ Sidekiq::Cron.configure do |config|
+
+ # config.cron_schedule_file doesn't work for some reason
+ # so we have to create the cron jobs manually
+ unless Sidekiq::Cron::Job.find('SendRecapEmails')
+ cron_expression = '0 15 * * *' # Every day at 15:00
+ # cron_expression = '*/30 * * * * *' # Every 30 seconds (for testing)
+ Sidekiq::Cron::Job.create(name: 'SendRecapEmails Job', cron: cron_expression, class: 'SendRecapEmails')
+ end
+
+ config.cron_poll_interval = Rails.env.production? ? 30 : 15
+ config.cron_history_size = 50
+ config.default_namespace = 'default'
+ config.natural_cron_parsing_mode = :strict
+
+ # Handles the case when the Sidekiq process was down for a while and the cron job should have run (set to 10 minutes, i.e. 600 seconds)
+ # This could happen during the deployment of a new version of the application
+ config.reschedule_grace_period = 600
+ end
+ end
end
\ No newline at end of file
diff --git a/config/initializers/sidekiq_cron.rb b/config/initializers/sidekiq_cron.rb
deleted file mode 100644
index 84caf634..00000000
--- a/config/initializers/sidekiq_cron.rb
+++ /dev/null
@@ -1,12 +0,0 @@
-Sidekiq::Cron.configure do |config|
- config.cron_schedule_file = 'config/sidekiq_cron_schedule.yml'
-
- config.cron_poll_interval = 30
- config.cron_history_size = 50
- config.default_namespace = 'default'
- config.natural_cron_parsing_mode = :strict
-
- # Handles the case when the Sidekiq process was down for a while and the cron job should have run (set to 10 minutes, i.e. 600 seconds)
- # This could happen during the deployment of a new version of the application
- config.reschedule_grace_period = 600
-end
\ No newline at end of file
diff --git a/config/sidekiq_cron_schedule.yml b/config/sidekiq_cron_schedule.yml
deleted file mode 100644
index 59f27a8b..00000000
--- a/config/sidekiq_cron_schedule.yml
+++ /dev/null
@@ -1,9 +0,0 @@
-# For crontab syntax, see https://crontab.guru/
-
-send_recap_emails:
- cron: "0 15 * * *" # At 15:00 every day
- # cron: "*/30 * * * * *" # Execute every 30 seconds (for testing purposes)
- class: "SendRecapEmails"
- queue: default
- args:
- hour: 15 # This should be in sync with the "cron" time
\ No newline at end of file