Skip to content

Instantly share code, notes, and snippets.

Created February 22, 2011 10:01
Show Gist options
  • Save anonymous/838448 to your computer and use it in GitHub Desktop.
Save anonymous/838448 to your computer and use it in GitHub Desktop.
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