Class: Cfruby::Scheduler::Scheduler

Inherits:
Object
  • Object
show all
Defined in:
lib/libcfruby/scheduler.rb

Overview

Base class implementation of a Scheduler interface

Instance Method Summary collapse

Instance Method Details

#schedule(program, schedule, user = 'root') ⇒ Object

Schedules program according to schedule (schedule takes standard crontab format). If program is already scheduled according to schedule the call will do nothing silently



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/libcfruby/scheduler.rb', line 26

def schedule(program, schedule, user='root')
	Cfruby.controller.attempt("adding \"#{program}\" to crontab", 'reversible', 'destructive') {
		tmp = Tempfile.new('cfruby-cron')
		output = Exec.exec('crontab -u ' + user + ' -l > ' + tmp.path)
		if(output[1].length > 0 && output[1][0] !~ /no crontab for/ )
			raise(SchedulerError, "Unable to get schedule for #{user}")
		end
		added = FileEdit.file_must_contain(tmp.path, "#{schedule} #{program}")
		if(added)
			output = Exec.exec("crontab -u #{user} #{Cfruby::Exec.shellescape(tmp.path)}")
			if(output[1].length > 0)
				raise(SchedulerError, "Unable to schedule \"#{program}\" - #{output[1][1].strip()}")
			end
		else
			Cfruby.controller.attempt_abort("No changes made")
		end
	}
end

#unschedule(regex, user = 'root') ⇒ Object

Removes any scheduled program that matches regex (regex may be given as a String or Regexp, or an Array of either)



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/libcfruby/scheduler.rb', line 48

def unschedule(regex, user='root')
	if(regex.kind_of?(Array))
		regex.each() { |r|
			unschedule(r)
		}
	end
	
	if regex.kind_of?(String)
		regex = Regexp.new(Regexp.escape(String))
	end
	
	Cfruby.controller.attempt("removing \"#{regex.source}\" from crontab", 'nonreversible', 'destructive') {
		if(!regex.kind_of?(Regexp))
			regex = Regexp.new(Regexp.escape(regex.to_s))
		end
		tmp = Tempfile.new('cfruby-cron')
		output = Exec.exec('crontab -u ' + user + ' -l > ' + tmp.path)
		if(output[1].length > 0 && output[1][0] !~ /no crontab for/)
			raise(SchedulerError, "Unable to remove \"#{regex.source()}\" from schedule - #{output[1][1].strip()}")
		end
		added = FileEdit.file_must_not_contain(tmp.path, regex)
		if(added)
			output = Exec.exec("crontab #{Cfruby::Exec.shellescape(tmp.path)}")
			if(output[1].length > 0)
				raise(SchedulerError, "Unable to remove \"#{regex.source()}\" from schedule - #{output[1][1].strip()}")
			end
		else
			Cfruby.controller.attempt_abort("No changes made")
		end
	}
end