Created
February 22, 2011 10:01
-
-
Save anonymous/838448 to your computer and use it in GitHub Desktop.
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
class NotificationCluster < ActiveRecord::Base | |
CLUSTER_TIMEOUT = 30.seconds | |
serialize :associated_files # an array of file ids | |
scope :unsent, :conditions => {:delivered => false} | |
scope :within_the_last, lambda {|secs| | |
{:conditions => ["created_at >= ?", secs.ago]} | |
} | |
scope :for, lambda{|arguments| | |
# add some more conditions here. | |
} | |
def self.find_or_create_for(*args) | |
# Find an existing NotificationCluster object for the given args, | |
# or create a new one. | |
n = unsent.within_the_last(CLUSTER_TIMEOUT).for(*args).first | |
n ||= new_for(*args) | |
end | |
def self.new_for(*args) | |
# build a new NotificationCluster object here | |
end | |
def enqueue! | |
# First, remove any existing enqueued jobs for this action so we can have events that | |
# are ongoing | |
Resque.remove_delayed(NotificationSendingJob, self.id) | |
# Then set another time for x seconds from now. | |
# if there's an event in between now and x seconds, then this job will be replaced | |
# with a new scheduled job | |
Resque.enqueue_at(CLUSTER_TIMEOUT.from_now, NotificationSendingJob, self.id) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment