Class: Arachni::BrowserCluster::Job Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/arachni/browser_cluster/job.rb,
lib/arachni/browser_cluster/job/result.rb

Overview

This class is abstract.

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

Author:

Defined Under Namespace

Classes: Error, Result

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Job

Returns a new instance of Job.

Parameters:

  • options (Hash) (defaults to: {})


54
55
56
57
58
59
60
61
# 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

Instance Attribute Details

#argsObject

Returns the value of attribute args.



51
52
53
# File 'lib/arachni/browser_cluster/job.rb', line 51

def args
  @args
end

#browserWorker (readonly)

Returns Browser to use in order to perform the relevant task -- set by Worker#run_job via #configure_and_run.

Returns:



38
39
40
# File 'lib/arachni/browser_cluster/job.rb', line 38

def browser
  @browser
end

#forwarderJob

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

Returns:

  • (Job)

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

See Also:



45
46
47
# File 'lib/arachni/browser_cluster/job.rb', line 45

def forwarder
  @forwarder
end

#timeInteger

Returns Duration of the job, in seconds.

Returns:

  • (Integer)

    Duration of the job, in seconds.



49
50
51
# File 'lib/arachni/browser_cluster/job.rb', line 49

def time
  @time
end

Instance Method Details

#==(other) ⇒ Object



185
186
187
# File 'lib/arachni/browser_cluster/job.rb', line 185

def ==( other )
    hash == other.hash
end

#clean_copyJob

Returns Copy of self with any resources set by #configure_and_run removed.

Returns:



138
139
140
# File 'lib/arachni/browser_cluster/job.rb', line 138

def clean_copy
    dup.tap { |j| j.remove_resources }
end

#configure_and_run(browser) ⇒ Object

Configures the job with the given resources, runs the payload and then removes the assigned resources.

Parameters:



107
108
109
110
111
112
# File 'lib/arachni/browser_cluster/job.rb', line 107

def configure_and_run( browser )
    set_resources( browser )
    run
ensure
    remove_resources
end

#dupJob

Returns Copy of self.

Returns:

  • (Job)

    Copy of self



144
145
146
147
148
149
# 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 = {}) ⇒ Job

Returns Re-used request (mainly its #id and thus its callback as well), configured with the given options.

Parameters:

Returns:

  • (Job)

    Re-used request (mainly its #id and thus its callback as well), configured with the given options.



157
158
159
# File 'lib/arachni/browser_cluster/job.rb', line 157

def forward( options = {} )
    self.class.new forward_options( options )
end

#forward_as(job_type, options = {}) ⇒ Job

Returns Forwarded request (preserving its #id and thus its callback as well), configured with the given options.

Parameters:

Returns:

  • (Job)

    Forwarded request (preserving its #id and thus its callback as well), configured with the given options.



169
170
171
172
173
# 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

#hashObject



181
182
183
# File 'lib/arachni/browser_cluster/job.rb', line 181

def hash
    @options.hash
end

#idInteger

Returns ID, used by the Arachni::BrowserCluster, to tie requests to callbacks.

Returns:



177
178
179
# File 'lib/arachni/browser_cluster/job.rb', line 177

def id
    @id
end

#never_ending=(bool) ⇒ Bool

Returns true if this job never ends, false otherwise.

Returns:

  • (Bool)

    true if this job never ends, false otherwise.



96
97
98
99
# File 'lib/arachni/browser_cluster/job.rb', line 96

def never_ending=( bool )
    @options[:never_ending] = bool
    @never_ending = bool
end

#never_ending?Bool

Returns true if this job never ends, false otherwise.

Returns:

  • (Bool)

    true if this job never ends, false otherwise.

See Also:

  • #never_ending


90
91
92
# File 'lib/arachni/browser_cluster/job.rb', line 90

def never_ending?
    !!@never_ending
end

#runObject

This method is abstract.
Note:

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

Encapsulates the job payload.



83
84
# File 'lib/arachni/browser_cluster/job.rb', line 83

def run
end

#save_result(data) ⇒ Object

Forwards the resulting data to the browser cluster which then forwards it to the entity that queued the job.

The result type will be the closest Result class to the Arachni::BrowserCluster::Job type. If the job is of type MyJob, MyJob::Result will be used, the default if Result.

Parameters:



124
125
126
127
128
129
130
131
132
133
# 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) ⇒ Object

Parameters:

  • time (Integer)

    Amount of #time elapsed until time-out.



65
66
67
68
# File 'lib/arachni/browser_cluster/job.rb', line 65

def timed_out!( time )
    @timed_out = true
    @time = time
end

#timed_out?Bool

Returns true if timed-ot, false otherwise.

Returns:

  • (Bool)

    true if timed-ot, false otherwise.



72
73
74
# File 'lib/arachni/browser_cluster/job.rb', line 72

def timed_out?
    !!@timed_out
end