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

2700 improve product refresh scheduling #2701

Merged
merged 3 commits into from
Sep 18, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ Layout/EmptyLines:
- 'lib/open_food_network/order_cycle_permissions.rb'
- 'lib/open_food_network/products_cache.rb'
- 'lib/open_food_network/products_cache_integrity_checker.rb'
- 'lib/open_food_network/products_cache_refreshment.rb'
- 'lib/open_food_network/products_renderer.rb'
- 'lib/open_food_network/property_merge.rb'
- 'lib/open_food_network/reports/bulk_coop_report.rb'
Expand Down Expand Up @@ -560,7 +559,6 @@ Layout/MultilineOperationIndentation:
- 'app/models/variant_override_set.rb'
- 'lib/open_food_network/accounts_and_billing_settings_validator.rb'
- 'lib/open_food_network/order_cycle_permissions.rb'
- 'lib/open_food_network/products_cache_refreshment.rb'
- 'lib/open_food_network/sales_tax_report.rb'
- 'lib/open_food_network/users_and_enterprises_report.rb'

Expand Down Expand Up @@ -986,7 +984,6 @@ Lint/IneffectiveAccessModifier:
- 'app/models/variant_override.rb'
- 'lib/open_food_network/feature_toggle.rb'
- 'lib/open_food_network/products_cache.rb'
- 'lib/open_food_network/products_cache_refreshment.rb'
- 'lib/open_food_network/property_merge.rb'
- 'spec/lib/open_food_network/reports/report_spec.rb'

Expand Down Expand Up @@ -1096,7 +1093,6 @@ Lint/UselessAccessModifier:
- 'app/models/column_preference.rb'
- 'lib/open_food_network/feature_toggle.rb'
- 'lib/open_food_network/products_cache.rb'
- 'lib/open_food_network/products_cache_refreshment.rb'
- 'lib/open_food_network/property_merge.rb'
- 'lib/open_food_network/reports/bulk_coop_report.rb'
- 'spec/lib/open_food_network/reports/report_spec.rb'
Expand Down Expand Up @@ -1505,7 +1501,6 @@ Rails/TimeZone:
- 'lib/open_food_network/users_and_enterprises_report.rb'
- 'spec/controllers/api/statuses_controller_spec.rb'
- 'spec/jobs/heartbeat_job_spec.rb'
- 'spec/lib/open_food_network/products_cache_refreshment_spec.rb'
- 'spec/lib/open_food_network/products_cache_spec.rb'
- 'spec/models/enterprise_relationship_spec.rb'
- 'spec/models/variant_override_spec.rb'
Expand Down Expand Up @@ -1861,7 +1856,6 @@ Style/GuardClause:
- 'lib/open_food_network/accounts_and_billing_settings_validator.rb'
- 'lib/open_food_network/order_cycle_form_applicator.rb'
- 'lib/open_food_network/products_cache.rb'
- 'lib/open_food_network/products_cache_refreshment.rb'
- 'lib/open_food_network/products_renderer.rb'
- 'lib/open_food_network/rack_request_blocker.rb'
- 'lib/open_food_network/variant_and_line_item_naming.rb'
Expand Down
36 changes: 16 additions & 20 deletions lib/open_food_network/products_cache_refreshment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,27 @@
module OpenFoodNetwork
class ProductsCacheRefreshment
def self.refresh(distributor, order_cycle)
unless pending_job? distributor, order_cycle
enqueue_job distributor, order_cycle
end
job = refresh_job(distributor, order_cycle)
enqueue_job(job) unless pending_job?(job)
end

class << self
Copy link
Contributor

@luisramos0 luisramos0 Sep 13, 2018

Choose a reason for hiding this comment

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

what happened here?
I just spent half an hour reading about eigenclasses... my head hurts :-)
In this particular case, is this a convention (use class << self instead of self. ) or is there any change in logic with this?

Copy link
Contributor

@sauloperez sauloperez Sep 13, 2018

Choose a reason for hiding this comment

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

I'd like to check what the Ruby Style guide says about it and I'd stick to the community's default. Also, private doesn't work for class methods AFAIK, so I'm not sure here if these methods get hid.

You met the deepest part of Ruby, congrats @luisramos0 :trollface:

Copy link
Contributor

Choose a reason for hiding this comment

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

hmm.. I read a bit more about this and actually, this is exactly how you create private class methods :-)

Copy link
Member Author

Choose a reason for hiding this comment

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

I know! I was a bit reluctant to use class << self here, but I followed rubocop and found that it is the widely accepted way to declare private class methods. I should have tackled one rubocop violation at a time to explain it better. Sorry.

The alternative is to declare it separately:

private_class_method :refresh_job
private_class_method :pending_job?
private_class_method :enqueue_job

That is more repetitive, but you definitely know what it's doing. Should I change it?

Copy link
Contributor

Choose a reason for hiding this comment

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

👍
private_class_method is much easier to understand :-)
If it's also correct I'd use it.

Copy link
Member Author

Choose a reason for hiding this comment

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

Okay, please see my additional commit. If you approve, I will squash it with the previous commit.

Copy link
Contributor

Choose a reason for hiding this comment

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

yes, imo, much better.

private

private

def self.pending_job?(distributor, order_cycle)
# To inspect each job, we need to deserialize the payload.
# This is slow, and if it's a problem in practice, we could pre-filter in SQL
# for handlers matching the class name, distributor id and order cycle id.
def refresh_job(distributor, order_cycle)
RefreshProductsCacheJob.new(distributor.id, order_cycle.id)
end

Delayed::Job.
where(locked_at: nil).
map(&:payload_object).
select { |j|
j.class == RefreshProductsCacheJob &&
j.distributor_id == distributor.id &&
j.order_cycle_id == order_cycle.id
}.any?
Copy link
Contributor

@sauloperez sauloperez Sep 13, 2018

Choose a reason for hiding this comment

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

what is the exact difference? although very slow, weren't we checking the same? no ho entenc

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, the logic is the same. We check for an identical job.

BUT: The previous implementation needed to call payload_object on each job. That call fails if the object can't be deserialised properly. That happens when the payload object is a user and that user doesn't exist any more. In that case map(&:payload_object) raised Delayed::DeserializationError caused by ActiveRecord::RecordNotFound. Instead of rescueing from that error in a separate each block, I simplified the implementation to just check for the serialised content in the db column handler. That avoids the error and is more efficient.

end
def pending_job?(job)
Delayed::Job.
where(locked_at: nil).
where(handler: job.to_yaml).
exists?
end

def self.enqueue_job(distributor, order_cycle)
Delayed::Job.enqueue RefreshProductsCacheJob.new(distributor.id, order_cycle.id), priority: 10
def enqueue_job(job)
Delayed::Job.enqueue job, priority: 10
end
end
end
end
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'spec_helper'
require 'open_food_network/products_cache_refreshment'

module OpenFoodNetwork
Expand Down