Class: Celluloid::Actor::System

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/celluloid/actor/system.rb

Constant Summary collapse

ROOT_SERVICES =
begin
  root_services = [
    {
      as: :notifications_fanout,
      type: Celluloid::Notifications::Fanout,
    },
    {
      as: :incident_reporter,
      type: Celluloid::IncidentReporter,
      args: [STDERR],
    },
    {
      as: :public_services,
      type: Celluloid::Supervision::Service::Public,
      accessors: [:services],
      supervise: [],
    },
  ]
  if $CELLULOID_MANAGED
    root_services << {
      as: :actor_manager,
      type: Celluloid::Actor::Manager,
      accessors: [:manager],
    }
  end
  root_services
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSystem

Returns a new instance of System.


50
51
52
53
54
55
# File 'lib/celluloid/actor/system.rb', line 50

def initialize
  @tree = nil
  @group = Celluloid.group_class.new
  @registry = Internals::Registry.new
  @root = ROOT_SERVICES
end

Instance Attribute Details

#groupObject (readonly)

Returns the value of attribute group


38
39
40
# File 'lib/celluloid/actor/system.rb', line 38

def group
  @group
end

#registryObject (readonly)

Returns the value of attribute registry


38
39
40
# File 'lib/celluloid/actor/system.rb', line 38

def registry
  @registry
end

Instance Method Details

#assert_inactiveObject


154
155
156
# File 'lib/celluloid/actor/system.rb', line 154

def assert_inactive
  @group.assert_inactive
end

#clear_registryObject


94
95
96
# File 'lib/celluloid/actor/system.rb', line 94

def clear_registry
  @registry.clear
end

#get_threadObject


75
76
77
78
79
80
# File 'lib/celluloid/actor/system.rb', line 75

def get_thread
  @group.get do
    Thread.current[:celluloid_actor_system] = self
    yield
  end
end

#registeredObject


90
91
92
# File 'lib/celluloid/actor/system.rb', line 90

def registered
  @registry.names
end

#root_configurationObject


46
47
48
# File 'lib/celluloid/actor/system.rb', line 46

def root_configuration
  @root
end

#root_servicesObject

the root of the supervisor tree is established at supervision/root


42
43
44
# File 'lib/celluloid/actor/system.rb', line 42

def root_services
  @tree
end

#runningObject


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/celluloid/actor/system.rb', line 98

def running
  actors = []
  @group.each do |t|
    next unless t.role == :actor
    actor = t.actor

    # NOTE - these are in separate statements, since on JRuby t.actor may
    # become nil befor .behavior_proxy() is called
    next unless actor
    next unless actor.respond_to?(:behavior_proxy)
    proxy = actor.behavior_proxy
    actors << proxy
  end
  actors
end

#running?Boolean

Returns:

  • (Boolean)

114
115
116
# File 'lib/celluloid/actor/system.rb', line 114

def running?
  @group.active?
end

#shutdownObject

Shut down all running actors


119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/celluloid/actor/system.rb', line 119

def shutdown
  actors = running
  Timeout.timeout(shutdown_timeout) do
    Internals::Logger.debug "Terminating #{actors.size} #{(actors.size > 1) ? 'actors' : 'actor'}..." if actors.size > 0

    # Actors cannot self-terminate, you must do it for them
    actors.each do |actor|
      begin
        actor.terminate!
      rescue DeadActorError
      end
    end

    actors.each do |actor|
      begin
        Actor.join(actor)
      rescue DeadActorError
      end
    end
  end
rescue Timeout::Error
  Internals::Logger.error("Couldn't cleanly terminate all actors in #{shutdown_timeout} seconds!")
  unless RUBY_PLATFORM == "java" || RUBY_ENGINE == "rbx"
    actors.each do |actor|
      begin
        Actor.kill(actor)
      rescue DeadActorError, MailboxDead
      end
    end
  end
ensure
  @group.shutdown
  clear_registry
end

#shutdown_timeoutObject


158
159
160
# File 'lib/celluloid/actor/system.rb', line 158

def shutdown_timeout
  Celluloid.shutdown_timeout
end

#stack_dumpObject


82
83
84
# File 'lib/celluloid/actor/system.rb', line 82

def stack_dump
  Internals::Stack::Dump.new(@group)
end

#stack_summaryObject


86
87
88
# File 'lib/celluloid/actor/system.rb', line 86

def stack_summary
  Internals::Stack::Summary.new(@group)
end

#startObject

Launch default services


58
59
60
61
62
63
64
65
# File 'lib/celluloid/actor/system.rb', line 58

def start
  within do
    @root = Supervision::Service::Root.define
    @tree = root_configuration.deploy
    # de root_services[:group_manager].manage! @group
  end
  true
end

#withinObject


67
68
69
70
71
72
73
# File 'lib/celluloid/actor/system.rb', line 67

def within
  old = Thread.current[:celluloid_actor_system]
  Thread.current[:celluloid_actor_system] = self
  yield
ensure
  Thread.current[:celluloid_actor_system] = old
end