Class: Rex::Job

Inherits:
Object
  • Object
show all
Defined in:
lib/rex/job_container.rb

Overview

This class is the concrete representation of an abstract job.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(container, jid, name, ctx, run_proc, clean_proc) ⇒ Job

Creates an individual job instance and initializes it with the supplied parameters.



15
16
17
18
19
20
21
22
23
# File 'lib/rex/job_container.rb', line 15

def initialize(container, jid, name, ctx, run_proc, clean_proc)
  self.container  = container
  self.jid        = jid
  self.name       = name
  self.run_proc   = run_proc
  self.clean_proc = clean_proc
  self.ctx        = ctx
  self.start_time = nil
end

Instance Attribute Details

#ctxObject

Some job context.



83
84
85
# File 'lib/rex/job_container.rb', line 83

def ctx
  @ctx
end

#jidObject

The job identifier as assigned by the job container.



73
74
75
# File 'lib/rex/job_container.rb', line 73

def jid
  @jid
end

#nameObject

The name of the job.



68
69
70
# File 'lib/rex/job_container.rb', line 68

def name
  @name
end

#start_timeObject

The time at which this job was started.



78
79
80
# File 'lib/rex/job_container.rb', line 78

def start_time
  @start_time
end

Instance Method Details

#start(async = false) ⇒ Object

Runs the job in the context of its own thread if the async flag is false. Otherwise, the job is run inline.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rex/job_container.rb', line 29

def start(async = false)
  self.start_time = Time.now
  if (async)
    self.job_thread = Rex::ThreadFactory.spawn("JobID(#{jid})-#{name}", false) {
      # Deschedule our thread momentarily
      ::IO.select(nil, nil, nil, 0.01)

      begin
        run_proc.call(ctx)
      ensure
        clean_proc.call(ctx)
        container.remove_job(self)
      end
    }
  else
    begin
      run_proc.call(ctx)
    rescue ::Exception
      container.stop_job(jid)
      raise $!
    end
  end
end

#stopObject

Stops the job if it’s currently running and calls its cleanup procedure



56
57
58
59
60
61
62
63
# File 'lib/rex/job_container.rb', line 56

def stop
  if (self.job_thread)
    self.job_thread.kill
    self.job_thread = nil
  end

  clean_proc.call(ctx) if (clean_proc)
end