From 6a70c162bb900eb2aca6fe3664ea506cc8365fc8 Mon Sep 17 00:00:00 2001 From: Rob Harrington Date: Fri, 23 Oct 2015 12:33:05 +1100 Subject: [PATCH] Account invoice jobs run according to rails config time zone rather than system time zone --- app/jobs/finalize_account_invoices.rb | 10 +++++----- app/jobs/update_account_invoices.rb | 12 ++++++------ app/jobs/update_billable_periods.rb | 14 +++++++------- spec/jobs/finalize_account_invoices_spec.rb | 11 ++++++----- spec/jobs/update_account_invoices_spec.rb | 8 +++++--- spec/jobs/update_billable_periods_spec.rb | 12 ++++++------ 6 files changed, 35 insertions(+), 32 deletions(-) diff --git a/app/jobs/finalize_account_invoices.rb b/app/jobs/finalize_account_invoices.rb index fcf872e1c3..4baa841db6 100644 --- a/app/jobs/finalize_account_invoices.rb +++ b/app/jobs/finalize_account_invoices.rb @@ -2,11 +2,11 @@ class FinalizeAccountInvoices attr_reader :year, :month, :start_date, :end_date def initialize(year = nil, month = nil) - ref_point = Time.now - 1.month + ref_point = Time.zone.now - 1.month @year = year || ref_point.year @month = month || ref_point.month - @start_date = Time.new(@year, @month) - @end_date = Time.new(@year, @month) + 1.month + @start_date = Time.zone.local(@year, @month) + @end_date = Time.zone.local(@year, @month) + 1.month end def before(job) @@ -46,13 +46,13 @@ class FinalizeAccountInvoices private def settings_are_valid? - unless end_date <= Time.now + unless end_date <= Time.zone.now Bugsnag.notify(RuntimeError.new("InvalidJobSettings"), { job: "FinalizeAccountInvoices", error: "end_date is in the future", data: { end_date: end_date.localtime.strftime("%F %T"), - now: Time.now.strftime("%F %T") + now: Time.zone.now.strftime("%F %T") } }) return false diff --git a/app/jobs/update_account_invoices.rb b/app/jobs/update_account_invoices.rb index 6f43a10e31..c0597a9ba3 100644 --- a/app/jobs/update_account_invoices.rb +++ b/app/jobs/update_account_invoices.rb @@ -2,12 +2,12 @@ class UpdateAccountInvoices attr_reader :year, :month, :start_date, :end_date def initialize(year = nil, month = nil) - ref_point = Time.now - 1.day + ref_point = Time.zone.now - 1.day @year = year || ref_point.year @month = month || ref_point.month - @start_date = Time.new(@year, @month) - @end_date = Time.new(@year, @month) + 1.month - @end_date = Time.now.beginning_of_day if start_date == Time.now.beginning_of_month + @start_date = Time.zone.local(@year, @month) + @end_date = Time.zone.local(@year, @month) + 1.month + @end_date = Time.zone.now.beginning_of_day if start_date == Time.zone.now.beginning_of_month end def before(job) @@ -76,13 +76,13 @@ class UpdateAccountInvoices private def settings_are_valid? - unless end_date <= Time.now + unless end_date <= Time.zone.now Bugsnag.notify(RuntimeError.new("InvalidJobSettings"), { job: "UpdateAccountInvoices", error: "end_date is in the future", data: { end_date: end_date.localtime.strftime("%F %T"), - now: Time.now.strftime("%F %T") + now: Time.zone.now.strftime("%F %T") } }) return false diff --git a/app/jobs/update_billable_periods.rb b/app/jobs/update_billable_periods.rb index 35daf48dbc..598d5f8e7b 100644 --- a/app/jobs/update_billable_periods.rb +++ b/app/jobs/update_billable_periods.rb @@ -2,18 +2,18 @@ class UpdateBillablePeriods attr_reader :year, :month, :start_date, :end_date def initialize(year = nil, month = nil) - ref_point = Time.now - 1.day + ref_point = Time.zone.now - 1.day @year = year || ref_point.year @month = month || ref_point.month - @start_date = Time.new(@year, @month) - @end_date = Time.new(@year, @month) + 1.month - @end_date = Time.now.beginning_of_day if start_date == Time.now.beginning_of_month + @start_date = Time.zone.local(@year, @month) + @end_date = Time.zone.local(@year, @month) + 1.month + @end_date = Time.zone.now.beginning_of_day if start_date == Time.zone.now.beginning_of_month end def perform return unless settings_are_valid? - job_start_time = Time.now + job_start_time = Time.zone.now enterprises = Enterprise.where('created_at < (?)', end_date).select([:id, :name, :owner_id, :sells, :shop_trial_start_date, :created_at]) @@ -111,13 +111,13 @@ class UpdateBillablePeriods private def settings_are_valid? - unless end_date <= Time.now + unless end_date <= Time.zone.now Bugsnag.notify(RuntimeError.new("InvalidJobSettings"), { job: "UpdateBillablePeriods", error: "end_date is in the future", data: { end_date: end_date.localtime.strftime("%F %T"), - now: Time.now.strftime("%F %T") + now: Time.zone.now.strftime("%F %T") } }) return false diff --git a/spec/jobs/finalize_account_invoices_spec.rb b/spec/jobs/finalize_account_invoices_spec.rb index 8ba3c66f24..eedc91592d 100644 --- a/spec/jobs/finalize_account_invoices_spec.rb +++ b/spec/jobs/finalize_account_invoices_spec.rb @@ -6,10 +6,11 @@ end describe FinalizeAccountInvoices do + let!(:year) { Time.zone.now.year } + describe "unit specs" do let!(:finalizer) { FinalizeAccountInvoices.new } - let!(:start_of_july) { Time.now.beginning_of_year + 6.months } - let!(:year) { Time.now.year } + let!(:start_of_july) { Time.local(year, 7) } describe "perform" do let!(:accounts_distributor) { create(:distributor_enterprise) } @@ -87,8 +88,8 @@ describe FinalizeAccountInvoices do end end - context "an a specific year and month are passed as arguments" do - let!(:finalizer) { FinalizeAccountInvoices.new(Time.now.year, 7) } + context "and a specific year and month are passed as arguments" do + let!(:finalizer) { FinalizeAccountInvoices.new(year, 7) } before do allow(finalizer).to receive(:finalizer) @@ -163,7 +164,7 @@ describe FinalizeAccountInvoices do end describe "validation spec" do - let!(:start_of_july) { Time.now.beginning_of_year + 6.months } + let!(:start_of_july) { Time.local(year, 7) } let!(:updater) { UpdateAccountInvoices.new } let!(:finalizer) { FinalizeAccountInvoices.new } diff --git a/spec/jobs/update_account_invoices_spec.rb b/spec/jobs/update_account_invoices_spec.rb index fd84a22e1e..8e1ca6a3cc 100644 --- a/spec/jobs/update_account_invoices_spec.rb +++ b/spec/jobs/update_account_invoices_spec.rb @@ -5,8 +5,10 @@ def travel_to(time) end describe UpdateAccountInvoices do + let(:year) { Time.zone.now.year } + describe "units specs" do - let!(:start_of_july) { Time.now.beginning_of_year + 6.months } + let!(:start_of_july) { Time.local(year, 7) } let!(:updater) { UpdateAccountInvoices.new } @@ -65,7 +67,7 @@ describe UpdateAccountInvoices do end context "when specfic a specific month (and year) are passed as arguments" do - let!(:updater) { UpdateAccountInvoices.new(Time.now.year, 7) } + let!(:updater) { UpdateAccountInvoices.new(year, 7) } before do allow(updater).to receive(:update) @@ -319,7 +321,7 @@ describe UpdateAccountInvoices do end describe "validation spec" do - let!(:start_of_july) { Time.now.beginning_of_year + 6.months } + let!(:start_of_july) { Time.local(year, 7) } let!(:updater) { UpdateAccountInvoices.new } diff --git a/spec/jobs/update_billable_periods_spec.rb b/spec/jobs/update_billable_periods_spec.rb index ef6d2b12ad..640519870f 100644 --- a/spec/jobs/update_billable_periods_spec.rb +++ b/spec/jobs/update_billable_periods_spec.rb @@ -5,9 +5,10 @@ def travel_to(time) end describe UpdateBillablePeriods do + let!(:year) { Time.zone.now.year } + describe "unit specs" do - let!(:start_of_july) { Time.now.beginning_of_year + 6.months } - let!(:year) { Time.now.year } + let!(:start_of_july) { Time.local(year, 7) } let!(:updater) { UpdateBillablePeriods.new } @@ -508,7 +509,7 @@ describe UpdateBillablePeriods do end context "cleaning up untouched billable periods" do - let(:job_start_time) { Time.now } + let(:job_start_time) { Time.zone.now } let(:enterprise) { create(:enterprise) } # Updated after start let!(:bp1) { create(:billable_period, enterprise: enterprise, updated_at: job_start_time + 2.seconds, begins_at: start_of_july, ends_at: start_of_july + 5.days ) } @@ -549,9 +550,8 @@ describe UpdateBillablePeriods do describe "validation spec" do # Chose july to test with because June has 30 days and so is easy to calculate end date for shop trial - let!(:start_of_july) { Time.now.beginning_of_year + 6.months } - - let!(:year) { Time.now.year } + let!(:year) { Time.zone.now.year } + let!(:start_of_july) { Time.local(year, 7) } let!(:enterprise) { create(:supplier_enterprise, sells: 'any') }