Skip to content

Commit 688121f

Browse files
aagrawalrtslAyushi Agrawal
andauthored
fixing scheduler for precription summary refresh cron (#5777)
**Story card:** [SIMPLEBACK-77](SIMPLEBACK_77_fix_prescription_summary_refresh_job) ## Because Scheduler for refreshing reporting_patient_prescriptions is expected to do a refresh of last 3 months dynamically. But it was scheduled to run for fixed dates ## This addresses From the scheduler we are passing how many months in the past we want a refresh for, which will be evaluated in the rake task to perform a refresh ## Test instructions rake reporting:refresh_partitioned_table[#{reporting_table_name}, #{months_back}] This should perform a refresh for the table passed as arg[1], for those many months in the past as passed under arg[2] --------- Co-authored-by: Ayushi Agrawal <ayushiagrawal@RTSL-P172G93770.local>
1 parent 26f823f commit 688121f

2 files changed

Lines changed: 24 additions & 18 deletions

File tree

config/schedule.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,7 @@ def local(time)
140140
end
141141

142142
every 1.month, at: local("06:00 am"), roles: [:cron] do
143-
refresh_months = (0..3).map { |i| Date.current.beginning_of_month - i.months }.reverse
144-
rake "reporting:refresh_partitioned_table[reporting_patient_prescriptions, #{refresh_months.first}, #{refresh_months.last}]"
143+
rake "reporting:refresh_partitioned_table[reporting_patient_prescriptions, 3]"
145144
end
146145

147146
every :day, at: local("05:00 am"), roles: [:cron] do

lib/tasks/reporting.rake

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,26 +47,33 @@ namespace :reporting do
4747
end
4848
end
4949

50-
desc "Refresh a partitoned table for given months"
51-
task :refresh_partitioned_table, [:table_name, :start_date, :end_date] => :environment do |_, args|
52-
unless args[:table_name].present? && args[:start_date].present? && args[:end_date].present?
53-
puts "ERROR: table_name, start_date and end_date are required."
54-
puts "Usage: rake reporting:refresh_partitioned_table['reporting_patient_states', '2024-01-01' ,'2024-02-01']"
50+
desc "Refresh a partitioned table for N months in the past"
51+
task :refresh_partitioned_table, [:table_name, :months_back] => :environment do |_, args|
52+
unless args[:table_name].present?
53+
puts "ERROR: table_name is required."
54+
puts "Usage: rake reporting:refresh_partitioned_table[reporting_patient_states, 6]"
55+
puts " rake reporting:refresh_partitioned_table[reporting_patient_states]"
5556
exit 1
5657
end
57-
5858
table_name = args[:table_name]
59-
start_date = Date.parse(args[:start_date]).beginning_of_month
60-
end_date = Date.parse(args[:end_date]).beginning_of_month
61-
months = []
62-
current = start_date
63-
while current <= end_date
64-
months << current.strftime("%Y-%m-%d")
65-
current = current.next_month
59+
months_back = args[:months_back].present? ? args[:months_back].to_i : 12
60+
if months_back < 0
61+
puts "ERROR: months_back must be >= 0"
62+
exit 1
63+
end
64+
current_month = Date.today.beginning_of_month
65+
start_month = current_month - months_back.months
66+
refresh_months = []
67+
while start_month <= current_month
68+
refresh_months << start_month
69+
start_month = start_month.next_month
6670
end
67-
months.each do |month|
68-
puts "Refreshing partitioned table: #{table_name} for month: #{month}"
69-
ActiveRecord::Base.connection.exec_query("CALL simple_reporting.add_shard_to_table('#{month}', '#{table_name}')")
71+
refresh_months.each do |month|
72+
formatted_month = month.strftime("%Y-%m-%d")
73+
puts "Refreshing partitioned table: #{table_name} for month: #{formatted_month}"
74+
ActiveRecord::Base.connection.exec_query(
75+
"CALL simple_reporting.add_shard_to_table('#{formatted_month}', '#{table_name}')"
76+
)
7077
end
7178
end
7279
end

0 commit comments

Comments
 (0)