Class: RQ::Cron

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

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, #main, #mode, #options, #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

Returns a new instance of Cron.


14
15
16
17
18
19
20
21
22
23
24
# File 'lib/rq-3.0.0/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-3.0.0/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
# File 'lib/rq-3.0.0/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
    tmp = Tempfile::new Process::pid.to_s
    lines.each{|line| tmp << "#{ line }\n"}
    opts = @options.map{|kv| "'--#{ kv.join('=') }'" }.join(' ')
    entry = "*/15 * * * * #{ @cmd } start #{ opts } ###md5:#{ @md5[:start] }\n"
    tmp << entry 
    tmp.close
    system("crontab #{ tmp.path }") or abort("failed to cronify!")
    tmp.close!
    puts entry
  end
#--}}}
end

#cron_deleteObject


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
# File 'lib/rq-3.0.0/cron.rb', line 78

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

  unless found.empty?
    deleted = lines[*found]
    lines.delete_at *found
    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

–}}}


108
109
110
111
112
113
# File 'lib/rq-3.0.0/cron.rb', line 108

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

#cron_startObject


72
73
74
75
76
77
# File 'lib/rq-3.0.0/cron.rb', line 72

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

#cron_stopObject

–}}}


114
115
116
117
118
119
# File 'lib/rq-3.0.0/cron.rb', line 114

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