module Rex
class Job
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
def synchronized_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
def start(async = false)
self.start_time = ::Time.now
if (async)
self.job_thread = Rex::ThreadFactory.spawn("JobID(#{jid})-#{name}", false) {
::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
def stop
if (self.job_thread)
self.job_thread.kill
self.job_thread = nil
end
synchronized_cleanup
end
attr_reader :name
attr_reader :jid
attr_reader :start_time
attr_reader :ctx
protected
attr_writer :name
attr_writer :jid
attr_accessor :job_thread
attr_accessor :container
attr_accessor :run_proc
attr_accessor :clean_proc
attr_writer :ctx
attr_writer :start_time
attr_accessor :cleanup_mutex
attr_accessor :cleaned_up
end
end