Class: Rex::JobContainer

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

Overview

This class contains zero or more abstract jobs that can be enumerated and stopped in a generic fashion. This is used to provide a mechanism for keeping track of arbitrary contexts that may or may not require a dedicated thread.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeJobContainer

Returns a new instance of JobContainer.



14
15
16
# File 'lib/rex/job_container.rb', line 14

def initialize
  self.job_id_pool = 0
end

Instance Attribute Details

#job_id_poolObject (protected)

:nodoc:



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

def job_id_pool
  @job_id_pool
end

Instance Method Details

#add_job(name, ctx, run_proc, clean_proc) ⇒ Object

Adds an already running task as a symbolic job to the container.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rex/job_container.rb', line 21

def add_job(name, ctx, run_proc, clean_proc)
  real_name = name
  count     = 0
  jid       = job_id_pool

  self.job_id_pool += 1

  # If we were not supplied with a job name, pick one from the hat
  if (real_name == nil)
    real_name = '#' + jid.to_s
  end

  # Find a unique job name
  while (j = self[real_name])
    real_name  = name + " #{count}"
    count     += 1
  end

  j = Job.new(self, jid, real_name, ctx, run_proc, clean_proc)

  self[jid.to_s] = j
end

#each(&block) ⇒ Object

Overrides the builtin ‘each’ operator to avoid the following exception on Ruby 1.9.2+

"can't add a new key into hash during iteration"


91
92
93
94
95
96
97
# File 'lib/rex/job_container.rb', line 91

def each(&block)
  list = []
  self.keys.sort.each do |sidx|
    list << [sidx, self[sidx]]
  end
  list.each(&block)
end

#remove_job(inst) ⇒ Object

Removes a job that was previously running. This is typically called when a job completes its task.



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

def remove_job(inst)
  self.delete(inst.jid.to_s)
end

#start_bg_job(name, ctx, run_proc, clean_proc = nil, async = true) ⇒ Object

Starts a background job that doesn’t call the cleanup routine or run the run_proc in its own thread. Rather, the run_proc is called immediately and the clean_proc is never called until the job is removed from the job container.



60
61
62
63
64
65
# File 'lib/rex/job_container.rb', line 60

def start_bg_job(name, ctx, run_proc, clean_proc = nil, async = true)
  j = add_job(name, ctx, run_proc, clean_proc)
  j.start(async)

  j.jid
end

#start_job(name, ctx, run_proc, clean_proc = nil) ⇒ Object

Starts a job using the supplied name and run/clean procedures.



47
48
49
50
51
52
# File 'lib/rex/job_container.rb', line 47

def start_job(name, ctx, run_proc, clean_proc = nil)
  j = add_job(name, ctx, run_proc, clean_proc)
  j.start

  j.jid
end

#stop_job(jid) ⇒ Object

Stops the job with the supplied name and forces it to cleanup. Stopping the job also leads to its removal.



71
72
73
74
75
76
77
# File 'lib/rex/job_container.rb', line 71

def stop_job(jid)
  if (j = self[jid.to_s])
    j.stop

    remove_job(j)
  end
end