Class: Rex::Job
- Inherits:
-
Object
- Object
- Rex::Job
- Defined in:
- lib/rex/job_container.rb
Overview
This class is the concrete representation of an abstract job.
Instance Attribute Summary collapse
-
#ctx ⇒ Object
readonly
Some job context.
-
#jid ⇒ Object
readonly
The job identifier as assigned by the job container.
-
#name ⇒ Object
readonly
The name of the job.
-
#start_time ⇒ Object
readonly
The time at which this job was started.
Instance Method Summary collapse
-
#initialize(container, jid, name, ctx, run_proc, clean_proc) ⇒ Job
constructor
Creates an individual job instance and initializes it with the supplied parameters.
-
#start(async = false) ⇒ Object
Runs the job in the context of its own thread if the async flag is false.
-
#stop ⇒ Object
Stops the job if it’s currently running and calls its cleanup procedure.
Constructor Details
#initialize(container, jid, name, ctx, run_proc, clean_proc) ⇒ Job
Creates an individual job instance and initializes it with the supplied parameters.
14 15 16 17 18 19 20 21 22 |
# File 'lib/rex/job_container.rb', line 14 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
#ctx ⇒ Object
Some job context.
82 83 84 |
# File 'lib/rex/job_container.rb', line 82 def ctx @ctx end |
#jid ⇒ Object
The job identifier as assigned by the job container.
72 73 74 |
# File 'lib/rex/job_container.rb', line 72 def jid @jid end |
#name ⇒ Object
The name of the job.
67 68 69 |
# File 'lib/rex/job_container.rb', line 67 def name @name end |
#start_time ⇒ Object
The time at which this job was started.
77 78 79 |
# File 'lib/rex/job_container.rb', line 77 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.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/rex/job_container.rb', line 28 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 |
#stop ⇒ Object
Stops the job if it’s currently running and calls its cleanup procedure
55 56 57 58 59 60 61 62 |
# File 'lib/rex/job_container.rb', line 55 def stop if (self.job_thread) self.job_thread.kill self.job_thread = nil end clean_proc.call(ctx) if (clean_proc) end |