2012-06-29 05:18:28 +00:00
|
|
|
# -*- coding: binary -*-
|
2005-09-22 04:53:46 +00:00
|
|
|
module Rex
|
|
|
|
|
2016-05-24 16:24:47 +00:00
|
|
|
autoload :Job, 'rex/job'
|
2005-09-22 04:53:46 +00:00
|
|
|
|
|
|
|
###
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
###
|
|
|
|
class JobContainer < Hash
|
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
def initialize
|
|
|
|
self.job_id_pool = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Adds an already running task as a symbolic job to the container.
|
|
|
|
#
|
|
|
|
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
|
|
|
|
|
|
|
|
#
|
|
|
|
# Starts a job using the supplied name and run/clean procedures.
|
|
|
|
#
|
|
|
|
def start_job(name, ctx, run_proc, clean_proc = nil)
|
|
|
|
j = add_job(name, ctx, run_proc, clean_proc)
|
|
|
|
j.start
|
|
|
|
|
|
|
|
j.jid
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
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
|
|
|
|
|
|
|
|
#
|
|
|
|
# Stops the job with the supplied name and forces it to cleanup. Stopping
|
|
|
|
# the job also leads to its removal.
|
|
|
|
#
|
|
|
|
def stop_job(jid)
|
|
|
|
if (j = self[jid.to_s])
|
|
|
|
j.stop
|
|
|
|
|
|
|
|
remove_job(j)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Removes a job that was previously running. This is typically called when
|
|
|
|
# a job completes its task.
|
|
|
|
#
|
|
|
|
def remove_job(inst)
|
|
|
|
self.delete(inst.jid.to_s)
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# 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"
|
|
|
|
#
|
|
|
|
def each(&block)
|
|
|
|
list = []
|
|
|
|
self.keys.sort.each do |sidx|
|
|
|
|
list << [sidx, self[sidx]]
|
|
|
|
end
|
|
|
|
list.each(&block)
|
|
|
|
end
|
2013-03-08 00:16:57 +00:00
|
|
|
|
2005-09-22 04:53:46 +00:00
|
|
|
protected
|
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
attr_accessor :job_id_pool # :nodoc:
|
2005-09-22 04:53:46 +00:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2008-11-16 20:54:41 +00:00
|
|
|
end
|
2010-05-20 20:42:17 +00:00
|
|
|
|