Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/lib/rex/job.rb
Views: 11766
# -*- coding: binary -*-1module Rex23###4#5# This class is the concrete representation of an abstract job.6#7###8class Job910#11# Creates an individual job instance and initializes it with the supplied12# parameters.13#14def initialize(container, jid, name, ctx, run_proc, clean_proc)15self.container = container16self.jid = jid17self.name = name18self.run_proc = run_proc19self.clean_proc = clean_proc20self.ctx = ctx21self.start_time = nil22self.cleanup_mutex = Mutex.new23self.cleaned_up = false24end2526def synchronized_cleanup27# Avoid start and stop both calling cleanup28self.cleanup_mutex.synchronize do29unless cleaned_up30self.cleaned_up = true31self.clean_proc.call(ctx) if self.clean_proc32end33end34end3536#37# Runs the job in the context of its own thread if the async flag is false.38# Otherwise, the job is run inline.39#40def start(async = false)41self.start_time = ::Time.now42if (async)43self.job_thread = Rex::ThreadFactory.spawn("JobID(#{jid})-#{name}", false) {44# Deschedule our thread momentarily45::IO.select(nil, nil, nil, 0.01)4647begin48run_proc.call(ctx)49ensure50synchronized_cleanup51container.remove_job(self)52end53}54else55begin56run_proc.call(ctx)57rescue ::Exception58container.stop_job(jid)59raise $!60end61end62end6364#65# Stops the job if it's currently running and calls its cleanup procedure66#67def stop68if (self.job_thread)69self.job_thread.kill70self.job_thread = nil71end7273synchronized_cleanup74end7576#77# The name of the job.78#79attr_reader :name8081#82# The job identifier as assigned by the job container.83#84attr_reader :jid8586#87# The time at which this job was started.88#89attr_reader :start_time9091#92# Some job context.93#94attr_reader :ctx9596protected9798attr_writer :name #:nodoc:99attr_writer :jid #:nodoc:100attr_accessor :job_thread #:nodoc:101attr_accessor :container #:nodoc:102attr_accessor :run_proc #:nodoc:103attr_accessor :clean_proc #:nodoc:104attr_writer :ctx #:nodoc:105attr_writer :start_time #:nodoc:106attr_accessor :cleanup_mutex #:nodoc:107attr_accessor :cleaned_up #:nodoc:108109end110111end112113114