Skip to content
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

Report subscription failures #7207

Merged

Conversation

andrewpbrett
Copy link
Contributor

@andrewpbrett andrewpbrett commented Mar 24, 2021

What? Why?

Addresses #7063

See #7063 (comment)

What should we test?

We're not sure how this is actually happening in production... one way to get to this branch of the code, though, is by trying to place an order for a subscription that contains a variant with a nil price should report the failure in the summary email.

I think this might be dev-test or no-test, actually.

Release notes

Fixed a bug where some subscriptions that failed to create orders would not appear in the summary email.

Changelog Category: User facing changes

Dependencies

Documentation updates

it "records an error " do
expect(job).to receive(:record_subscription_issue)
expect(job).to_not receive(:place_order)
job.send(:place_proxy_order, proxy_order)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

weird, I tried to execute this test and I get "NoMethodError: undefined method `place_proxy_order' for #SubscriptionPlacementJob:0x000055d45d802fa0"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

besides, something is wrong if need to unit test a private method. IMO this is a clear symptom that the existing design doesn't allow for proper testing and should be improved.

@sauloperez
Copy link
Contributor

sauloperez commented Mar 29, 2021

I offer myself ☝️ if you think I can be of any help getting this sorted out.

@andrewpbrett
Copy link
Contributor Author

Thanks for the review @sauloperez. I made a couple changes that are marginal improvements, but agree that there's quite a bit that could still be improved about subscriptions. I didn't change the spec that tests the private method, however; we test other private methods in the same spec file, was my thinking with that, and to call perform would make the spec far too complex.

What do you think about these changes? Would you want to proceed with the PR, or wait until we can do some more refactoring on subscriptions? I don't know where that would fit in a priority list.

@sauloperez
Copy link
Contributor

sauloperez commented Mar 30, 2021

IMO given we've introduced a new feature (yes, rather small but still a new feature) it's on us to adequate the underlying OOP design to better fit this new requirement so we keep things changeable. It's part of the job and the more we neglect it that it'll get overtime. In any case, this is not fixing the specific user error on #7063 but improves the experience if this were to happen again, right? so we're not rushing.

So, there are various code smells that lead me to think that we need to extract a class and move there the private methods that we're testing. A simple case of Extract Class. As a result, tests will probably require less setup and become easier to read, which is always good. The code smells that come to mind are:

  • This job class has many responsibilities: on one hand what is expected from an ActiveJob class, being the entry point for a background worker, and on the other, all the business logic related to placing subs. orders. IMO it's very similar to a controller. We'd likely extract private methods to a service so HTTP-related logic is separate from the business logic. Here is more or less the same: job logic vs business logic.
  • Related to this, 90% of the times we change this file (totally biased personal perception 😂 ), it's because the logic to treat a proxy order needed to change but not the data-fetching or anything related to the job class. So, separating the things that change more frequently from the things that don't would result in smaller and less risky changes.
  • Most of the private methods take either a proxy order or an order as an argument. Therefore, it feels a lot like Feature Envy.
  • The tests are considerably complex and cumbersome thus, the class is way too coupled with too many things.

For the record, I used reek and churn to assess the situation.

@@ -56,6 +56,15 @@ module Subscriptions
end
end

describe "record_subscription_issue" do
let(:subscription) { double(:subscription, id: 101) }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should rely on an RSpec's verifying double such as https://relishapp.com/rspec/rspec-mocks/docs/verifying-doubles/using-an-instance-double to avoid the double drifting away from Susbcription's interface.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth it if the only method being used is #id?

@@ -81,6 +90,21 @@ module Subscriptions
summary.instance_variable_set(:@success_ids, success_ids)
expect(summary.issue_count).to be 2 # 7 & 9
end

context "when there are also subscription issues" do
let(:subscription) { double(:subscription, id: 101) }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here as well


context "when there are also subscription issues" do
let(:subscription) { double(:subscription, id: 101) }
let(:order) { double(:order, id: 1) }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and the same happens with the order. We must ensure we don't diverge from Spree::Order's public API.

@sauloperez
Copy link
Contributor

sauloperez commented Mar 30, 2021

Sorry for the wall of text, but I wanted to stress the need to improve subs. I'm sorry you're paying the price 😅

I have an attempt halfway through at #7281. What do you think?

@sauloperez
Copy link
Contributor

I think we can move forward with this while we finish off #7281. Then that'll be just a refactoring which will ease reviews.

@codecov
Copy link

codecov bot commented Apr 4, 2021

Codecov Report

❗ No coverage uploaded for pull request base (master@4bddc59). Click here to learn what that means.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff            @@
##             master    #7207   +/-   ##
=========================================
  Coverage          ?   89.69%           
=========================================
  Files             ?      648           
  Lines             ?    18857           
  Branches          ?        0           
=========================================
  Hits              ?    16913           
  Misses            ?     1944           
  Partials          ?        0           

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4bddc59...c30ced2. Read the comment docs.

@filipefurtad0 filipefurtad0 self-assigned this Apr 9, 2021
@filipefurtad0 filipefurtad0 added pr-staged-uk staging.openfoodnetwork.org.uk and removed pr-staged-uk staging.openfoodnetwork.org.uk labels Apr 9, 2021
@filipefurtad0 filipefurtad0 added pr-staged-uk staging.openfoodnetwork.org.uk and removed pr-staged-uk staging.openfoodnetwork.org.uk labels Apr 9, 2021
@filipefurtad0
Copy link
Contributor

filipefurtad0 commented Apr 9, 2021

Hey @andrewpbrett ,

Not sure what to test here.

I looked at the original issue, which seems to relate to situations where:
i) an item is out of stock
ii) possibly related with #6680 (now closed)

Looking at your code:

  • you seem to test for the recording of issues - :record_subscription_issue
  • after this PR :subscription_issues will be included into the body of the sent emails (was this not the case, before? - sorry, my rails is a bit rusty 😅 )

I tried to reproduce the bug with the hints from the issue, but wasn't really successful.

Did a brief sanity check, around:

  • subs which should go through - all good
  • some stock, but not all to cover the originally placed order -> order is created with item's amount limited by stock - OK
  • subs which have no stock at all, for the order - order is not created -> there's no stock - OK (pic below)

image

While I think these are valid tests, it probably would be interesting to test the changes this PR introduces on the email. We'd need to reproduce the bug for this. What do you think?

Other than this, I guess we can merge.

@filipefurtad0 filipefurtad0 removed the pr-staged-uk staging.openfoodnetwork.org.uk label Apr 9, 2021
@filipefurtad0
Copy link
Contributor

filipefurtad0 commented Apr 9, 2021

What about:

  • manually changing Price to nil in the DB, for a variant on the subscription
  • running the placement/confirmation jobs for that sub

Maybe worth a try...

@filipefurtad0 filipefurtad0 added the pr-staged-uk staging.openfoodnetwork.org.uk label Apr 9, 2021
@andrewpbrett
Copy link
Contributor Author

@filipefurtad0 - yes, if you manually set the price to nil on a variant in a subscription, you should see an example of the changes in the emails that go out. That was what I did as a dev-test; I think what you already did (making sure there weren't any big changes to existing behavior) was a good general test and we could probably go ahead and merge with just that. But if you did want to see it in action, manually setting a price to nil should do it.

@filipefurtad0
Copy link
Contributor

filipefurtad0 commented Apr 9, 2021

That was what I did as a dev-test;

Ok, I was not aware you've tested this scenario before, I just gave it a try on staging-UK.

I just made these changes on the relevant variant:

image

Before staging this PR - Running the placement job command:
bundle exec rake ofn:subs:test:repeat_placement_job
triggers:
https://app.bugsnag.com/yaycode/openfoodnetwork-uk/errors/607070a04fe96f00078f837b?event_id=607070a0007713303b760000&i=sk&m=nw

After staging this PR - Running the placement job command:
bundle exec rake ofn:subs:test:repeat_placement_job
triggers:

https://app.bugsnag.com/yaycode/openfoodnetwork-uk/errors/60422eda3c3c6600182214fd?event_id=607070a100773cb863810000&i=sk&m=nw

this seems to point to the code introduced, on engines/order_management/app/services/order_management/subscriptions/summarizer.rb:26

The order confirmation email does not arrive, in this case. Any thoughts on this?

@filipefurtad0 filipefurtad0 removed the pr-staged-uk staging.openfoodnetwork.org.uk label Apr 9, 2021
@andrewpbrett
Copy link
Contributor Author

Something strange is going on here. When I look at the trace for the bugsnag triggered after this PR is staged, I see this code:

def place_order_for(proxy_order)
  JobLogger.logger.info("Placing Order for Proxy Order #{proxy_order.id}")
  initialise_order(proxy_order)
  place_order(proxy_order.order)
end

But this PR has those lines as:

def place_order_for(proxy_order)
  JobLogger.logger.info("Placing Order for Proxy Order #{proxy_order.id}")
  initialise_order(proxy_order)
  return unless proxy_order.order.present?

  proxy_order.update_column(:placed_at, Time.zone.now)
  place_order(proxy_order.order)
end

I double checked the notifications channel and I don't see this PR as being deployed to the UK - maybe there was an error deploying it?

@filipefurtad0 filipefurtad0 added the pr-staged-uk staging.openfoodnetwork.org.uk label Apr 9, 2021
@filipefurtad0
Copy link
Contributor

filipefurtad0 commented Apr 9, 2021

Thanks @andrewpbrett ,
It seems the PR really wasn't deployed - my bad, sorry.

I've tried it again - now the deploy appears on the logs - and repeated the procedure.

Changing the price to nil and running the subscription jobs now triggers this bugsnag:
https://app.bugsnag.com/yaycode/openfoodnetwork-uk/errors/60709c1622b3e30007d0c099?event_id=60709c16007709f7fc120000&i=sk&m=nw

This error is new, I can't find it on Slack nor on bugsnag (the code is also new, so that's ok I think). It seems to break subs as well (when compared to the case before staging) - no emails arrive in this case.

We should perhaps consider that this does not seem reproduce what was reported on issue #7063 (NaN), namely on the UI. The price for that item appears in /subscriptions, although it is set to zero.

So maybe it is price_estimate from table subscription_line_items which needs to be set to NULL to reproduce the issue?

 id  | subscription_id | variant_id | quantity |         created_at         |         updated_at         | price_estimate 
-----+-----------------+------------+----------+----------------------------+----------------------------+----------------
 285 |             167 |     235225 |        5 | 2021-04-09 18:37:28.171287 | 2021-04-09 18:37:28.171287 |          12.00

Ok, I've tried this as well, with:
UPDATE subscription_line_items SET price_estimate=NULL WHERE id='285';

and now it does look like #7063 🎉

image

image

What's interesting is that, despite seeing the NaN on the screen, the subscription runs as normal, at least in testing, in which one places and confirms the jobs manually... all 6 emails arrive, everything seemingly normal, despite the NaN and having this in the DB:

 id  | subscription_id | variant_id | quantity |         created_at         |         updated_at         | price_estimate 
-----+-----------------+------------+----------+----------------------------+----------------------------+----------------
 285 |             167 |     235225 |        5 | 2021-04-09 18:37:28.171287 | 2021-04-09 18:37:28.171287 |               

So - long story short - this PR seems to introduce no regression. It's difficult to check the change in the email, but I guess we are good to merge? I'll probably have yet another look during release testing.

@filipefurtad0 filipefurtad0 removed the pr-staged-uk staging.openfoodnetwork.org.uk label Apr 9, 2021
@andrewpbrett
Copy link
Contributor Author

Thanks again @filipefurtad0. I'm going to merge since we also have some work coming that builds on it in #7281 that should improve things further.

@andrewpbrett andrewpbrett merged commit d00970e into openfoodfoundation:master Apr 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants