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 1 commit
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
32 changes: 17 additions & 15 deletions lib/open_food_network/products_cache_refreshment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,23 @@ def self.refresh(distributor, order_cycle)
enqueue_job(job) unless pending_job?(job)
end

private

def self.refresh_job(distributor, order_cycle)
RefreshProductsCacheJob.new(distributor.id, order_cycle.id)
end

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

def self.enqueue_job(job)
Delayed::Job.enqueue job, priority: 10
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

def refresh_job(distributor, order_cycle)
RefreshProductsCacheJob.new(distributor.id, order_cycle.id)
end

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

def enqueue_job(job)
Delayed::Job.enqueue job, priority: 10
end
end
end
end