Class: Rex::Job

Inherits:
Object
  • Object
show all
Defined in:
lib/rex/job.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
24
25
# File 'lib/rex/job.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
  self.cleanup_mutex = Mutex.new
  self.cleaned_up = false
end

Instance Attribute Details

#clean_procObject (protected)

:nodoc:


104
105
106
# File 'lib/rex/job.rb', line 104

def clean_proc
  @clean_proc
end

#cleaned_upObject (protected)

:nodoc:


108
109
110
# File 'lib/rex/job.rb', line 108

def cleaned_up
  @cleaned_up
end

#cleanup_mutexObject (protected)

:nodoc:


107
108
109
# File 'lib/rex/job.rb', line 107

def cleanup_mutex
  @cleanup_mutex
end

#containerObject (protected)

:nodoc:


102
103
104
# File 'lib/rex/job.rb', line 102

def container
  @container
end

#ctxObject

Some job context.


95
96
97
# File 'lib/rex/job.rb', line 95

def ctx
  @ctx
end

#jidObject

The job identifier as assigned by the job container.


85
86
87
# File 'lib/rex/job.rb', line 85

def jid
  @jid
end

#job_threadObject (protected)

:nodoc:


101
102
103
# File 'lib/rex/job.rb', line 101

def job_thread
  @job_thread
end

#nameObject

The name of the job.


80
81
82
# File 'lib/rex/job.rb', line 80

def name
  @name
end

#run_procObject (protected)

:nodoc:


103
104
105
# File 'lib/rex/job.rb', line 103

def run_proc
  @run_proc
end

#start_timeObject

The time at which this job was started.


90
91
92
# File 'lib/rex/job.rb', line 90

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.


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rex/job.rb', line 41

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
        synchronized_cleanup
        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


68
69
70
71
72
73
74
75
# File 'lib/rex/job.rb', line 68

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

  synchronized_cleanup
end

#synchronized_cleanupObject


27
28
29
30
31
32
33
34
35
# File 'lib/rex/job.rb', line 27

def synchronized_cleanup
  # Avoid start and stop both calling cleanup
  self.cleanup_mutex.synchronize do
    unless cleaned_up
      self.cleaned_up = true
      self.clean_proc.call(ctx) if self.clean_proc
    end
  end
end