-
Notifications
You must be signed in to change notification settings - Fork 166
Extract a synchronous proofing job #1524
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 @@ | ||
| class VendorValidatorJob < ActiveJob::Base | ||
| queue_as :idv | ||
|
|
||
| def perform(result_id:, vendor_validator_class:, vendor:, vendor_params:, applicant_json:, | ||
| vendor_session_id:) | ||
| vendor_validator = vendor_validator_class.constantize.new( | ||
| applicant: Proofer::Applicant.new(JSON.parse(applicant_json, symbolize_names: true)), | ||
| vendor: vendor, | ||
| vendor_params: indifferent_access(vendor_params), | ||
| vendor_session_id: vendor_session_id | ||
| ) | ||
|
|
||
| VendorValidatorResultStorage.new.store( | ||
| result_id: result_id, | ||
| result: extract_result(vendor_validator.result) | ||
| ) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def extract_result(result) | ||
| vendor_resp = result.vendor_resp | ||
|
|
||
| Idv::VendorResult.new( | ||
| success: result.success?, | ||
| errors: result.errors, | ||
| reasons: vendor_resp.reasons, | ||
| normalized_applicant: vendor_resp.try(:normalized_applicant), | ||
| session_id: result.try(:session_id) | ||
| ) | ||
| end | ||
|
|
||
| def indifferent_access(params) | ||
| return params if params.is_a?(String) | ||
| params.with_indifferent_access | ||
| end | ||
| end |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,32 @@ | ||
| class SubmitIdvJob | ||
| def initialize(vendor_validator_class:, idv_session:, vendor_params:) | ||
| @vendor_validator_class = vendor_validator_class | ||
| @idv_session = idv_session | ||
| @vendor_params = vendor_params | ||
| end | ||
|
|
||
| def call | ||
| idv_session.async_result_id = result_id | ||
|
|
||
| VendorValidatorJob.perform_now( | ||
| result_id: result_id, | ||
| vendor_validator_class: vendor_validator_class.to_s, | ||
| vendor: vendor, | ||
| vendor_params: vendor_params, | ||
| vendor_session_id: idv_session.vendor_session_id, | ||
| applicant_json: idv_session.applicant.to_json | ||
| ) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| attr_reader :vendor_validator_class, :idv_session, :vendor_params | ||
|
|
||
| def result_id | ||
| @_result_id ||= SecureRandom.uuid | ||
| end | ||
|
|
||
| def vendor | ||
| idv_session.vendor || Idv::Vendor.new.pick | ||
| end | ||
| end |
This file contains hidden or 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,24 @@ | ||
| class VendorValidatorResultStorage | ||
| TTL = Figaro.env.session_timeout_in_minutes.to_i.minutes.seconds.to_i | ||
|
|
||
| def store(result_id:, result:) | ||
| Sidekiq.redis do |redis| | ||
| redis.setex(redis_key(result_id), TTL, result.to_json) | ||
| end | ||
| end | ||
|
|
||
| def load(result_id) | ||
| result_json = Sidekiq.redis do |redis| | ||
| redis.get(redis_key(result_id)) | ||
| end | ||
|
|
||
| return unless result_json | ||
|
|
||
| Idv::VendorResult.new_from_json(result_json) | ||
| end | ||
|
|
||
| # @api private | ||
| def redis_key(result_id) | ||
| "vendor-validator-result-#{result_id}" | ||
| end | ||
| end | ||
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -3,4 +3,5 @@ | |
| - voice | ||
| - mailers | ||
| - analytics | ||
| - idv | ||
| :logfile: 'log/sidekiq.log' | ||
This file contains hidden or 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,44 @@ | ||
| require 'rails_helper' | ||
|
|
||
| RSpec.describe VendorValidatorJob do | ||
| let(:result_id) { SecureRandom.uuid } | ||
| let(:vendor_validator_class) { 'Idv::PhoneValidator' } | ||
| let(:vendor) { :mock } | ||
| let(:vendor_params) { '+1 (888) 123-4567' } | ||
| let(:applicant) { Proofer::Applicant.new(first_name: 'Test') } | ||
| let(:applicant_json) { applicant.to_json } | ||
| let(:vendor_session_id) { SecureRandom.uuid } | ||
|
|
||
| subject(:job) { VendorValidatorJob.new } | ||
|
|
||
| describe '#perform' do | ||
| subject(:perform) do | ||
| job.perform( | ||
| result_id: result_id, | ||
| vendor_validator_class: vendor_validator_class, | ||
| vendor: vendor, | ||
| vendor_params: vendor_params, | ||
| applicant_json: applicant_json, | ||
| vendor_session_id: vendor_session_id | ||
| ) | ||
| end | ||
|
|
||
| it 'calls out to a vendor and serializes the result' do | ||
| expect(Idv::PhoneValidator).to receive(:new). | ||
| with( | ||
| applicant: kind_of(Proofer::Applicant), | ||
| vendor: vendor, | ||
| vendor_params: vendor_params, | ||
| vendor_session_id: vendor_session_id | ||
| ).and_call_original | ||
|
|
||
| before_result = VendorValidatorResultStorage.new.load(result_id) | ||
| expect(before_result).to be_nil | ||
|
|
||
| perform | ||
|
|
||
| after_result = VendorValidatorResultStorage.new.load(result_id) | ||
| expect(after_result).to be_a(Idv::VendorResult) | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this TTL get updated or only set once when first called? I'm thinking about the scenario where the user starts proofing at 12:00, this TTL is then set to expire at 12:08, but the user takes longer than that to finish proofing. For example, after they submit the first profile page, let's say the vendor takes a minute to respond. Then, the user stays idle for 7 minutes on the financial info page because they had to go find their credit card. It's now 12:08, but the session is still active because their last request was only 7 minutes ago. So then they enter their credit card, but now the Redis TTL has expired. Will this cause any issues?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the TTL for the result value of one job.
So for the 3 steps, they will have 3 separate result objects that will have 3 separate expirations.
When this is asynchronous, the result is created after hitting submit and hearing back from the vendor. So as long as the user's browser takes less than this time to refresh the page (which it definitely will, I think we'll set refresh interval to something like 10-15 seconds) then the controller will pick up the result from redis and use it for what it needs to be used and move on to the next step.