Class: Cron

Inherits:
Object
  • Object
show all
Defined in:
lib/cl_wiki/tools/cron.reminders.rb

Instance Method Summary collapse

Constructor Details

#initialize(crontabFilename, verbose = false) ⇒ Cron

Returns a new instance of Cron.



142
143
144
145
146
147
# File 'lib/cl_wiki/tools/cron.reminders.rb', line 142

def initialize(crontabFilename,verbose=false)
  @crontabAge = 0
  @lines = 0
  @verbose = verbose
  @crontabFilename = crontabFilename
end

Instance Method Details

#inRange(value, range) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/cl_wiki/tools/cron.reminders.rb', line 37

def inRange(value,range)
  range.split(/,/).each { | subrange |
    low, high = subrange.split(/\-/)
    low = low.to_i
    if high
      high = high.to_i
      return true if value >= low and value <= high;
    else
      return true if low == value;
    end
  }
  false
end

#loadCrontabObject



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/cl_wiki/tools/cron.reminders.rb', line 24

def loadCrontab()
  File.open(@crontabFilename, "r") do |f|
    @crontab = []
    f.each_line { | line |
      line.chomp!
      next if (line =~ /^$/)  # skip blank lines
      next if (line =~ /^\#/)  # skip comments
      @crontab << line.split(/ +/,6)
    }
  end
  @crontabAge = File.mtime(@crontabFilename)
end

#nth_wday_of_month?(nth) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
55
56
57
58
59
60
61
# File 'lib/cl_wiki/tools/cron.reminders.rb', line 51

def nth_wday_of_month?(nth)
  require 'date'

  all_wday_in_month = []
  today = Date.today
  (-5..5).each do |i|
    a_date = (today + (i*7))
    all_wday_in_month << a_date if a_date.mon == today.mon
  end
  all_wday_in_month[nth - 1] == today
end

#parse_tcommand_for_wiki_pages(tcommand) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/cl_wiki/tools/cron.reminders.rb', line 121

def parse_tcommand_for_wiki_pages(tcommand)
  content = ''
  content << tcommand + "\n\n"
  formatter = ClWikiPageFormatter.new(tcommand, '/UrgentToDo')
  formatter.formatLinks do |word|
    if formatter.isWikiName?(word)
      wiki_page_name = word
      ref_page_full_name = formatter.expand_path(wiki_page_name, '/UrgentToDo')
      puts ref_page_full_name
      if ClWikiPage.page_exists?(ref_page_full_name)
        ref_page = ClWikiPage.new(ref_page_full_name, $wiki_path)
        ref_page.read_raw_content
        content << ref_page_full_name << "\n" <<
          ('-' * ref_page_full_name.length) << "\n\n" <<
          ref_page.raw_content << "\n"
      end
    end
  end
  content
end

#runObject



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/cl_wiki/tools/cron.reminders.rb', line 162

def run()
  if @verbose
    print "\n\nDO NOT CLOSE THIS WINDOW!\n\n"
  end
  while (true)
    if @crontabAge != File.mtime(@crontabFilename)
      loadCrontab()
    end

    if $debug
      testAndLaunch(Time.now)
    else
      # we do this to strip the seconds off for pure minute comparisons
      now = str_to_time(time_to_str(Time.now))

      lastrun_fn = 'cron.reminders.lastrun'
      if File.exists?(lastrun_fn)
        lastruntime = File.readlines(lastrun_fn)[0]
        if lastruntime.nil?
          File.delete(lastrun_fn)
          next
        end
        lastrun = str_to_time(lastruntime.chomp)
      else
        # default to starting a week ago, that should be enough catchup
        lastrun = now - (60 * 60 * 24 * 7)
      end
      nextrun = lastrun + (60)
      catchup = false
      if nextrun < now
        catchup = true
        time_do = nextrun
      elsif nextrun == now
        time_do = now
      else
        time_do = nil
      end
      if time_do
        puts 'catching up ' + time_to_str(time_do) if catchup
        #puts 'doing ' + time_to_str(time_do) if @verbose
        testAndLaunch(time_do)
        File.open(lastrun_fn, 'w') do |f| f.puts time_to_str(time_do) end
      end
      sleep (60) if !catchup
    end
    break if $debug
  end
end

#str_to_time(str) ⇒ Object



149
150
151
152
153
154
155
156
# File 'lib/cl_wiki/tools/cron.reminders.rb', line 149

def str_to_time(str)
  begin
    Time.local(*([ParseDate.parsedate(str)[0..4], 0].flatten))
  rescue => e
    puts(str.inspect)
    raise e
  end
end

#testAndLaunch(now) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/cl_wiki/tools/cron.reminders.rb', line 72

def testAndLaunch(now)
  @crontab.each { | task |
    tmin, thour, tmonthday, tmonth, tweekday, tcommand = task
    launch = false

    # Launch this month of the year?
    if tmonth.eql?("*") || inRange(now.mon,tmonth)
      launch = true
    end

    # Launch this day of the month?
    if launch && !tmonthday.eql?("*") &&
      !inRange(now.mday,tmonthday)
      launch = false
    end

    # Launch this day of the week?
    if launch && !tweekday.eql?("*") &&
      !wday_in_range(now.wday, tweekday)
      launch = false
    end

    # Launch this hour of the day?
    if launch && !thour.eql?("*") &&
      !inRange(now.hour,thour)
      launch = false
    end

    # Launch this minute of the hour?
    if launch && !tmin.eql?("*") &&
      !inRange(now.min,tmin)
      launch = false;
    end

    if launch || $debug
      subj = "[rem] " + tcommand.gsub(/_/, ' ')
      body = time_to_str(now) + "\n" + parse_tcommand_for_wiki_pages(tcommand) + "\n" +
             "\n" +
             "http://localhost/clwiki/clwikicgi.rb?page=/UrgentToDo"
      to = '[email protected]'
      msg_id = "#{subj.scan(/[A-Za-z0-9]/).to_s}@clabs.org"
      do_sendmail(to, '[email protected]', subj, body, msg_id)
      puts 'sent ' + subj + "\n" +
        ('-' * (subj.length + 5)) + "\n" +
        body + "\n" if @verbose
    end
  }
end

#time_to_str(time) ⇒ Object



158
159
160
# File 'lib/cl_wiki/tools/cron.reminders.rb', line 158

def time_to_str(time)
  time.strftime("%m/%d/%Y %H:%M")
end

#wday_in_range(current_wday, tweekday) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/cl_wiki/tools/cron.reminders.rb', line 63

def wday_in_range(current_wday, tweekday)
  tweekday, nth_wday = tweekday.split('/')
  result = inRange(current_wday, tweekday)
  if result && nth_wday
    result = nth_wday_of_month?(nth_wday.to_i)
  end
  result
end