Skip to content

Commit

Permalink
move newrelic gem to all groups to allow testing; basic specs for not…
Browse files Browse the repository at this point in the history
…icing stale orders
  • Loading branch information
armandofox committed Jun 13, 2022
1 parent 0545862 commit fdde3d6
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ gem 'i18n'
gem 'jbuilder', '~> 2.0'
gem 'jquery-rails', '= 4.0.5'
gem 'jquery-ui-rails', '= 5.0.5'
gem 'newrelic_rpm'
gem 'nokogiri'
gem 'protected_attributes' # remove once we migrate to Strong Parameters
gem 'responders', '~> 2.0'
Expand All @@ -34,7 +35,6 @@ gem 'uglifier'

group :production do
gem 'rack-timeout' # prevent Heroku dynos from hanging up on timeout
gem 'newrelic_rpm'
gem 'puma-heroku'
gem 'puma', '>= 4.3.8'
gem 'rails_12factor'
Expand Down
16 changes: 8 additions & 8 deletions Guardfile
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ group 'cucumber' do
# notification: false
}

guard "cucumber", cucumber_options do
watch(%r{^features/.+\.feature$})
watch(%r{^features/support/.+$}) { "features" }

watch(%r{^features/step_definitions/(.+)_steps\.rb$}) do |m|
Dir[File.join("**/#{m[1]}.feature")][0] || "features"
end
end
# guard "cucumber", cucumber_options do
# watch(%r{^features/.+\.feature$})
# watch(%r{^features/support/.+$}) { "features" }

# watch(%r{^features/step_definitions/(.+)_steps\.rb$}) do |m|
# Dir[File.join("**/#{m[1]}.feature")][0] || "features"
# end
# end
end
# Note: The cmd option is now required due to the increasing number of ways
# rspec may be run, below are examples of the most common uses.
Expand Down
16 changes: 13 additions & 3 deletions app/services/stale_order_sweeper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ class StaleOrderSweeper
# There's only one class method, designed to be called from a periodic runner or similar.

def self.sweep!
# destroy stale orders and update sweep timer
stale_order_date = (Option.order_timeout + 1).minutes.ago # just to be on the safe side
stale_import_date = (Option.import_timeout + 1).minutes.ago
# raise error for orders where CC charge & order proc didn't happen atomically
self.notice_failed_orders!
# destroy stale (customer-abandoned) orders and update sweep timer
self.sweep_abandoned_orders_and_imports
end

def self.notice_failed_orders!
# first check for 'pending' CC orders and raise an alarm if any are found, since no
# order should ever be 'pending' for more than a few seconds
if (pending = Order.pending_but_paid.abandoned_since(2.minutes.ago))
Expand All @@ -17,10 +21,16 @@ def self.sweep!
# mark those order(s) as errored, so they don't trigger the warning again
pending.update_all(:authorization => Order::ERRORED)
end
end

def self.sweep_abandoned_orders_and_imports
stale_order_date = (Option.order_timeout + 1).minutes.ago # just to be on the safe side
stale_import_date = (Option.import_timeout + 1).minutes.ago
ActiveRecord::Base.transaction do
Order.where(:type => 'Order').abandoned_since(stale_order_date).destroy_all
TicketSalesImport.abandoned_since(stale_import_date).destroy_all
Option.first.update_attribute(:last_sweep, Time.current)
end
end

end
35 changes: 35 additions & 0 deletions spec/services/stale_order_sweeper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
describe StaleOrderSweeper do

describe 'abandoned orders' do
before(:each) do
@old_order = create(:order)
@old_id = old_order.id
end
specify 'are swept if too old' do
@old_order.update_attribute(:updated_at, 1.day.ago)
StaleOrderSweeper.sweep_abandoned_orders_and_imports
expect(Order.find_by(:id => @old_id)).to be_nil
end
specify 'are not swept if within abandon threshold' do
Option.first.update_attribute(:stale_order_timeout, 10) # minutes
StaleOrderSweeper.sweep_abandoned_orders_and_imports
expect(Order.find_by(:id => @old_id)).to eq(@old_order)
end
end

describe 'when an order has failed', focus: true do
before(:each) do
@o = create(:order)
@o.update_attribute(:authorization, Order::PENDING)
@o.update_attribute(:updated_at, 3.minutes.ago)
end
it 'notifies of error' do
expect(NewRelic::Agent).to receive(:notice_error)
StaleOrderSweeper.notice_failed_orders!
end
it 'changes order status to errored' do
StaleOrderSweeper.notice_failed_orders!
expect(@o.reload.authorization).to eq(Order::ERRORED)
end
end
end

0 comments on commit fdde3d6

Please sign in to comment.