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

Run background reports with Sidekiq, not fork #10615

Merged

Conversation

mkllnk
Copy link
Member

@mkllnk mkllnk commented Mar 27, 2023

What? Why?

Forking worked in theory but crashed the browser in system specs. It also came with many other hurdles and isn't a well known solution in the Rails community. Sidekiq can give us better control over execution limits as well.

This is just a first iteration to test if it works in principle. If this is successful, we should continue with:

What should we test?

Before deploy:

  • Visit /admin/reports/sales_tax
  • Select a date range and a distributor to show some data.

After deploy:

  • Reload the report.
  • It should display as before.
  • Activate background reports and reload the report.
  • It should display as before.
  • If you see a server error (out of memory) then restart the app first:
    cd ~/apps/openfoodnetwork/current
    sudo systemctl restart puma.service
    

Release notes

Changelog Category: Technical changes

The title of the pull request will be included in the release notes.

Dependencies

Documentation updates

Forking worked in theory but crashed the browser in system specs. It
also came with many other hurdles and isn't well known solution in the
Rails community. Sidekiq can give us better control over execution
limits as well.
@mkllnk mkllnk self-assigned this Mar 27, 2023
@mkllnk mkllnk marked this pull request as ready for review March 27, 2023 05:24
Copy link
Contributor

@jibees jibees left a comment

Choose a reason for hiding this comment

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

Looks good to me, I only have one comment!

report_class, spree_current_user, params, format
)
sleep 1 until job.done?
Copy link
Contributor

Choose a reason for hiding this comment

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

Just wondering why this is needed?

Copy link
Member Author

Choose a reason for hiding this comment

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

Once the job is done, we want to render it here in the same web request. So we are waiting for the result. That's like the current behaviour. We display the report (or download) when it's ready.

It's planned to send a download link for longer running reports. Then we don't need to wait and can tell the user straight away that the report is processing. But we don't have that yet.

Copy link
Collaborator

@rioug rioug left a comment

Choose a reason for hiding this comment

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

Looks good 👍

Copy link
Member

@dacook dacook left a comment

Choose a reason for hiding this comment

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

Awesome, just a simple change to try this out!

Another consideration, which we discussed earlier, is that we might need to adjust the sidekiq workers/queues/priorities to help improve the responsiveness of the reports. But that would be after proving the concept.

Comment on lines +34 to +48
describe "Background processing" do
before do
Flipper.enable(:background_reports)
ActiveJob::Base.queue_adapter.perform_enqueued_jobs = true
end

it "can run the customers report" do
login_as_admin_and_visit admin_report_path(
report_type: :customers, report_subtype: :mailing_list
)
click_button "Go"
expect(page).to have_content "EMAIL FIRST NAME"
end
end

Copy link
Member

Choose a reason for hiding this comment

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

Because we're passing report parameters and chosen format to the job, it might be worth testing that these options have been used.
But I wouldn't suggest that prevents us from merging this, this is just a thought maybe for the next iteration.

@dacook
Copy link
Member

dacook commented Mar 28, 2023

Deployed to confirm ready for testing:

✅ au-staging

@filipefurtad0 filipefurtad0 self-assigned this Mar 29, 2023
@filipefurtad0 filipefurtad0 added the pr-staged-uk staging.openfoodnetwork.org.uk label Mar 29, 2023
@filipefurtad0
Copy link
Contributor

Hey @mkllnk ,

I've ran the report as you've indicated before and after staging, and got the same data - all good. Also, after toggling the background_reports feature, I found all the reports to render correctly (I've added a note on the only exception):

Orders And Distributors
Bulk Co-Op (all subtypes)
Payment Reports (all subtypes) -> Payments By Type -> got a consistent 504 Gateway Timeout; I don't think this is related to this PR, as the report rendered fine for other hubs. Looks like something related to a payment method, I have a question on this regard (*)
Orders and Fulfillment (all subtypes)
Customers (all subtypes)
Products & Inventory (all subtypes)
Enterprise Fee Summary (all subtypes)
Order Cycle Management (all subtypes)
Sales Tax(all subtypes)
Xero (all subtypes)
Packing Reports (all subtypes)

This looks good to go. Before merging, I'll still stage another PR, just to be sure about the failing report. Moving to ready to go! 🎉

(*) Additional Note - although not related to this PR
When I first got the 504 I thought it could be related to memory usage, so I logged into the server and restarted puma, following your instructions, and checked memory usage before and after restarting, with htop; I was expecting it to change, but it did not:

image

Any idea why?

@filipefurtad0
Copy link
Contributor

filipefurtad0 commented Mar 29, 2023

Merging: the Payment Report by Type still blows up for that user/date range, after staging another PR and disabling background_reports. It seems there is a payment method without name. This might be due corrupt test data, since I've found no occurrences in production (in the last 30 days),

@filipefurtad0 filipefurtad0 removed the pr-staged-uk staging.openfoodnetwork.org.uk label Mar 29, 2023
@filipefurtad0 filipefurtad0 merged commit 90ce526 into openfoodfoundation:master Mar 29, 2023
@mkllnk mkllnk deleted the background-report-job branch March 31, 2023 03:42
@mkllnk
Copy link
Member Author

mkllnk commented Mar 31, 2023

Nice testing, thank you.

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.

5 participants