Class: Lobster::Job
- Inherits:
-
Object
- Object
- Lobster::Job
- Defined in:
- lib/lobster/job.rb
Instance Attribute Summary collapse
-
#next_run ⇒ Object
Returns the value of attribute next_run.
Instance Method Summary collapse
-
#initialize(name) ⇒ Job
constructor
A new instance of Job.
- #kill(sig) ⇒ Object
- #reload(options, lobster_dir) ⇒ Object
- #run(out, err) ⇒ Object
- #running? ⇒ Boolean
Constructor Details
Instance Attribute Details
#next_run ⇒ Object
Returns the value of attribute next_run.
3 4 5 |
# File 'lib/lobster/job.rb', line 3 def next_run @next_run end |
Instance Method Details
#kill(sig) ⇒ Object
53 54 55 56 57 58 |
# File 'lib/lobster/job.rb', line 53 def kill(sig) if @pid Lobster.logger.info "Killing job #{@name} with pid #{@pid} and all its children" kill_tree sig, @pid end end |
#reload(options, lobster_dir) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/lobster/job.rb', line 11 def reload(, lobster_dir) [:delay] ||= 10 [:directory] ||= lobster_dir [:command, :delay, :user, :directory].each do |opt| val = instance_variable_get "@#{opt}" if [opt] != val Lobster.logger.info "Job #{opt} updated for #{@name}, was \"#{val}\", now \"#{[opt]}\"" if val instance_variable_set "@#{opt}", .delete(opt) # special case: reset @next_run if delay is updated @next_run = nil if opt == :delay and not running? end end @name ||= "<unnamed_job_#{@command.hash.abs}>" @next_run ||= Time.now + rand(@delay*60) end |
#run(out, err) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/lobster/job.rb', line 41 def run(out,err) Lobster.logger.info "Starting job #{@name}" command_line = @user ? "sudo -inu #{@user} 'cd #{@directory}; #{@command}'" : @command begin @pid = spawn(command_line, :out=>out, :err=>err, :chdir=> @directory) rescue Exception => e Lobster.logger.error "#{e}: error when starting job #{@name}" @next_run = Time.now + 10 end end |
#running? ⇒ Boolean
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/lobster/job.rb', line 29 def running? return false if @pid.nil? if Process.wait @pid, Process::WNOHANG Lobster.logger.error "Job #{@name} Failed with status #{$?}" unless $?.success? @pid = nil @next_run = Time.now + @delay*60 false else true end end |