@@ -60,8 +60,6 @@ class Agent < ActiveRecord::Base |
||
| 60 | 60 |
|
| 61 | 61 |
scope :of_type, lambda { |type|
|
| 62 | 62 |
type = case type |
| 63 |
- when String, Symbol, Class |
|
| 64 |
- type.to_s |
|
| 65 | 63 |
when Agent |
| 66 | 64 |
type.class.to_s |
| 67 | 65 |
else |
@@ -26,6 +26,10 @@ class Event < ActiveRecord::Base |
||
| 26 | 26 |
after_create :update_agent_last_event_at |
| 27 | 27 |
after_create :possibly_propagate |
| 28 | 28 |
|
| 29 |
+ scope :expired, lambda {
|
|
| 30 |
+ where("expires_at IS NOT NULL AND expires_at < ?", Time.now)
|
|
| 31 |
+ } |
|
| 32 |
+ |
|
| 29 | 33 |
# Emit this event again, as a new Event. |
| 30 | 34 |
def reemit! |
| 31 | 35 |
agent.create_event :payload => payload, :lat => lat, :lng => lng |
@@ -34,8 +38,8 @@ class Event < ActiveRecord::Base |
||
| 34 | 38 |
# Look for Events whose `expires_at` is present and in the past. Remove those events and then update affected Agents' |
| 35 | 39 |
# `events_counts` cache columns. This method is called by bin/schedule.rb periodically. |
| 36 | 40 |
def self.cleanup_expired! |
| 37 |
- affected_agents = Event.where("expires_at IS NOT NULL AND expires_at < ?", Time.now).group("agent_id").pluck(:agent_id)
|
|
| 38 |
- Event.where("expires_at IS NOT NULL AND expires_at < ?", Time.now).delete_all
|
|
| 41 |
+ affected_agents = Event.expired.group("agent_id").pluck(:agent_id)
|
|
| 42 |
+ Event.expired.delete_all |
|
| 39 | 43 |
Agent.where(:id => affected_agents).update_all "events_count = (select count(*) from events where agent_id = agents.id)" |
| 40 | 44 |
end |
| 41 | 45 |
|