Class: ActionCable::Server::Worker

Inherits:
Object
  • Object
show all
Includes:
ActiveRecordConnectionManagement, ActiveSupport::Callbacks
Defined in:
actioncable/lib/action_cable/server/worker.rb,
actioncable/lib/action_cable/server/worker/active_record_connection_management.rb

Overview

Воркеры используют Server.send_async, чтобы выполнять работу по соединению в тредах.

Defined Under Namespace

Modules: ActiveRecordConnectionManagement

Constant Summary

Constants included from ActiveSupport::Callbacks

ActiveSupport::Callbacks::CALLBACK_FILTER_TYPES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ActiveRecordConnectionManagement

#with_database_connections

Methods included from ActiveSupport::Concern

#append_features, #class_methods, extended, #included

Methods included from ActiveSupport::Callbacks

#run_callbacks

Constructor Details

#initialize(max_size: 5) ⇒ Worker

Returns a new instance of Worker.


17
18
19
20
21
22
23
# File 'actioncable/lib/action_cable/server/worker.rb', line 17

def initialize(max_size: 5)
  @executor = Concurrent::ThreadPoolExecutor.new(
    min_threads: 1,
    max_threads: max_size,
    max_queue: 0,
  )
end

Instance Attribute Details

#executorObject (readonly)

Returns the value of attribute executor


15
16
17
# File 'actioncable/lib/action_cable/server/worker.rb', line 15

def executor
  @executor
end

Instance Method Details

#async_exec(receiver, *args, connection:, &block) ⇒ Object


44
45
46
# File 'actioncable/lib/action_cable/server/worker.rb', line 44

def async_exec(receiver, *args, connection:, &block)
  async_invoke receiver, :instance_exec, *args, connection: connection, &block
end

#async_invoke(receiver, method, *args, connection: receiver, &block) ⇒ Object


48
49
50
51
52
# File 'actioncable/lib/action_cable/server/worker.rb', line 48

def async_invoke(receiver, method, *args, connection: receiver, &block)
  @executor.post do
    invoke(receiver, method, *args, connection: connection, &block)
  end
end

#haltObject

Прекратить обработку ворка: любой ворк, который еще не запущен, будет удален из очереди


26
27
28
# File 'actioncable/lib/action_cable/server/worker.rb', line 26

def halt
  @executor.shutdown
end

#invoke(receiver, method, *args, connection:, &block) ⇒ Object


54
55
56
57
58
59
60
61
62
63
64
65
# File 'actioncable/lib/action_cable/server/worker.rb', line 54

def invoke(receiver, method, *args, connection:, &block)
  work(connection) do
    begin
      receiver.send method, *args, &block
    rescue Exception => e
      logger.error "There was an exception - #{e.class}(#{e.message})"
      logger.error e.backtrace.join("\n")

      receiver.handle_exception if receiver.respond_to?(:handle_exception)
    end
  end
end

#stopping?Boolean

Returns:

  • (Boolean)

30
31
32
# File 'actioncable/lib/action_cable/server/worker.rb', line 30

def stopping?
  @executor.shuttingdown?
end

#work(connection) ⇒ Object


34
35
36
37
38
39
40
41
42
# File 'actioncable/lib/action_cable/server/worker.rb', line 34

def work(connection)
  self.connection = connection

  run_callbacks :work do
    yield
  end
ensure
  self.connection = nil
end