-
-
Notifications
You must be signed in to change notification settings - Fork 723
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
5. Trigger SubscriptionPlacementJob for each order cycle
A new OrderCycleOpeningJob will trigger the job as before, but provides a way to consistently trigger other events when order cycles open. Ruby ranges with Arel allow us to be nice and concise :)
- Loading branch information
Showing
5 changed files
with
62 additions
and
21 deletions.
There are no files selected for viewing
This file contains 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,18 @@ | ||
# frozen_string_literal: true | ||
|
||
# Trigger jobs for any order cycles that recently opened | ||
class OrderCycleOpeningJob < ApplicationJob | ||
def perform | ||
recently_opened_order_cycles.each do |oc_id| | ||
SubscriptionPlacementJob.perform_later(oc_id) | ||
end | ||
end | ||
|
||
private | ||
|
||
def recently_opened_order_cycles | ||
OrderCycle | ||
.where(orders_open_at: 1.hour.ago..Time.zone.now) | ||
.pluck(:id) | ||
end | ||
end |
This file contains 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 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 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,23 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
describe OrderCycleOpeningJob do | ||
let(:oc_opened_before) { | ||
create(:order_cycle, orders_open_at: Time.zone.now - 1.hour) | ||
} | ||
let(:oc_opened_now) { | ||
create(:order_cycle, orders_open_at: Time.zone.now) | ||
} | ||
let(:oc_opening_soon) { | ||
create(:order_cycle, orders_open_at: Time.zone.now + 1.minute) | ||
} | ||
|
||
it "triggers jobs for recently opened order cycles only" do | ||
expect(SubscriptionPlacementJob).to_not receive(:perform_later).with(oc_opened_before.id) | ||
expect(SubscriptionPlacementJob).to receive(:perform_later).with(oc_opened_now.id) | ||
expect(SubscriptionPlacementJob).to_not receive(:perform_later).with(oc_opening_soon.id) | ||
|
||
OrderCycleOpeningJob.perform_now | ||
end | ||
end |
This file contains 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