Class: Lobster::Job

Inherits:
Object
  • Object
show all
Defined in:
lib/lobster/job.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Job

Returns a new instance of Job.



5
6
7
8
9
# File 'lib/lobster/job.rb', line 5

def initialize(name)
  @name = name
  Lobster.logger.info "Job #{name} created."
  @pid = nil
end

Instance Attribute Details

#next_runObject

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(options, lobster_dir)
  options[:delay] ||= 10
  options[:directory] ||= lobster_dir

  [:command, :delay, :user, :directory].each do |opt|
    val = instance_variable_get "@#{opt}"
    if options[opt] != val
      Lobster.logger.info "Job #{opt} updated for #{@name}, was \"#{val}\", now \"#{options[opt]}\"" if val
      instance_variable_set "@#{opt}", options.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

Returns:

  • (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