Skip to content

Commit

Permalink
update Agent#events_count when events are expired
Browse files Browse the repository at this point in the history
  • Loading branch information
cantino committed Jan 5, 2014
1 parent b3304a7 commit 276d593
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 8 deletions.
2 changes: 2 additions & 0 deletions app/models/event.rb
Expand Up @@ -21,6 +21,8 @@ def reemit!
end

def self.cleanup_expired!
affected_agents = Event.where("expires_at IS NOT NULL AND expires_at < ?", Time.now).group("agent_id").pluck(:agent_id)
Event.where("expires_at IS NOT NULL AND expires_at < ?", Time.now).delete_all
Agent.where(:id => affected_agents).update_all "events_count = (select count(*) from events where agent_id = agents.id)"
end
end
2 changes: 1 addition & 1 deletion spec/controllers/events_controller_spec.rb
Expand Up @@ -49,7 +49,7 @@
}.should change { Event.count }.by(1)
Event.last.payload.should == events(:bob_website_agent_event).payload
Event.last.agent.should == events(:bob_website_agent_event).agent
Event.last.created_at.should be_within(1).of(Time.now)
Event.last.created_at.to_i.should be_within(2).of(Time.now.to_i)
end

it "can only re-emit Events for the current user" do
Expand Down
2 changes: 2 additions & 0 deletions spec/fixtures/agents.yml
@@ -1,6 +1,7 @@
jane_website_agent:
type: Agents::WebsiteAgent
user: jane
events_count: 1
schedule: "5pm"
name: "ZKCD"
options: <%= {
Expand All @@ -16,6 +17,7 @@ jane_website_agent:
bob_website_agent:
type: Agents::WebsiteAgent
user: bob
events_count: 1
schedule: "midnight"
name: "ZKCD"
options: <%= {
Expand Down
39 changes: 32 additions & 7 deletions spec/models/event_spec.rb
Expand Up @@ -18,20 +18,45 @@
end

describe ".cleanup_expired!" do
it "removes any Events whose expired_at date is non-null and in the past" do
event = agents(:jane_weather_agent).create_event :expires_at => 2.hours.from_now
it "removes any Events whose expired_at date is non-null and in the past, updating Agent counter caches" do
half_hour_event = agents(:jane_weather_agent).create_event :expires_at => 20.minutes.from_now
one_hour_event = agents(:bob_weather_agent).create_event :expires_at => 1.hours.from_now
two_hour_event = agents(:jane_weather_agent).create_event :expires_at => 2.hours.from_now
three_hour_event = agents(:jane_weather_agent).create_event :expires_at => 3.hours.from_now
non_expiring_event = agents(:bob_weather_agent).create_event({})

initial_bob_count = agents(:bob_weather_agent).reload.events_count
initial_jane_count = agents(:jane_weather_agent).reload.events_count

current_time = Time.now
stub(Time).now { current_time }

Event.cleanup_expired!
Event.find_by_id(event.id).should_not be_nil
current_time = 119.minutes.from_now
Event.find_by_id(half_hour_event.id).should_not be_nil
Event.find_by_id(one_hour_event.id).should_not be_nil
Event.find_by_id(two_hour_event.id).should_not be_nil
Event.find_by_id(three_hour_event.id).should_not be_nil
Event.find_by_id(non_expiring_event.id).should_not be_nil
agents(:bob_weather_agent).reload.events_count.should == initial_bob_count
agents(:jane_weather_agent).reload.events_count.should == initial_jane_count

current_time = 119.minutes.from_now # move almost 2 hours into the future
Event.cleanup_expired!
Event.find_by_id(event.id).should_not be_nil
current_time = 2.minutes.from_now
Event.find_by_id(half_hour_event.id).should be_nil
Event.find_by_id(one_hour_event.id).should be_nil
Event.find_by_id(two_hour_event.id).should_not be_nil
Event.find_by_id(three_hour_event.id).should_not be_nil
Event.find_by_id(non_expiring_event.id).should_not be_nil
agents(:bob_weather_agent).reload.events_count.should == initial_bob_count - 1
agents(:jane_weather_agent).reload.events_count.should == initial_jane_count - 1

current_time = 2.minutes.from_now # move 2 minutes further into the future
Event.cleanup_expired!
Event.find_by_id(event.id).should be_nil
Event.find_by_id(two_hour_event.id).should be_nil
Event.find_by_id(three_hour_event.id).should_not be_nil
Event.find_by_id(non_expiring_event.id).should_not be_nil
agents(:bob_weather_agent).reload.events_count.should == initial_bob_count - 1
agents(:jane_weather_agent).reload.events_count.should == initial_jane_count - 2
end

it "doesn't touch Events with no expired_at" do
Expand Down

0 comments on commit 276d593

Please sign in to comment.