-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into fb-add-better-admin-interface-15446845
- Loading branch information
Showing
19 changed files
with
408 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
class ReschedulingJob < ApplicationJob | ||
queue_as :default | ||
|
||
def perform | ||
ProtocolSubscription.active.find_each do |protocol_subscription| | ||
RescheduleResponses.run!(protocol_subscription: protocol_subscription) | ||
end | ||
end | ||
|
||
def max_attempts | ||
2 | ||
end | ||
|
||
def reschedule_at(current_time, _attempts) | ||
current_time + 5.minutes | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# frozen_string_literal: true | ||
|
||
class RescheduleResponses < ActiveInteraction::Base | ||
object :protocol_subscription | ||
time :future, default: TimeTools.increase_by_duration(Time.zone.now, 3.hours) | ||
|
||
# Reschedules future responses for a protocol subscription | ||
# | ||
# Params: | ||
# - protocol_subscription: the current protocol subscription | ||
def execute | ||
ActiveRecord::Base.transaction do | ||
protocol_subscription.responses.after_date(future).destroy_all | ||
schedule_responses | ||
end | ||
end | ||
|
||
private | ||
|
||
def schedule_responses | ||
protocol_subscription.protocol.measurements.each do |measurement| | ||
schedule_responses_for_measurement(measurement) | ||
end | ||
end | ||
|
||
def schedule_responses_for_measurement(measurement) | ||
measurement.response_times(protocol_subscription.start_date, protocol_subscription.end_date).each do |time| | ||
next if time <= future | ||
Response.create!(protocol_subscription_id: protocol_subscription.id, | ||
measurement_id: measurement.id, | ||
open_from: time) | ||
end | ||
end | ||
end |
5 changes: 5 additions & 0 deletions
5
db/migrate/20180613123125_allow_open_from_offset_to_be_nil.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AllowOpenFromOffsetToBeNil < ActiveRecord::Migration[5.0] | ||
def change | ||
change_column_null :measurements, :open_from_offset, true | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
describe ReschedulingJob do | ||
describe '#perform_later' do | ||
it 'performs something later' do | ||
ActiveJob::Base.queue_adapter = :test | ||
expect do | ||
described_class.perform_later | ||
end.to have_enqueued_job(described_class) | ||
end | ||
end | ||
|
||
describe 'perform' do | ||
it 'should call the reschedule responses use case with the correct parameters' do | ||
FactoryBot.create(:protocol_subscription, :canceled) | ||
protsub = FactoryBot.create(:protocol_subscription) | ||
expect(RescheduleResponses).to receive(:run!).with(protocol_subscription: protsub) | ||
described_class.perform_now | ||
end | ||
end | ||
|
||
describe 'max_attempts' do | ||
it 'should be two' do | ||
expect(subject.max_attempts).to eq 2 | ||
end | ||
end | ||
|
||
describe 'reschedule_at' do | ||
it 'should be in one hour' do | ||
time_now = Time.zone.now | ||
expect(subject.reschedule_at(time_now, 1)).to be_within(1.minute) | ||
.of(TimeTools.increase_by_duration(time_now, 5.minutes)) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.