Class: Ferrum::Worker

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

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::DEEP_SERIALIZATION, Frame::Runtime::INTERMITTENT_ATTEMPTS, Frame::Runtime::INTERMITTENT_SLEEP, Frame::Runtime::SHALLOW_SERIALIZATION, Frame::Runtime::UNSERIALIZABLE_NUMBERS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Interceptable

#off, #on, #subscribe_interception, #subscribed?

Methods included from Frame::Runtime

#evaluate, #evaluate_handle, #evaluate_in, #execute

Constructor Details

#initialize(client, target_id:, url:, type: "worker") ⇒ Worker

Returns a new instance of Worker.

Parameters:

  • client (Client, SessionClient)
  • target_id: (String)
  • url: (String)
  • type: (String) (defaults to: "worker")


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

def initialize(client, target_id:, url:, type: "worker")
  @client = client
  @target_id = target_id
  @url = url
  @type = type
  @options = client.options
  @page = self
  @execution_id = Concurrency::Cell.new
  @network = Network.new(self)
  @console = Console.new(self)

  subscribe
  prepare
end

Instance Attribute Details

#client ⇒ Client, SessionClient (readonly)

Client connection.

Returns:



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

def client
  @client
end

#console ⇒ Console (readonly)

Console messages.

Returns:



45
46
47
# File 'lib/ferrum/worker.rb', line 45

def console
  @console
end

#network ⇒ Network (readonly)

Network object.

Returns:



40
41
42
# File 'lib/ferrum/worker.rb', line 40

def network
  @network
end

#target_id ⇒ String (readonly)

Returns the value of attribute target_id.

Returns:

  • (String)


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

def target_id
  @target_id
end

#type ⇒ String (readonly)

Returns the value of attribute type.

Returns:

  • (String)


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

def type
  @type
end

#url ⇒ String (readonly)

Returns the value of attribute url.

Returns:

  • (String)


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

def url
  @url
end

Instance Method Details

#close ⇒ nil

Closes the worker in the browser. Chrome refuses Target.closeTarget for a dedicated worker, so it closes itself instead.

Returns:

  • (nil)


87
88
89
90
91
92
93
94
95
# File 'lib/ferrum/worker.rb', line 87

def close
  if type == "worker"
    client.notify("Runtime.evaluate", expression: "self.close()")
  else
    client.notify("Target.closeTarget", targetId: target_id)
  end

  nil
end

#command(method, timeout: nil, **params) ⇒ Hash

Sends a CDP command through #client.

Parameters:

  • method (String) —

    The CDP method name.

  • timeout (Numeric, Deadline, nil) (defaults to: nil) —

    How long to wait for the response, the worker's timeout by default.

  • params (Hash) —

    The command's parameters.

  • timeout: (::Numeric, Deadline, nil) (defaults to: nil)

Returns:

  • (Hash) —

    The command's result.

Raises:



69
70
71
# File 'lib/ferrum/worker.rb', line 69

def command(method, timeout: nil, **params)
  client.command(method, timeout: timeout || self.timeout, **params)
end

#inspect ⇒ String

Debug representation of the worker.

Returns:

  • (String)


110
111
112
# File 'lib/ferrum/worker.rb', line 110

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

#main_frame ⇒ nil

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Returns:

  • (nil)


79
80
81
# File 'lib/ferrum/worker.rb', line 79

def main_frame
  nil
end

#release_waiters ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Wakes the threads waiting for the worker's execution context for good, the worker is gone and no context is coming.



103
104
105
# File 'lib/ferrum/worker.rb', line 103

def release_waiters
  @execution_id.close
end

#timeout ⇒ Numeric

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

Returns:

  • (Numeric)


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

def timeout
  @options.timeout
end