Sha256: 304979c6dca1312093b932b247ff55c71be34732e73790897842a1c0986fb43c

Contents?: true

Size: 1.95 KB

Versions: 2

Compression:

Stored size: 1.95 KB

Contents

class WorkLockNagger
  include ActionController::UrlWriter
  
  def initialize
  end
  
  def nag
    sleep 1.minute
    puts "Work Lock Nagger started"
    begin
      if File.exists? APP_CONFIG_FILE
        config = YAML::load(ERB.new(IO.read(APP_CONFIG_FILE)).result) || {}
        app_url = config[:app_url]
      end
      if app_url
        url = app_url + 'works/weekly_work_sheet_by_work_account'
      else      
        host = Socket::gethostname
        port = config[:port] || 3000
        url = url_for(:host => host, :port => port, :controller => 'works', :action => :weekly_work_sheet_by_work_account)
      end
    rescue Exception => e
      puts "Work Lock Nagger Exception"
      puts e.message
      puts e.backtrace
      exit 1
    end
    
    loop do
      puts "Nagging"
      begin
        late_work_locks = WorkLock.find(
                                        :all, 
                                        :conditions => ["end_on < ? and not exists (select id from work_locks wl2 where wl2.user_id = work_locks.user_id and wl2.end_on > work_locks.end_on)", 1.week.ago.monday ])
        
        late_users = late_work_locks.map{|wl| wl.user}.uniq
        late_users.each do |u|
          missing_date = (u.work_locks.last.end_on + 7)
          year = missing_date.year
          week = missing_date.cweek
          WorkLockNotify.deliver_nag(u, week, url + "/#{year}/#{week}")
        end
        now = Time.now
        next_nag_time = Time.local(now.year, now.month, now.day, 10, 0, 0)
        next_nag_time += 1.day if next_nag_time <= now
        sleep_duration = next_nag_time - now
        puts "Sleeping #{sleep_duration.to_i / 3600} hours #{(sleep_duration.to_i % 3600) / 60} minutes and #{sleep_duration.to_i % 60} seconds."
        puts "From #{now} to #{next_nag_time}."
        sleep sleep_duration
      rescue Exception => e
        p e
        puts e.backtrace
        sleep 5.minutes
      end
      puts "Nag ends"; STDOUT.flush
    end
  end
  
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
backlog-0.28.0 app/models/work_lock_nagger.rb
backlog-0.29.0 app/models/work_lock_nagger.rb