Class: RQ::Cron

Inherits:
MainHelper show all
Defined in:
lib/rq/cron.rb

Overview

a class for managing crontab entries and to start/stop rq

Constant Summary

Constants included from Logging

Logging::DIV0, Logging::DIV1, Logging::DIV2, Logging::DIV3, Logging::EOL, Logging::SEC0, Logging::SEC1, Logging::SEC2, Logging::SEC3

Instance Attribute Summary

Attributes inherited from MainHelper

#argv, #cmd, #dot_rq_dir, #env, #fields, #job_stdin, #loops, #main, #mode, #options, #program, #q, #qpath, #quiet, #stdin

Instance Method Summary collapse

Methods inherited from MainHelper

#dumping_yaml_tuples, #field_match, #init_job_stdin!, #loadio, #loadyaml, #set_q

Methods included from Logging

append_features

Methods included from Logging::LogMethods

#debug, #error, #fatal, #info, #logerr, #logger, #logger=, #warn

Methods included from Util

#alive?, append_features, #btrace, #columnize, #defval, #emsg, #erreq, #errmsg, #escape, #escape!, #exec, export, #fork, #getopt, #hashify, #hms, #host, #hostname, #klass, #maim, #mcp, #realpath, #stamptime, #system, #timestamp, #tmpnam, #uncache, #which_ruby

Constructor Details

#initialize(*a, &b) ⇒ Cron

–{{{



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/rq/cron.rb', line 14

def initialize *a, &b
#--{{{
  super
  ruby = Util::which_ruby
  this = Util::realpath( File.expand_path( $0 ) )
  q = qpath

  @cmd = "#{ ruby } #{ this } #{ q }"
  @md5 = lambda{|mode| Digest::MD5::hexdigest "#{ @cmd } #{ mode }" }
#--}}}
end

Instance Method Details

#cronObject

–}}}



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rq/cron.rb', line 25

def cron 
#--{{{
  which = @argv.shift || 'start'
  which = which.strip.downcase
  #abort "arg not add|start|shutdown|stop" unless %w( start shutdown stop ).include? which
  msg = "cron_#{ which }"
  begin
    send msg
  rescue NoMethodError
    raise ArgumentError, which
  end
  self
#--}}}
end

#cron_addObject

–}}}



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/rq/cron.rb', line 39

def cron_add
#--{{{
  lines = `crontab -l`.split "\n"

  found = nil

  re = %r/###\s*md5:#{ @md5[:start] }/

  lines.each do |line|
    line.strip!
    next if line[ %r/^\s*#/ ]
    min, hour, dom, mon, dow, entry = line.split %r/\s+/, 6
    next unless entry
    entry.strip!
    entry.gsub! %r/#[^'"]$/, ''
    entry.strip!
    found = re.match entry
    break if found
  end

  unless found
    opts = @options.map{|kv| "'--#{ kv.join('=') }'" }.join(' ')
    entries = [
      "*/15 * * * * #{ @cmd } start #{ opts } ###md5:#{ @md5[:start] }\n",
      "0 0 * * * #{ @cmd } rotate ###md5:#{ @md5[:start] }\n",
    ]
    tmp = Tempfile::new Process::pid.to_s
    lines.each{|line| tmp << "#{ line }\n"}
    entries.each do |entry|
      tmp << entry 
    end
    tmp.close
    system("crontab #{ tmp.path }") or abort("failed to cronify!")
    tmp.close!
    entries.each do |entry|
      puts entry
    end
  end
#--}}}
end

#cron_deleteObject

–}}}



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
120
121
122
123
124
125
126
# File 'lib/rq/cron.rb', line 95

def cron_delete
#--{{{
  lines = `crontab -l`.split "\n"

  re = %r/###\s*md5:(#{ @md5[:start] })/
  found = [] 

  lines.each_with_index do |line, idx|
    line.strip!
    next if line[ %r/^\s*#/ ]
    min, hour, dom, mon, dow, entry = line.split %r/\s+/, 6
    next unless entry
    entry.strip!
    entry.gsub! %r/#[^'"]$/, ''
    entry.strip!
    found << idx if(re.match entry)
  end

p found

  unless found.empty?
    deleted = [] 
    found.each{|idx| deleted << lines[idx]; lines.delete_at(idx)} 
    tmp = Tempfile::new Process::pid.to_s
    lines.each{|line| tmp << "#{ line }\n"}
    tmp.close
    system("crontab #{ tmp.path }") or abort("failed to cronify!")
    tmp.close!
    puts deleted
  end
#--}}}
end

#cron_shutdownObject

–}}}



127
128
129
130
131
132
# File 'lib/rq/cron.rb', line 127

def cron_shutdown
#--{{{
  cron_delete
  main.shutdown
#--}}}
end

#cron_startObject

–}}}



89
90
91
92
93
94
# File 'lib/rq/cron.rb', line 89

def cron_start
#--{{{
  cron_add
  #main.start
#--}}}
end

#cron_stopObject

–}}}



133
134
135
136
137
138
# File 'lib/rq/cron.rb', line 133

def cron_stop
#--{{{
  cron_delete
  main.stop
#--}}}
end

#cron_tabObject

–}}}



79
80
81
82
83
84
85
86
87
88
# File 'lib/rq/cron.rb', line 79

def cron_tab
#--{{{
  opts = @options.map{|kv| "'--#{ kv.join('=') }'" }.join(' ')
  entries = [
    "*/15 * * * * #{ @cmd } start #{ opts } ###md5:#{ @md5[:start] }\n",
    "0 0 * * * #{ @cmd } rotate ###md5:#{ @md5[:start] }\n",
  ]
  puts entries
#--}}}
end