class Arachni::BrowserCluster::Job

Represents a job to be passed to the {BrowserCluster#queue} for deferred execution.

@abstract @author Tasos “Zapotek” Laskos <tasos.laskos@arachni-scanner.com>

Attributes

args[RW]
browser[R]

@return [Worker]

Browser to use in order to perform the relevant {#run task} -- set by
{Worker#run_job} via {#configure_and_run}.
forwarder[RW]

@return [Job]

Forwarder [Job] in case `self` is a result of a forward operation.

@see forward @see forward_as

time[RW]

@return [Integer]

Duration of the job, in seconds.

Public Class Methods

new( options = {} ) click to toggle source

@param [Hash] options

# File lib/arachni/browser_cluster/job.rb, line 54
def initialize( options = {} )
    @options      = options.dup
    @options[:id] = @id = options.delete(:id) || increment_id

    @args = @options[:args] || []

    options.each { |k, v| options[k] = send( "#{k}=", v ) }
end

Public Instance Methods

==( other ) click to toggle source
# File lib/arachni/browser_cluster/job.rb, line 185
def ==( other )
    hash == other.hash
end
clean_copy() click to toggle source

@return [Job]

{#dup Copy} of `self` with any resources set by {#configure_and_run}
removed.
# File lib/arachni/browser_cluster/job.rb, line 138
def clean_copy
    dup.tap { |j| j.remove_resources }
end
configure_and_run( browser ) click to toggle source

Configures the job with the given resources, {#run runs} the payload and then removes the assigned resources.

@param [Worker] browser

{#browser Browser} to use in order to perform the relevant task -- set
by {BrowserCluster::Worker#run_job}.
# File lib/arachni/browser_cluster/job.rb, line 107
def configure_and_run( browser )
    set_resources( browser )
    run
ensure
    remove_resources
end
dup() click to toggle source

@return [Job]

Copy of `self`
# File lib/arachni/browser_cluster/job.rb, line 144
def dup
    n = self.class.new( add_id( @options ) )
    n.time = time
    n.timed_out!( time ) if timed_out?
    n
end
forward( options = {} ) click to toggle source

@param [Hash] options

See {#initialize}.

@return [Job]

Re-used request (mainly its {#id} and thus its callback as well),
configured with the given `options`.
# File lib/arachni/browser_cluster/job.rb, line 157
def forward( options = {} )
    self.class.new forward_options( options )
end
forward_as( job_type, options = {} ) click to toggle source

@param [Job] job_type

Job class under {Jobs}.

@param [Hash] options

Initialization options for `job_type`.

@return [Job]

Forwarded request (preserving its {#id} and thus its callback as well),
configured with the given `options`.
# File lib/arachni/browser_cluster/job.rb, line 169
def forward_as( job_type, options = {} )
    # Remove the ID because this will be a different class/job type and
    # we thus need to keep track of it separately in the BrowserCluster.
    job_type.new forward_options( options ).tap { |h| h.delete :id }
end
hash() click to toggle source
# File lib/arachni/browser_cluster/job.rb, line 181
def hash
    @options.hash
end
id() click to toggle source

@return [Integer]

ID, used by the {BrowserCluster}, to tie requests to callbacks.
# File lib/arachni/browser_cluster/job.rb, line 177
def id
    @id
end
never_ending=( bool ) click to toggle source

@return [Bool]

`true` if this job never ends, `false` otherwise.
# File lib/arachni/browser_cluster/job.rb, line 96
def never_ending=( bool )
    @options[:never_ending] = bool
    @never_ending = bool
end
never_ending?() click to toggle source

@return [Bool]

`true` if this job never ends, `false` otherwise.

@see never_ending

# File lib/arachni/browser_cluster/job.rb, line 90
def never_ending?
    !!@never_ending
end
run() click to toggle source

@note The following resources will be available at the time of execution:

* {#browser}

Encapsulates the job payload.

@abstract

# File lib/arachni/browser_cluster/job.rb, line 83
def run
end
save_result( data ) click to toggle source

Forwards the {Result resulting} ‘data` to the {BrowserCluster#handle_job_result browser cluster} which then forwards it to the entity that {BrowserCluster#queue queued} the job.

The result type will be the closest {Result} class to the {Job} type. If the job is of type ‘MyJob`, `MyJob::Result` will be used, the default if {Result}.

@param [Hash] data

Used to initialize the {Result}.
# File lib/arachni/browser_cluster/job.rb, line 124
def save_result( data )
    # Results coming in after the job has already finished won't have a
    # browser.
    return if !browser

    browser.master.handle_job_result(
        self.class::Result.new( data.merge( job: self.clean_copy ) )
    )
    nil
end
timed_out!( time ) click to toggle source

@param [Integer] time

Amount of {#time} elapsed until time-out.
# File lib/arachni/browser_cluster/job.rb, line 65
def timed_out!( time )
    @timed_out = true
    @time = time
end
timed_out?() click to toggle source

@return [Bool]

`true` if timed-ot, `false` otherwise.
# File lib/arachni/browser_cluster/job.rb, line 72
def timed_out?
    !!@timed_out
end

Protected Instance Methods

remove_resources() click to toggle source
# File lib/arachni/browser_cluster/job.rb, line 191
def remove_resources
    @browser = nil
end

Private Instance Methods

add_id( options ) click to toggle source
# File lib/arachni/browser_cluster/job.rb, line 205
def add_id( options )
    options.merge( id: @id )
end
forward_options( options ) click to toggle source
# File lib/arachni/browser_cluster/job.rb, line 197
def forward_options( options )
    add_id( options ).merge(
        args:         args,
        never_ending: never_ending?,
        forwarder:    self.clean_copy
    )
end
increment_id() click to toggle source

Increments the {#id} upon {#initialize initialization}.

@return [Integer]

# File lib/arachni/browser_cluster/job.rb, line 216
def increment_id
    @@id ||= 0
    @@id += 1
end
set_resources( browser ) click to toggle source
# File lib/arachni/browser_cluster/job.rb, line 209
def set_resources( browser )
    @browser = browser
end