From 74314f59eadfcfacdde0e803dc6afbeaeeea9edb Mon Sep 17 00:00:00 2001 From: Kevin and aimee daniells Date: Sun, 26 Dec 2010 17:15:52 +0000 Subject: [PATCH] Allow running the notification task more than once --- ...last_emailed_and_tweeted_fields_to_user.rb | 11 +++ lib/tasks/deliver_geohash_notifications.rake | 86 ++++++++++--------- 2 files changed, 57 insertions(+), 40 deletions(-) create mode 100644 db/migrate/20101226165500_add_last_emailed_and_tweeted_fields_to_user.rb diff --git a/db/migrate/20101226165500_add_last_emailed_and_tweeted_fields_to_user.rb b/db/migrate/20101226165500_add_last_emailed_and_tweeted_fields_to_user.rb new file mode 100644 index 0000000..0e9ec4c --- /dev/null +++ b/db/migrate/20101226165500_add_last_emailed_and_tweeted_fields_to_user.rb @@ -0,0 +1,11 @@ +class AddLastEmailedAndTweetedFieldsToUser < ActiveRecord::Migration + def self.up + add_column :users, :last_emailed, :datetime + add_column :users, :last_tweeted, :datetime + end + + def self.down + remove_column :users, :last_tweeted + remove_column :users, :last_emailed + end +end diff --git a/lib/tasks/deliver_geohash_notifications.rake b/lib/tasks/deliver_geohash_notifications.rake index 806d873..18c698e 100755 --- a/lib/tasks/deliver_geohash_notifications.rake +++ b/lib/tasks/deliver_geohash_notifications.rake @@ -7,62 +7,65 @@ namespace :geohashing do start_time = Time.now date = Date.today - # Return early if today's dow has already been found (ie it's a weekend and notifications were delivered yesterday) + # See whether today's dow value has already been found if Dow.count(:all, :conditions => {:date => date.strftime('%Y-%m-%d')}) > 0 puts "Dow already found for #{date.strftime('%Y-%m-%d')}" - exit - end + else - # Look for today's dow value and keep looking until it is found - # Must have at least one dow value to be worth continuing. - dow_found = false - while !dow_found do - puts "Looking for dow for #{date.strftime('%Y-%m-%d')}" - dow = Dow.find_or_create_for_date(date) - if dow.nil? - puts 'Waiting 10 minutes' - sleep 10*60 - else - dow_found = true + # Look for today's dow value and keep looking until it is found + # Must have at least one dow value to be worth continuing. + dow_found = false + while !dow_found do + puts "Looking for dow for #{date.strftime('%Y-%m-%d')}" + dow = Dow.find_or_create_for_date(date) + if dow.nil? + puts 'Waiting 10 minutes' + sleep 10*60 + else + dow_found = true + end end - end - # Check whether there are any other dow dates available already - # These are optional extras for weekends and public holidays. - dows = [dow] - until dow.nil? do - date += 1.day - puts "Looking for optional extra dow for #{date.strftime('%Y-%m-%d')}" - dow = Dow.find_or_create_for_date(date) - if dow.nil? - puts "Dow for #{date.strftime('%Y-%m-%d')} not yet available" - else - dows << dow + # Check whether there are any other dow dates available already + # These are optional extras for weekends and public holidays. + dows = [dow] + until dow.nil? do + date += 1.day + puts "Looking for optional extra dow for #{date.strftime('%Y-%m-%d')}" + dow = Dow.find_or_create_for_date(date) + if dow.nil? + puts "Dow for #{date.strftime('%Y-%m-%d')} not yet available" + else + dows << dow + end end - end - # Find geohash locations for all known graticules on all dows found - date_range = Range.new(dows.first.date, (dows.last.date + 1.day)) - date_range.each do |date| - puts "Looking up geohash locations for #{date.strftime('%Y-%m-%d')}" - Graticule.all.each do |graticule| - puts "* #{graticule.display_name}" - Geohash.find_or_create(date, graticule.latitude, graticule.longitude) - end + # Find geohash locations for all known graticules on all dows found + date_range = Range.new(dows.first.date, (dows.last.date + 1.day)) + date_range.each do |date| + puts "Looking up geohash locations for #{date.strftime('%Y-%m-%d')}" + Graticule.all.each do |graticule| + puts "* #{graticule.display_name}" + Geohash.find_or_create(date, graticule.latitude, graticule.longitude) + end - # Look up the globalhash - puts "Looking up the globalhash for #{date.strftime('%Y-%m-%d')}" - Globalhash.find_or_create(date) + # Look up the globalhash + puts "Looking up the globalhash for #{date.strftime('%Y-%m-%d')}" + Globalhash.find_or_create(date) + end end # Send emails number_sent = 0 User.receiving_email.each do |user| next if user.graticules.size == 0 + period = user.last_emailed || 1.day.ago + next if user.new_geohashes_since(period).empty? ActionMailer::Base.smtp_settings[:user_name] = "activegeohasher#{number_sent / 150}@gmail.com" begin puts "Delivering email to #{user.name}" - Notifier.deliver_upcoming_geohashes(user, start_time) + Notifier.deliver_upcoming_geohashes(user, period) + user.update_attribute(:last_emailed, Time.now) number_sent += 1 rescue Exception => e Fail.create!(:klass => e.class, @@ -77,8 +80,11 @@ namespace :geohashing do tweeter = Tweeter.new User.receiving_direct_messages.each do |user| next if user.graticules.size == 0 + period = user.last_tweeted || 1.day.ago + next if user.new_geohashes_since(period).empty? puts "Sending direct messages to #{user.twitter_username}" - tweeter.deliver_upcoming_geohashes(user, start_time) + tweeter.deliver_upcoming_geohashes(user, period) + user.update_attribute(:last_tweeted, Time.now) end end end