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_container.rb
Views: 11766
# -*- coding: binary -*-1module Rex23###4#5# This class contains zero or more abstract jobs that can be enumerated and6# stopped in a generic fashion. This is used to provide a mechanism for7# keeping track of arbitrary contexts that may or may not require a dedicated8# thread.9#10###11class JobContainer < Hash1213def initialize14self.job_id_pool = 015end1617#18# Adds an already running task as a symbolic job to the container.19#20def add_job(name, ctx, run_proc, clean_proc)21real_name = name22count = 023jid = job_id_pool2425self.job_id_pool += 12627# If we were not supplied with a job name, pick one from the hat28if (real_name == nil)29real_name = '#' + jid.to_s30end3132# Find a unique job name33while (j = self[real_name])34real_name = name + " #{count}"35count += 136end3738j = Job.new(self, jid, real_name, ctx, run_proc, clean_proc)3940self[jid.to_s] = j41end4243#44# Starts a job using the supplied name and run/clean procedures.45#46def start_job(name, ctx, run_proc, clean_proc = nil)47j = add_job(name, ctx, run_proc, clean_proc)48j.start4950j.jid51end5253#54# Starts a background job that doesn't call the cleanup routine or run55# the run_proc in its own thread. Rather, the run_proc is called56# immediately and the clean_proc is never called until the job is removed57# from the job container.58#59def start_bg_job(name, ctx, run_proc, clean_proc = nil, async = true)60j = add_job(name, ctx, run_proc, clean_proc)61j.start(async)6263j.jid64end6566#67# Stops the job with the supplied name and forces it to cleanup. Stopping68# the job also leads to its removal.69#70def stop_job(jid)71if (j = self[jid.to_s])72j.stop7374remove_job(j)75end76end7778#79# Removes a job that was previously running. This is typically called when80# a job completes its task.81#82def remove_job(inst)83self.delete(inst.jid.to_s)84end8586#87# Overrides the builtin 'each' operator to avoid the following exception on Ruby 1.9.2+88# "can't add a new key into hash during iteration"89#90def each(&block)91list = []92self.keys.sort.each do |sidx|93list << [sidx, self[sidx]]94end95list.each(&block)96end9798protected99100attr_accessor :job_id_pool # :nodoc:101102end103104end105106107108