Class: WorkLockNagger

Inherits:
Object
  • Object
show all
Includes:
ActionController::UrlWriter
Defined in:
app/models/work_lock_nagger.rb

Instance Method Summary collapse

Constructor Details

#initialize(start_delay = 1) ⇒ WorkLockNagger

Returns a new instance of WorkLockNagger.



4
5
6
# File 'app/models/work_lock_nagger.rb', line 4

def initialize(start_delay = 1)
  @start_delay = start_delay
end

Instance Method Details

#nagObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/models/work_lock_nagger.rb', line 8

def nag
  sleep @start_delay.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'
    else      
      host = Socket::gethostname
      port = config && config[:port] || 3000
      url = url_for(:host => host, :port => port, :controller => 'works', :action => :weekly_work_sheet)
    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