Class: Ferrum::Worker

Inherits:
Object
  • Object
show all
Includes:
Frame::Runtime, Interceptable
Defined in:
lib/ferrum/worker.rb

Overview

A dedicated or shared Worker spawned by a page. Unlike Page it has no DOM, frames, mouse/keyboard, or navigation history -- just a single global execution context and its own network activity.

Constant Summary

Constants included from Frame::Runtime

Frame::Runtime::INTERMITTENT_ATTEMPTS, Frame::Runtime::INTERMITTENT_SLEEP

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Interceptable

#off, #on, #subscribed?

Methods included from Frame::Runtime

#evaluate, #evaluate_async, #evaluate_func, #evaluate_on, #execute

Constructor Details

#initialize(client, target_id:, url:) ⇒ Worker

Returns a new instance of Worker.



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ferrum/worker.rb', line 21

def initialize(client, target_id:, url:)
  @client = client
  @target_id = target_id
  @url = url
  @options = client.options
  @page = self
  @execution_id = Concurrent::MVar.new
  @network = Network.new(self)

  subscribe
  prepare
end

Instance Attribute Details

#clientClient, SessionClient (readonly)

Client connection.

Returns:



17
18
19
# File 'lib/ferrum/worker.rb', line 17

def client
  @client
end

#networkNetwork (readonly)

Network object.

Returns:



37
38
39
# File 'lib/ferrum/worker.rb', line 37

def network
  @network
end

#target_idObject (readonly)

Returns the value of attribute target_id.



19
20
21
# File 'lib/ferrum/worker.rb', line 19

def target_id
  @target_id
end

#urlObject (readonly)

Returns the value of attribute url.



19
20
21
# File 'lib/ferrum/worker.rb', line 19

def url
  @url
end

Instance Method Details

#closeBoolean

Closes the target in the browser, and its connection.

Returns:

  • (Boolean)


63
64
65
66
67
68
# File 'lib/ferrum/worker.rb', line 63

def close
  client.command("Target.closeTarget", async: true, targetId: target_id)
  close_connection

  true
end

#close_connectionvoid

This method returns an undefined value.

Closes the WebSocket connection only, without asking the browser to close the underlying target. Kept separate from #close so a whole context can be dropped (the browser closes its targets anyway) without every worker having to be closed one by one.



76
77
78
# File 'lib/ferrum/worker.rb', line 76

def close_connection
  client&.close
end

#commandBoolean, Hash

Sends a CDP command through #client.

Returns:

  • (Boolean, Hash)

    true when sent asynchronously, otherwise the command's result.



50
51
52
# File 'lib/ferrum/worker.rb', line 50

def command(...)
  client.command(...)
end

#inspectString

Debug representation of the worker.

Returns:

  • (String)


83
84
85
# File 'lib/ferrum/worker.rb', line 83

def inspect
  "#<#{self.class} @target_id=#{@target_id.inspect} @url=#{@url.inspect}>"
end

#main_frameObject

Workers have a single execution context and no navigable document, so there's nothing for Network to check requests against.



56
57
58
# File 'lib/ferrum/worker.rb', line 56

def main_frame
  nil
end

#timeoutNumeric

How long to wait for CDP responses and JS evaluation to complete.

Returns:

  • (Numeric)


42
43
44
# File 'lib/ferrum/worker.rb', line 42

def timeout
  @options.timeout
end