Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
38 changed files
with
2,320 additions
and
6 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
require "celluloid" unless defined? Celluloid | ||
|
||
require "celluloid/supervision" | ||
|
||
require "celluloid/supervision/container/pool" | ||
require "celluloid/supervision/container/behavior/pool" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" |
Oops, something went wrong.