celluloid 0.17.4 → 0.18.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (171) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGES.md +300 -81
  3. data/CONDUCT.md +13 -0
  4. data/CONTRIBUTING.md +39 -0
  5. data/README.md +54 -155
  6. data/REFACTOR.md +1 -0
  7. data/architecture.md +120 -0
  8. data/examples/basic_usage.rb +1 -1
  9. data/examples/configurations.rb +78 -0
  10. data/examples/futures.rb +1 -1
  11. data/examples/ring.rb +5 -4
  12. data/examples/simple_pmap.rb +1 -1
  13. data/examples/stack.rb +2 -2
  14. data/examples/supervisors_and_registry.rb +82 -0
  15. data/examples/timers.rb +2 -2
  16. data/lib/celluloid/actor/system.rb +13 -29
  17. data/lib/celluloid/actor.rb +27 -17
  18. data/lib/celluloid/autostart.rb +6 -1
  19. data/lib/celluloid/call/async.rb +2 -0
  20. data/lib/celluloid/call/sync.rb +10 -3
  21. data/lib/celluloid/calls.rb +13 -12
  22. data/lib/celluloid/cell.rb +5 -9
  23. data/lib/celluloid/condition.rb +3 -3
  24. data/lib/celluloid/core_ext.rb +0 -2
  25. data/lib/celluloid/debug.rb +3 -0
  26. data/lib/celluloid/exceptions.rb +2 -2
  27. data/lib/celluloid/future.rb +8 -10
  28. data/lib/celluloid/group/pool.rb +1 -3
  29. data/lib/celluloid/group/spawner.rb +2 -6
  30. data/lib/celluloid/group.rb +12 -8
  31. data/lib/celluloid/internals/call_chain.rb +15 -0
  32. data/lib/celluloid/internals/cpu_counter.rb +62 -0
  33. data/lib/celluloid/internals/handlers.rb +42 -0
  34. data/lib/celluloid/internals/links.rb +38 -0
  35. data/lib/celluloid/internals/logger.rb +104 -0
  36. data/lib/celluloid/internals/method.rb +34 -0
  37. data/lib/celluloid/internals/properties.rb +32 -0
  38. data/lib/celluloid/internals/receivers.rb +64 -0
  39. data/lib/celluloid/internals/registry.rb +102 -0
  40. data/lib/celluloid/internals/responses.rb +46 -0
  41. data/lib/celluloid/internals/signals.rb +24 -0
  42. data/lib/celluloid/internals/stack/dump.rb +12 -0
  43. data/lib/celluloid/internals/stack/states.rb +72 -0
  44. data/lib/celluloid/internals/stack/summary.rb +12 -0
  45. data/lib/celluloid/internals/stack.rb +74 -0
  46. data/lib/celluloid/internals/task_set.rb +51 -0
  47. data/lib/celluloid/internals/thread_handle.rb +52 -0
  48. data/lib/celluloid/internals/uuid.rb +40 -0
  49. data/lib/celluloid/logging/incident.rb +21 -0
  50. data/lib/celluloid/logging/incident_logger.rb +147 -0
  51. data/lib/celluloid/logging/incident_reporter.rb +49 -0
  52. data/lib/celluloid/logging/log_event.rb +20 -0
  53. data/lib/celluloid/logging/ring_buffer.rb +64 -0
  54. data/lib/celluloid/mailbox/evented.rb +13 -5
  55. data/lib/celluloid/mailbox.rb +22 -9
  56. data/lib/celluloid/notifications.rb +95 -0
  57. data/lib/celluloid/pool.rb +6 -0
  58. data/lib/celluloid/probe.rb +81 -0
  59. data/lib/celluloid/proxy/abstract.rb +9 -9
  60. data/lib/celluloid/proxy/async.rb +1 -1
  61. data/lib/celluloid/proxy/block.rb +2 -2
  62. data/lib/celluloid/proxy/cell.rb +1 -1
  63. data/lib/celluloid/proxy/future.rb +2 -4
  64. data/lib/celluloid/proxy/sync.rb +1 -3
  65. data/lib/celluloid/rspec.rb +22 -33
  66. data/lib/celluloid/supervision/configuration/injections.rb +8 -0
  67. data/lib/celluloid/supervision/configuration/instance.rb +113 -0
  68. data/lib/celluloid/supervision/configuration.rb +169 -0
  69. data/lib/celluloid/supervision/constants.rb +123 -0
  70. data/lib/celluloid/supervision/container/behavior/pool.rb +71 -0
  71. data/lib/celluloid/supervision/container/behavior/tree.rb +23 -0
  72. data/lib/celluloid/supervision/container/behavior.rb +89 -0
  73. data/lib/celluloid/supervision/container/injections.rb +8 -0
  74. data/lib/celluloid/supervision/container/instance.rb +116 -0
  75. data/lib/celluloid/supervision/container/pool.rb +210 -0
  76. data/lib/celluloid/supervision/container.rb +144 -0
  77. data/lib/celluloid/supervision/service.rb +27 -0
  78. data/lib/celluloid/supervision/supervise.rb +34 -0
  79. data/lib/celluloid/supervision/validation.rb +40 -0
  80. data/lib/celluloid/supervision/version.rb +5 -0
  81. data/lib/celluloid/supervision.rb +17 -0
  82. data/lib/celluloid/system_events.rb +11 -6
  83. data/lib/celluloid/task/fibered.rb +6 -2
  84. data/lib/celluloid/task/threaded.rb +3 -3
  85. data/lib/celluloid/task.rb +25 -12
  86. data/lib/celluloid/test.rb +5 -2
  87. data/lib/celluloid/thread.rb +0 -2
  88. data/lib/celluloid/version.rb +1 -1
  89. data/lib/celluloid.rb +74 -64
  90. data/spec/celluloid/block_spec.rb +29 -32
  91. data/spec/celluloid/calls_spec.rb +5 -15
  92. data/spec/celluloid/future_spec.rb +7 -1
  93. data/spec/celluloid/internals/cpu_counter_spec.rb +129 -0
  94. data/spec/celluloid/internals/links_spec.rb +43 -0
  95. data/spec/celluloid/internals/properties_spec.rb +40 -0
  96. data/spec/celluloid/internals/registry_spec.rb +62 -0
  97. data/spec/celluloid/internals/stack/dump_spec.rb +4 -0
  98. data/spec/celluloid/internals/stack/summary_spec.rb +4 -0
  99. data/spec/celluloid/internals/thread_handle_spec.rb +60 -0
  100. data/spec/celluloid/internals/uuid_spec.rb +9 -0
  101. data/spec/celluloid/logging/ring_buffer_spec.rb +36 -0
  102. data/spec/celluloid/mailbox/evented_spec.rb +11 -22
  103. data/spec/celluloid/misc/leak_spec.rb +3 -4
  104. data/spec/celluloid/notifications_spec.rb +140 -0
  105. data/spec/celluloid/probe_spec.rb +102 -0
  106. data/spec/celluloid/proxy_spec.rb +30 -30
  107. data/spec/celluloid/supervision/behavior_spec.rb +74 -0
  108. data/spec/celluloid/supervision/configuration_spec.rb +181 -0
  109. data/spec/celluloid/supervision/container_spec.rb +72 -0
  110. data/spec/celluloid/supervision/instance_spec.rb +13 -0
  111. data/spec/celluloid/supervision/root_spec.rb +28 -0
  112. data/spec/celluloid/supervision/supervisor_spec.rb +93 -0
  113. data/spec/celluloid/task/fibered_spec.rb +1 -3
  114. data/spec/celluloid/task/threaded_spec.rb +1 -3
  115. data/spec/shared/actor_examples.rb +58 -33
  116. data/spec/shared/group_examples.rb +2 -2
  117. data/spec/shared/mailbox_examples.rb +1 -1
  118. data/spec/shared/stack_examples.rb +87 -0
  119. data/spec/shared/task_examples.rb +2 -3
  120. data/spec/spec_helper.rb +2 -4
  121. data/spec/support/configure_rspec.rb +2 -3
  122. data/spec/support/coverage.rb +2 -4
  123. data/spec/support/crash_checking.rb +2 -2
  124. data/spec/support/examples/actor_class.rb +3 -8
  125. data/spec/support/examples/call_class.rb +2 -2
  126. data/spec/support/examples/container_class.rb +35 -0
  127. data/spec/support/examples/evented_mailbox_class.rb +1 -2
  128. data/spec/support/examples/stack_classes.rb +58 -0
  129. data/spec/support/examples/stack_methods.rb +23 -0
  130. data/spec/support/examples/subordinate_class.rb +19 -0
  131. data/spec/support/logging.rb +3 -34
  132. data/spec/support/loose_threads.rb +3 -16
  133. data/spec/support/reset_class_variables.rb +5 -1
  134. data/spec/support/stubbing.rb +1 -1
  135. metadata +91 -289
  136. data/culture/CONDUCT.md +0 -28
  137. data/culture/Gemfile +0 -9
  138. data/culture/LICENSE.txt +0 -22
  139. data/culture/README.md +0 -22
  140. data/culture/Rakefile +0 -5
  141. data/culture/SYNC.md +0 -70
  142. data/culture/celluloid-culture.gemspec +0 -18
  143. data/culture/gems/README.md +0 -39
  144. data/culture/gems/dependencies.yml +0 -85
  145. data/culture/gems/loader.rb +0 -101
  146. data/culture/rubocop/README.md +0 -38
  147. data/culture/rubocop/lint.yml +0 -8
  148. data/culture/rubocop/metrics.yml +0 -15
  149. data/culture/rubocop/perf.yml +0 -0
  150. data/culture/rubocop/rubocop.yml +0 -5
  151. data/culture/rubocop/style.yml +0 -57
  152. data/culture/spec/gems_spec.rb +0 -2
  153. data/culture/spec/spec_helper.rb +0 -0
  154. data/culture/spec/sync_spec.rb +0 -2
  155. data/culture/sync.rb +0 -56
  156. data/culture/tasks/rspec.rake +0 -5
  157. data/culture/tasks/rubocop.rake +0 -2
  158. data/lib/celluloid/actor/manager.rb +0 -7
  159. data/lib/celluloid/backported.rb +0 -2
  160. data/lib/celluloid/current.rb +0 -2
  161. data/lib/celluloid/deprecate.rb +0 -21
  162. data/lib/celluloid/fiber.rb +0 -32
  163. data/lib/celluloid/managed.rb +0 -3
  164. data/lib/celluloid/notices.rb +0 -15
  165. data/spec/deprecate/actor_system_spec.rb +0 -72
  166. data/spec/deprecate/block_spec.rb +0 -52
  167. data/spec/deprecate/calls_spec.rb +0 -39
  168. data/spec/deprecate/evented_mailbox_spec.rb +0 -34
  169. data/spec/deprecate/future_spec.rb +0 -32
  170. data/spec/deprecate/internal_pool_spec.rb +0 -4
  171. data/spec/support/env.rb +0 -21
@@ -0,0 +1,34 @@
1
+ # collect together all instances of the `supervise` method
2
+ module Celluloid
3
+ class << self
4
+ def supervise(config = {}, &block)
5
+ supervisor = Supervision.router(config)
6
+ supervisor.supervise(config, &block)
7
+ end
8
+ end
9
+ module ClassMethods
10
+ def supervise(config = {}, &block)
11
+ Celluloid.supervise(config.merge(type: self), &block)
12
+ end
13
+ end
14
+ module Supervision
15
+ class << self
16
+ def router(_config = {})
17
+ # TODO: Actually route.
18
+ Celluloid.services # for now, hardcode .services
19
+ end
20
+ end
21
+ class Container
22
+ class << self
23
+ def supervise(config, &block)
24
+ blocks << lambda do |container|
25
+ container.add(config, &block)
26
+ end
27
+ end
28
+ end
29
+ def supervise(config, &block)
30
+ add(Configuration.options(config, block: block))
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,40 @@
1
+ module Celluloid
2
+ module Supervision
3
+ class Configuration
4
+ class << self
5
+ def valid?(configuration, fails = false)
6
+ parameters(:mandatory).each do |k|
7
+ unless configuration.key? k
8
+ if fails
9
+ raise Error::Incomplete, "Missing `:#{k}` in supervision configuration."
10
+ else
11
+ return false
12
+ end
13
+ end
14
+ end
15
+ arity.each do |klass, args|
16
+ next if configuration[args].is_a? Proc
17
+ __a = configuration[args] && configuration[args].count || 0
18
+ __arity = configuration[klass].allocate.method(:initialize).arity
19
+ unless (__arity < 0 && __a >= __arity.abs - 1) || __a == __arity.abs
20
+ if fails
21
+ raise ArgumentError, "#{__a} vs. #{__arity}"
22
+ else
23
+ return false
24
+ end
25
+ end
26
+ end
27
+ true
28
+ end
29
+
30
+ def options(config = {}, options = {})
31
+ configuration = config.merge(options)
32
+ return configuration if configuration.is_a? Configuration
33
+ configuration[:configuration] = Container::Behavior.configure(configuration)
34
+ valid?(configuration, true)
35
+ configuration
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,5 @@
1
+ module Celluloid
2
+ module Supervision
3
+ VERSION = "0.20.6".freeze
4
+ end
5
+ end
@@ -0,0 +1,17 @@
1
+ require "celluloid" unless defined? Celluloid
2
+
3
+ require "celluloid/supervision/constants"
4
+ require "celluloid/supervision/supervise"
5
+
6
+ require "celluloid/supervision/container"
7
+ require "celluloid/supervision/container/instance"
8
+ require "celluloid/supervision/container/behavior"
9
+ require "celluloid/supervision/container/injections"
10
+
11
+ require "celluloid/supervision/container/behavior/tree"
12
+
13
+ require "celluloid/supervision/validation"
14
+ require "celluloid/supervision/configuration"
15
+ require "celluloid/supervision/configuration/instance"
16
+
17
+ require "celluloid/supervision/service"
@@ -5,7 +5,10 @@ module Celluloid
5
5
  if handler = SystemEvent.handle(event.class)
6
6
  send(handler, event)
7
7
  else
8
+ # !!! DO NOT INTRODUCE ADDITIONAL GLOBAL VARIABLES !!!
9
+ # rubocop:disable Style/GlobalVars
8
10
  Internals::Logger.debug "Discarded message (unhandled): #{message}" if $CELLULOID_DEBUG
11
+ # rubocop:enable Style/GlobalVars
9
12
  end
10
13
  end
11
14
  end
@@ -18,7 +21,7 @@ module Celluloid
18
21
  end
19
22
 
20
23
  def handler(&block)
21
- fail ArgumentError, "SystemEvent handlers must be defined with a block." unless block
24
+ raise ArgumentError, "SystemEvent handlers must be defined with a block." unless block
22
25
  method = begin
23
26
  handler = name
24
27
  .split("::").last
@@ -38,7 +41,7 @@ module Celluloid
38
41
  def initialize(actor, type)
39
42
  @actor = actor
40
43
  @type = type.to_sym
41
- fail ArgumentError, "type must be link or unlink" unless [:link, :unlink].include?(@type)
44
+ raise ArgumentError, "type must be link or unlink" unless %i[link unlink].include?(@type)
42
45
  end
43
46
  end
44
47
  end
@@ -87,7 +90,11 @@ module Celluloid
87
90
 
88
91
  handler do |event|
89
92
  @name = event.name
93
+
94
+ # !!! DO NOT INTRODUCE ADDITIONAL GLOBAL VARIABLES !!!
95
+ # rubocop:disable Style/GlobalVars
90
96
  Celluloid::Probe.actor_named(self) if $CELLULOID_MONITORING
97
+ # rubocop:enable Style/GlobalVars
91
98
  end
92
99
 
93
100
  def initialize(name)
@@ -97,7 +104,7 @@ module Celluloid
97
104
 
98
105
  # Request for an actor to terminate
99
106
  class TerminationRequest < SystemEvent
100
- handler do |event|
107
+ handler do |_event|
101
108
  terminate
102
109
  end
103
110
  end
@@ -110,9 +117,7 @@ module Celluloid
110
117
  end
111
118
  attr_reader :task, :value
112
119
 
113
- handler do |event|
114
- event.call
115
- end
120
+ handler(&:call)
116
121
 
117
122
  def call
118
123
  @task.resume(@value)
@@ -12,8 +12,10 @@ module Celluloid
12
12
  Thread.current[:celluloid_queue] = queue
13
13
  Thread.current[:celluloid_actor_system] = actor_system
14
14
  yield
15
- # TODO: Determine why infinite thread leakage happens under jRuby, if `Fiber.yield` is used:
16
- Fiber.yield unless RUBY_PLATFORM == "java"
15
+
16
+ # Alleged workaround for a MRI memory leak
17
+ # TODO: validate/confirm this is actually necessary
18
+ Fiber.yield if RUBY_ENGINE == "ruby"
17
19
  end
18
20
  end
19
21
 
@@ -38,7 +40,9 @@ module Celluloid
38
40
  end
39
41
 
40
42
  def backtrace
43
+ # rubocop:disable Metrics/LineLength
41
44
  ["#{self.class} backtrace unavailable. Please try `Celluloid.task_class = Celluloid::Task::Threaded` if you need backtraces here."]
45
+ # rubocop:enable Metrics/LineLength
42
46
  end
43
47
  end
44
48
  end
@@ -18,7 +18,7 @@ module Celluloid
18
18
  thread = Internals::ThreadHandle.new(Thread.current[:celluloid_actor_system], :task) do
19
19
  begin
20
20
  ex = @resume_queue.pop
21
- fail ex if ex.is_a?(TaskTerminated)
21
+ raise ex if ex.is_a?(TaskTerminated)
22
22
 
23
23
  yield
24
24
  rescue ::Exception => ex
@@ -40,12 +40,12 @@ module Celluloid
40
40
  end
41
41
 
42
42
  def deliver(value)
43
- fail DeadTaskError, "cannot resume a dead task" unless @thread.alive?
43
+ raise DeadTaskError, "cannot resume a dead task" unless @thread.alive?
44
44
 
45
45
  @yield_mutex.synchronize do
46
46
  @resume_queue.push(value)
47
47
  @yield_cond.wait(@yield_mutex)
48
- fail @exception_queue.pop while @exception_queue.size > 0
48
+ raise @exception_queue.pop until @exception_queue.empty?
49
49
  end
50
50
  rescue ThreadError
51
51
  raise DeadTaskError, "cannot resume a dead task"
@@ -3,7 +3,7 @@ module Celluloid
3
3
  class Task
4
4
  # Obtain the current task
5
5
  def self.current
6
- Thread.current[:celluloid_task] || fail(NotTaskError, "not within a task context")
6
+ Thread.current[:celluloid_task] || raise(NotTaskError, "not within a task context")
7
7
  end
8
8
 
9
9
  # Suspend the running task, deferring to the scheduler
@@ -27,7 +27,7 @@ module Celluloid
27
27
  actor = Thread.current[:celluloid_actor]
28
28
  @chain_id = Internals::CallChain.current_id
29
29
 
30
- fail NotActorError, "can't create tasks outside of actors" unless actor
30
+ raise NotActorError, "can't create tasks outside of actors" unless actor
31
31
  guard "can't create tasks inside of tasks" if Thread.current[:celluloid_task]
32
32
 
33
33
  create do
@@ -53,26 +53,29 @@ module Celluloid
53
53
  end
54
54
 
55
55
  def create(&_block)
56
- fail "Implement #{self.class}#create"
56
+ raise "Implement #{self.class}#create"
57
57
  end
58
58
 
59
59
  # Suspend the current task, changing the status to the given argument
60
60
  def suspend(status)
61
- fail "Cannot suspend while in exclusive mode" if exclusive?
62
- fail "Cannot suspend a task from outside of itself" unless Task.current == self
61
+ raise "Cannot suspend while in exclusive mode" if exclusive?
62
+ raise "Cannot suspend a task from outside of itself" unless Task.current == self
63
63
 
64
64
  @status = status
65
65
 
66
+ # !!! DO NOT INTRODUCE ADDITIONAL GLOBAL VARIABLES !!!
67
+ # rubocop:disable Style/GlobalVars
66
68
  if $CELLULOID_DEBUG && @dangerous_suspend
67
69
  Internals::Logger.with_backtrace(caller[2...8]) do |logger|
68
70
  logger.warn "Dangerously suspending task: type=#{@type.inspect}, meta=#{@meta.inspect}, status=#{@status.inspect}"
69
71
  end
70
72
  end
73
+ # rubocop:enable Style/GlobalVars
71
74
 
72
75
  value = signal
73
76
 
74
77
  @status = :running
75
- fail value if value.is_a?(Celluloid::Interruption)
78
+ raise value if value.is_a?(Celluloid::Interruption)
76
79
  value
77
80
  end
78
81
 
@@ -82,7 +85,9 @@ module Celluloid
82
85
  if running?
83
86
  deliver(value)
84
87
  else
88
+ # rubocop:disable Metrics/LineLength
85
89
  Internals::Logger.warn "Attempted to resume a dead task: type=#{@type.inspect}, meta=#{@meta.inspect}, status=#{@status.inspect}"
90
+ # rubocop:enable Metrics/LineLength
86
91
  end
87
92
  nil
88
93
  end
@@ -103,20 +108,24 @@ module Celluloid
103
108
 
104
109
  # Terminate this task
105
110
  def terminate
106
- fail "Cannot terminate an exclusive task" if exclusive?
111
+ raise "Cannot terminate an exclusive task" if exclusive?
107
112
 
108
113
  if running?
114
+ # !!! DO NOT INTRODUCE ADDITIONAL GLOBAL VARIABLES !!!
115
+ # rubocop:disable Style/GlobalVars
109
116
  if $CELLULOID_DEBUG
110
117
  Internals::Logger.with_backtrace(backtrace) do |logger|
111
118
  type = @dangerous_suspend ? :warn : :debug
112
119
  logger.send(type, "Terminating task: type=#{@type.inspect}, meta=#{@meta.inspect}, status=#{@status.inspect}")
113
120
  end
114
121
  end
122
+ # rubocop:enable Style/GlobalVars
123
+
115
124
  exception = TaskTerminated.new("task was terminated")
116
125
  exception.set_backtrace(caller)
117
126
  resume exception
118
127
  else
119
- fail DeadTaskError, "task is already dead"
128
+ raise DeadTaskError, "task is already dead"
120
129
  end
121
130
  end
122
131
 
@@ -125,8 +134,7 @@ module Celluloid
125
134
  @exclusive
126
135
  end
127
136
 
128
- def backtrace
129
- end
137
+ def backtrace; end
130
138
 
131
139
  # Is the current task still running?
132
140
  def running?
@@ -139,11 +147,14 @@ module Celluloid
139
147
  end
140
148
 
141
149
  def guard(message)
150
+ # !!! DO NOT INTRODUCE ADDITIONAL GLOBAL VARIABLES !!!
151
+ # rubocop:disable Style/GlobalVars
142
152
  if @guard_warnings
143
153
  Internals::Logger.warn message if $CELLULOID_DEBUG
144
154
  else
145
- fail message if $CELLULOID_DEBUG
155
+ raise message if $CELLULOID_DEBUG
146
156
  end
157
+ # rubocop:enable Style/GlobalVars
147
158
  end
148
159
 
149
160
  private
@@ -161,7 +172,9 @@ module Celluloid
161
172
 
162
173
  def thread_metadata
163
174
  method = @meta && @meta[:method_name] || "<no method>"
164
- klass = Thread.current[:celluloid_actor] && Thread.current[:celluloid_actor].behavior.subject.bare_object.class || "<no actor>"
175
+ klass = Thread.current[:celluloid_actor] &&
176
+ Thread.current[:celluloid_actor].behavior.subject.bare_object.class ||
177
+ "<no actor>"
165
178
  format("[Celluloid] %s#%s", klass, method)
166
179
  end
167
180
  end
@@ -1,3 +1,6 @@
1
+ # !!! DO NOT INTRODUCE ADDITIONAL GLOBAL VARIABLES !!!
2
+ # rubocop:disable Style/GlobalVars
1
3
  $CELLULOID_TEST = true
2
-
3
- require "celluloid"
4
+ # rubocop:enable Style/GlobalVars
5
+ #
6
+ require "celluloid/autostart"
@@ -1,5 +1,3 @@
1
- require "celluloid/fiber"
2
-
3
1
  module Celluloid
4
2
  class Thread < ::Thread
5
3
  def celluloid?
@@ -1,3 +1,3 @@
1
1
  module Celluloid
2
- VERSION = "0.17.4"
2
+ VERSION = "0.18.0".freeze
3
3
  end
data/lib/celluloid.rb CHANGED
@@ -1,17 +1,17 @@
1
+ # TODO: eliminate use of global variables
2
+ require "English"
3
+
1
4
  require "logger"
2
- require "thread"
3
- require "timeout"
4
5
  require "set"
6
+ require "timeout"
5
7
 
8
+ # !!! DO NOT INTRODUCE ADDITIONAL GLOBAL VARIABLES !!!
9
+ # rubocop:disable Style/GlobalVars
6
10
  $CELLULOID_DEBUG = false
7
- $CELLULOID_MANAGED ||= false
11
+ $CELLULOID_MONITORING = false
12
+ # rubocop:enable Style/GlobalVars
8
13
 
9
14
  require "celluloid/version"
10
- require "celluloid/notices"
11
-
12
- $CELLULOID_BACKPORTED = false if defined?(CELLULOID_FUTURE) && CELLULOID_FUTURE
13
- $CELLULOID_BACKPORTED = (ENV["CELLULOID_BACKPORTED"] != "false") unless defined?($CELLULOID_BACKPORTED)
14
- Celluloid::Notices.backported if $CELLULOID_BACKPORTED
15
15
 
16
16
  module Celluloid
17
17
  # Expose all instance methods as singleton methods
@@ -21,7 +21,7 @@ module Celluloid
21
21
  LINKING_TIMEOUT = 5
22
22
 
23
23
  # Warning message added to Celluloid objects accessed outside their actors
24
- BARE_OBJECT_WARNING_MESSAGE = "WARNING: BARE CELLULOID OBJECT "
24
+ BARE_OBJECT_WARNING_MESSAGE = "WARNING: BARE CELLULOID OBJECT ".freeze
25
25
 
26
26
  class << self
27
27
  attr_writer :actor_system # Default Actor System
@@ -33,9 +33,11 @@ module Celluloid
33
33
 
34
34
  def actor_system
35
35
  if Thread.current.celluloid?
36
- Thread.current[:celluloid_actor_system] || fail(Error, "actor system not running")
36
+ Thread.current[:celluloid_actor_system] || raise(Error, "actor system not running")
37
37
  else
38
- Thread.current[:celluloid_actor_system] || @actor_system || fail(Error, "Celluloid is not yet started; use Celluloid.boot")
38
+ Thread.current[:celluloid_actor_system] ||
39
+ @actor_system ||
40
+ raise(Error, "Celluloid is not yet started; use Celluloid.boot")
39
41
  end
40
42
  end
41
43
 
@@ -54,15 +56,23 @@ module Celluloid
54
56
  klass.property :exclusive_actor, default: false
55
57
  klass.property :exclusive_methods, multi: true
56
58
  klass.property :execute_block_on_receiver,
57
- default: [:after, :every, :receive],
59
+ default: %i[after every receive],
58
60
  multi: true
59
61
 
60
62
  klass.property :finalizer
61
63
  klass.property :exit_handler_name
62
64
 
63
65
  singleton = class << klass; self; end
64
- singleton.send(:remove_method, :trap_exit) rescue nil
65
- singleton.send(:remove_method, :exclusive) rescue nil
66
+ begin
67
+ singleton.send(:remove_method, :trap_exit)
68
+ rescue
69
+ nil
70
+ end
71
+ begin
72
+ singleton.send(:remove_method, :exclusive)
73
+ rescue
74
+ nil
75
+ end
66
76
 
67
77
  singleton.send(:define_method, :trap_exit) do |*args|
68
78
  exit_handler_name(*args)
@@ -96,20 +106,20 @@ module Celluloid
96
106
  def cores
97
107
  Internals::CPUCounter.cores
98
108
  end
99
- alias_method :cpus, :cores
100
- alias_method :ncpus, :cores
109
+ alias cpus cores
110
+ alias ncpus cores
101
111
 
102
112
  # Perform a stack dump of all actors to the given output object
103
113
  def stack_dump(output = STDERR)
104
114
  actor_system.stack_dump.print(output)
105
115
  end
106
- alias_method :dump, :stack_dump
116
+ alias dump stack_dump
107
117
 
108
118
  # Perform a stack summary of all actors to the given output object
109
119
  def stack_summary(output = STDERR)
110
120
  actor_system.stack_summary.print(output)
111
121
  end
112
- alias_method :summarize, :stack_summary
122
+ alias summarize stack_summary
113
123
 
114
124
  def public_registry
115
125
  actor_system.public_registry
@@ -148,7 +158,7 @@ module Celluloid
148
158
  end
149
159
 
150
160
  def init
151
- @actor_system = Actor::System.new
161
+ @actor_system ||= Actor::System.new
152
162
  end
153
163
 
154
164
  def start
@@ -157,24 +167,16 @@ module Celluloid
157
167
 
158
168
  def running?
159
169
  actor_system && actor_system.running?
170
+ rescue Error
171
+ false
160
172
  end
161
173
 
174
+ # de TODO Anticipate outside process finalizer that would by-pass this.
162
175
  def register_shutdown
163
176
  return if defined?(@shutdown_registered) && @shutdown_registered
164
-
165
- # Terminate all actors at exit
177
+ # Terminate all actors at exit, unless the exit is abnormal.
166
178
  at_exit do
167
- sleep 0.126 # hax grace period for unnaturally terminating actors
168
- # allows "reason" in exit_handler to resolve before being destroyed
169
- if defined?(RUBY_ENGINE) && RUBY_ENGINE == "ruby" && RUBY_VERSION >= "1.9"
170
- # workaround for MRI bug losing exit status in at_exit block
171
- # http://bugs.ruby-lang.org/issues/5218
172
- exit_status = $ERROR_INFO.status if $ERROR_INFO.is_a?(SystemExit)
173
- Celluloid.shutdown
174
- exit exit_status if exit_status
175
- else
176
- Celluloid.shutdown
177
- end
179
+ Celluloid.shutdown unless $ERROR_INFO
178
180
  end
179
181
  @shutdown_registered = true
180
182
  end
@@ -182,6 +184,7 @@ module Celluloid
182
184
  # Shut down all running actors
183
185
  def shutdown
184
186
  actor_system.shutdown
187
+ @actor_system = nil
185
188
  end
186
189
 
187
190
  def version
@@ -196,18 +199,18 @@ module Celluloid
196
199
  proxy._send_(:initialize, *args, &block)
197
200
  proxy
198
201
  end
199
- alias_method :spawn, :new
202
+ alias spawn new
200
203
 
201
204
  # Create a new actor and link to the current one
202
205
  def new_link(*args, &block)
203
- fail NotActorError, "can't link outside actor context" unless Celluloid.actor?
206
+ raise NotActorError, "can't link outside actor context" unless Celluloid.actor?
204
207
 
205
208
  proxy = Cell.new(allocate, behavior_options, actor_options).proxy
206
209
  Actor.link(proxy)
207
210
  proxy._send_(:initialize, *args, &block)
208
211
  proxy
209
212
  end
210
- alias_method :spawn_link, :new_link
213
+ alias spawn_link new_link
211
214
 
212
215
  # Run an actor in the foreground
213
216
  def run(*args, &block)
@@ -225,7 +228,7 @@ module Celluloid
225
228
  mailbox_class: mailbox_class,
226
229
  mailbox_size: mailbox_size,
227
230
  task_class: task_class,
228
- exclusive: exclusive_actor,
231
+ exclusive: exclusive_actor
229
232
  }
230
233
  end
231
234
 
@@ -235,7 +238,7 @@ module Celluloid
235
238
  exclusive_methods: exclusive_methods,
236
239
  exit_handler_name: exit_handler_name,
237
240
  finalizer: finalizer,
238
- receiver_block_executions: execute_block_on_receiver,
241
+ receiver_block_executions: execute_block_on_receiver
239
242
  }
240
243
  end
241
244
 
@@ -263,7 +266,7 @@ module Celluloid
263
266
  def bare_object
264
267
  self
265
268
  end
266
- alias_method :wrapped_object, :bare_object
269
+ alias wrapped_object bare_object
267
270
 
268
271
  # Are we being invoked in a different thread from our owner?
269
272
  def leaked?
@@ -279,18 +282,18 @@ module Celluloid
279
282
  def registered_name
280
283
  Actor.registered_name
281
284
  end
282
- alias_method :name, :registered_name
285
+ alias name registered_name
283
286
 
284
287
  def inspect
285
288
  return "..." if Celluloid.detect_recursion
286
289
 
287
290
  str = "#<"
288
291
 
289
- if leaked?
290
- str << Celluloid::BARE_OBJECT_WARNING_MESSAGE
291
- else
292
- str << "Celluloid::Proxy::Cell"
293
- end
292
+ str << if leaked?
293
+ Celluloid::BARE_OBJECT_WARNING_MESSAGE
294
+ else
295
+ "Celluloid::Proxy::Cell"
296
+ end
294
297
 
295
298
  str << "(#{self.class}:0x#{object_id.to_s(16)})"
296
299
  str << " " unless instance_variables.empty?
@@ -318,9 +321,10 @@ module Celluloid
318
321
  cause = case cause
319
322
  when String then RuntimeError.new(cause)
320
323
  when Exception then cause
321
- else fail TypeError, "Exception object/String expected, but #{cause.class} received"
322
- end
323
- fail AbortError.new(cause)
324
+ else raise TypeError, "Exception object/String expected, but #{cause.class} received"
325
+ end
326
+
327
+ raise AbortError, cause
324
328
  end
325
329
 
326
330
  # Terminate this actor
@@ -457,14 +461,13 @@ module Celluloid
457
461
  end
458
462
  end
459
463
 
460
- if defined?(JRUBY_VERSION) && JRUBY_VERSION == "1.7.3"
461
- fail "Celluloid is broken on JRuby 1.7.3. Please upgrade to 1.7.4+"
462
- end
463
-
464
464
  require "celluloid/exceptions"
465
465
 
466
466
  Celluloid.logger = Logger.new(STDERR).tap do |logger|
467
+ # !!! DO NOT INTRODUCE ADDITIONAL GLOBAL VARIABLES !!!
468
+ # rubocop:disable Style/GlobalVars
467
469
  logger.level = Logger::INFO unless $CELLULOID_DEBUG
470
+ # rubocop:enable Style/GlobalVars
468
471
  end
469
472
 
470
473
  Celluloid.shutdown_timeout = 10
@@ -483,8 +486,6 @@ require "celluloid/proxies"
483
486
  require "celluloid/mailbox"
484
487
  require "celluloid/mailbox/evented"
485
488
 
486
- require "celluloid/essentials"
487
-
488
489
  require "celluloid/group"
489
490
  require "celluloid/group/spawner"
490
491
  require "celluloid/group/pool" # TODO: Find way to only load this if being used.
@@ -497,13 +498,27 @@ require "celluloid/actor"
497
498
  require "celluloid/cell"
498
499
  require "celluloid/future"
499
500
 
501
+ require "celluloid/internals/call_chain"
502
+ require "celluloid/internals/cpu_counter"
503
+ require "celluloid/internals/handlers"
504
+ require "celluloid/internals/links"
505
+ require "celluloid/internals/logger"
506
+ require "celluloid/internals/method"
507
+ require "celluloid/internals/properties"
508
+ require "celluloid/internals/receivers"
509
+ require "celluloid/internals/registry"
510
+ require "celluloid/internals/responses"
511
+ require "celluloid/internals/signals"
512
+ require "celluloid/internals/stack"
513
+ require "celluloid/internals/task_set"
514
+ require "celluloid/internals/thread_handle"
515
+ require "celluloid/internals/uuid"
516
+
517
+ require "celluloid/notifications"
518
+ require "celluloid/supervision"
519
+
520
+ require "celluloid/logging"
500
521
  require "celluloid/actor/system"
501
- require "celluloid/actor/manager"
502
-
503
- require "celluloid/deprecate" unless $CELLULOID_BACKPORTED == false
504
-
505
- $CELLULOID_MONITORING = false
506
- Celluloid::Notices.output
507
522
 
508
523
  # Configure default systemwide settings
509
524
 
@@ -530,8 +545,3 @@ Celluloid.group_class =
530
545
  Celluloid::Group.const_get(str)
531
546
  end
532
547
  end
533
-
534
- unless defined?($CELLULOID_TEST) && $CELLULOID_TEST
535
- Celluloid.register_shutdown
536
- Celluloid.init
537
- end