Skip to content

Commit

Permalink
Merge 8131c8a into 4d9f931
Browse files Browse the repository at this point in the history
  • Loading branch information
tarcieri committed Aug 21, 2018
2 parents 4d9f931 + 8131c8a commit 48c65b9
Show file tree
Hide file tree
Showing 38 changed files with 2,320 additions and 6 deletions.
1 change: 0 additions & 1 deletion .ruby-version

This file was deleted.

2 changes: 0 additions & 2 deletions celluloid.gemspec
Expand Up @@ -34,6 +34,4 @@ Gem::Specification.new do |spec|
spec.required_rubygems_version = ">= 2.0.0"

spec.add_runtime_dependency "timers", "~> 4"
spec.add_runtime_dependency "celluloid-pool", "~> 0.20"
spec.add_runtime_dependency "celluloid-supervision", "~> 0.20"
end
78 changes: 78 additions & 0 deletions examples/configurations.rb
@@ -0,0 +1,78 @@
require "celluloid/current"

puts "Use Supervision::Configuration objects!"

class Hello
include Celluloid

finalizer :ceasing

def initialize(to)
@to = to
puts "Created Hello #{@to}"
end

def ceasing
puts "Hello #{@to} go buhbye"
end
end

class FooBar
include Celluloid

finalizer :ceasing

def initialize(i=0)
@i = i
puts "Created FooBar: #{@i}"
end

def ceasing
puts "FooBar FooBar: #{@i} go buhbye"
end
end

puts "\nInstantiated in bulk, using #deploy"

config = Celluloid::Supervision::Configuration.define([
{type: FooBar, as: :foobar},
{type: Hello, as: :hello, args: ["World"]},
])

config.deploy
puts "...shut it down"
config.shutdown

puts "\nInstantiated in bulk, using .deploy"

config = Celluloid::Supervision::Configuration.deploy([
{type: FooBar, as: :foobar, args: [1]},
{type: Hello, as: :hello, args: ["World"]},
])

puts "...shut it down"
config.shutdown

puts "\nInstantiated two actors individually, using a local configuration object"

config = Celluloid::Supervision::Configuration.new
config.define type: FooBar, as: :foobar11, args: [11]
config.define type: FooBar, as: :foobar33, args: [33]
config.deploy

puts "Instantiated another, which starts automatically."
puts "... using the local configuration object"
puts "... using a lazy loaded argument"
config.add type: Hello, as: :hello, args: -> { "Spinning World" }

config.shutdown

puts "\nReuse our last configuration!"

config.deploy
puts "...shut it down"
config.shutdown

puts "Thinking for 4 seconds, and 4 seconds only."
sleep 4
puts "Use Supervision::Configuration objects. Really!"
83 changes: 83 additions & 0 deletions examples/supervisors_and_registry.old.rb
@@ -0,0 +1,83 @@
require "celluloid/autostart"

class MyActor
include Celluloid
attr_reader :state

def initialize
@state = :clean
end

def broken_method
@state = :dirty
oh_crap_im_totally_broken
end
end

#
# Using the Supervisor API directly
#

puts "*** Demonstrating using the Supervisor API directly"

# Calling supervise directly returns the supervisor
supervisor = MyActor.supervise

# We can get to the current version of an actor by calling
# Celluloid::Supervisor#actors. This prints ':clean'

puts "We should be in a clean state now: #{supervisor.actors.first.state}"
puts "Brace yourself for a crash message..."

# If we call a method that crashes an actor, it will print out a debug message,
# then restart an actor in a clean state
begin
supervisor.actors.first.broken_method
rescue NameError
puts "Uhoh, we crashed the actor..."
end

puts "The supervisor should automatically restart the actor"

# By now we'll be back in a :clean state!
begin
puts "We should now be in a clean state again: #{supervisor.actors.first.state}"
rescue Celluloid::DeadActorError
# Perhaps we got ahold of the actor before the supervisor restarted it
retry
end

#
# Using the Actor Registry
# This is the preferred approach and will make using DCell easier
#

puts "*** Demonstrating using the actor registry"

# We can give our actor a name and thus avoid interacting with the supervisor
MyActor.supervise_as :my_actor

# Same as above, just getting the actor from a different place
puts "We should be in a clean state now: #{Celluloid::Actor[:my_actor].state}"
puts "Brace yourself for a crash message..."

# If we call a method that crashes an actor, it will print out a debug message,
# then restart an actor in a clean state
begin
Celluloid::Actor[:my_actor].broken_method
rescue NameError
puts "Uhoh, we crashed the actor..."
end

puts "The supervisor should automatically restart the actor"

# By now we'll be back in a :clean state!
begin
puts "We should now be in a clean state again: #{Celluloid::Actor[:my_actor].state}"
rescue Celluloid::DeadActorError
# Perhaps we got ahold of the actor before the supervisor restarted it
# Don't want to catch Celluloid::DeadActorError all over the place? If this
# code were in a supervised Celluloid::Actor itself, the supervisor would
# catch Celluloid::DeadActorError and automatically restart this actor
retry
end
82 changes: 82 additions & 0 deletions examples/supervisors_and_registry.rb
@@ -0,0 +1,82 @@
require "celluloid/autostart"

class MyActor
include Celluloid
attr_reader :state

def initialize
@state = :clean
end

def broken_method
@state = :dirty
oh_crap_im_totally_broken
end
end

#
# Using the Supervisor API directly
#

puts "*** Demonstrating using the Supervisor API directly"

# Calling supervise directly returns the supervisor
supervisor = MyActor.supervise

# We can get to the current version of an actor by calling
# Celluloid::Supervisor#actors. This prints ':clean'
puts "We should be in a clean state now: #{supervisor.actors.first.state}"
puts "Brace yourself for a crash message..."

# If we call a method that crashes an actor, it will print out a debug message,
# then restart an actor in a clean state
begin
supervisor.actors.first.broken_method
rescue NameError
puts "Uhoh, we crashed the actor..."
end

puts "The supervisor should automatically restart the actor"

# By now we'll be back in a :clean state!
begin
puts "We should now be in a clean state again: #{supervisor.actors.first.state}"
rescue Celluloid::DeadActorError
# Perhaps we got ahold of the actor before the supervisor restarted it
retry
end

#
# Using the Actor Registry
# This is the preferred approach and will make using DCell easier
#

puts "*** Demonstrating using the actor registry"

# We can give our actor a name and thus avoid interacting with the supervisor
MyActor.supervise as: :my_actor

# Same as above, just getting the actor from a different place
puts "We should be in a clean state now: #{Celluloid::Actor[:my_actor].state}"
puts "Brace yourself for a crash message..."

# If we call a method that crashes an actor, it will print out a debug message,
# then restart an actor in a clean state
begin
Celluloid::Actor[:my_actor].broken_method
rescue NameError
puts "Uhoh, we crashed the actor..."
end

puts "The supervisor should automatically restart the actor"

# By now we'll be back in a :clean state!
begin
puts "We should now be in a clean state again: #{Celluloid::Actor[:my_actor].state}"
rescue Celluloid::DeadActorError
# Perhaps we got ahold of the actor before the supervisor restarted it
# Don't want to catch Celluloid::DeadActorError all over the place? If this
# code were in a supervised Celluloid::Actor itself, the supervisor would
# catch Celluloid::DeadActorError and automatically restart this actor
retry
end
3 changes: 0 additions & 3 deletions lib/celluloid.rb
Expand Up @@ -10,9 +10,6 @@
# rubocop:disable Style/GlobalVars
$CELLULOID_DEBUG = false
$CELLULOID_MONITORING = false

# TODO: gut this
$CELLULOID_BACKPORTED = false
# rubocop:enable Style/GlobalVars

require "celluloid/version"
Expand Down
6 changes: 6 additions & 0 deletions lib/celluloid/pool.rb
@@ -0,0 +1,6 @@
require "celluloid" unless defined? Celluloid

require "celluloid/supervision"

require "celluloid/supervision/container/pool"
require "celluloid/supervision/container/behavior/pool"
17 changes: 17 additions & 0 deletions lib/celluloid/supervision.rb
@@ -0,0 +1,17 @@
require "celluloid" unless defined? Celluloid

require "celluloid/supervision/constants"
require "celluloid/supervision/supervise"

require "celluloid/supervision/container"
require "celluloid/supervision/container/instance"
require "celluloid/supervision/container/behavior"
require "celluloid/supervision/container/injections"

require "celluloid/supervision/container/behavior/tree"

require "celluloid/supervision/validation"
require "celluloid/supervision/configuration"
require "celluloid/supervision/configuration/instance"

require "celluloid/supervision/service"

0 comments on commit 48c65b9

Please sign in to comment.