Skip to content

Commit

Permalink
Merge pull request #782 from celluloid/merge-supervision
Browse files Browse the repository at this point in the history
Merge 'celluloid-supervision' back into the tree
  • Loading branch information
tarcieri committed Aug 21, 2018
2 parents 4d9f931 + 8dd7136 commit 471b7d8
Show file tree
Hide file tree
Showing 78 changed files with 2,202 additions and 134 deletions.
49 changes: 44 additions & 5 deletions .rubocop.yml
@@ -1,10 +1,14 @@
AllCops:
DisplayCopNames: true
TargetRubyVersion: 2.2.6

#
# Lint
#

Lint/AmbiguousBlockAssociation:
Enabled: false # TODO: enable

Lint/AssignmentInCondition:
Enabled: false # TODO: enable

Expand All @@ -24,7 +28,10 @@ Lint/RescueException:
Enabled: false # TODO: explicitly mark cases where we want to rescue Exception

Lint/ShadowedException:
Enabled: false # TODO; enable
Enabled: false # TODO: enable

Lint/UnderscorePrefixedVariableName:
Enabled: false # TODO: enable

Lint/UselessAssignment:
Enabled: false # TODO: enable
Expand All @@ -51,7 +58,7 @@ Metrics/CyclomaticComplexity:
Enabled: false # TODO: enable

Metrics/LineLength:
Max: 200 # TODO: decrease
Max: 128 # TODO: decrease

Metrics/MethodLength:
Max: 100
Expand All @@ -66,12 +73,32 @@ Metrics/PerceivedComplexity:
Enabled: false # TODO: enable

#
# Style
# Naming
#

Naming/AccessorMethodName:
Enabled: false # TODO: enable

Naming/MemoizedInstanceVariableName:
Enabled: false # TODO: enable

Naming/UncommunicativeMethodParamName:
Enabled: false # TODO: enable(?)

#
# Performance
#

Style/AccessorMethodName:
Performance/TimesMap:
Enabled: false # TODO: enable

#
# Style
#

Style/AccessModifierDeclarations:
Enabled: false # TODO: enable(?)

Style/CaseEquality:
Enabled: false

Expand All @@ -81,6 +108,9 @@ Style/ClassAndModuleChildren:
Style/ClassVars:
Enabled: false # TODO: enable

Style/CommentedKeyword:
Enabled: false # TODO: enable

Style/Documentation:
Enabled: false # TODO: enable

Expand All @@ -90,6 +120,9 @@ Style/DoubleNegation:
Style/For:
Enabled: false # TODO: enable

Style/FormatStringToken:
Enabled: false # TODO: enable

Style/FrozenStringLiteralComment:
Enabled: false # TODO: enable

Expand All @@ -99,7 +132,10 @@ Style/GuardClause:
Style/IfInsideElse:
Enabled: false # TODO: enable

Style/MethodMissing:
Style/MethodMissingSuper:
Enabled: false # TODO: enable

Style/MissingRespondToMissing:
Enabled: false # TODO: enable

Style/ModuleFunction:
Expand All @@ -114,6 +150,9 @@ Style/RegexpLiteral:
Style/RescueModifier:
Enabled: false # TODO: enable

Style/RescueStandardError:
Enabled: false # TODO: enable

Style/SafeNavigation:
Enabled: false

Expand Down
1 change: 0 additions & 1 deletion .ruby-version

This file was deleted.

6 changes: 3 additions & 3 deletions Gemfile
Expand Up @@ -7,11 +7,11 @@ group :development do
end

group :test do
gem "benchmark-ips", require: false
gem "coveralls", ">= 0.8", require: false
gem "rspec", "~> 3", require: false
gem "rspec-retry", "~> 0.5", require: false
gem "rubocop", "= 0.45.0", require: false
gem "coveralls", ">= 0.8", require: false
gem "benchmark-ips", require: false
gem "rubocop", "= 0.58.2", require: false
end

group :development, :test do
Expand Down
2 changes: 1 addition & 1 deletion Guardfile
Expand Up @@ -5,7 +5,7 @@ guard "rspec", cmd: "bundle exec rspec" do
{
"actor_examples" => "actor",
"example_actor_class" => "actor",
"mailbox_examples" => %w(mailbox evented_mailbox),
"mailbox_examples" => %w[mailbox evented_mailbox],
"task_examples" => ["tasks/task_fiber", "tasks/task_thread"]
}.each do |examples, spec|
watch("spec/support/#{examples}.rb") do
Expand Down
4 changes: 2 additions & 2 deletions Rakefile
Expand Up @@ -2,5 +2,5 @@ require "bundler/gem_tasks"

Dir["tasks/**/*.rake"].each { |task| load task }

task default: %w(spec rubocop)
task ci: %w(default benchmark)
task default: %w[spec rubocop]
task ci: %w[default benchmark]
2 changes: 1 addition & 1 deletion benchmarks/ring.rb 100644 → 100755
Expand Up @@ -4,7 +4,7 @@
require "bundler/setup"
require "celluloid"
require "benchmark/ips"
require File.expand_path("../../examples/ring", __FILE__)
require File.expand_path("../examples/ring", __dir__)

# 512-node ring
ring = Ring.new 512
Expand Down
Empty file modified benchmarks/uuid.rb 100644 → 100755
Empty file.
5 changes: 1 addition & 4 deletions celluloid.gemspec
@@ -1,7 +1,6 @@
# -*- encoding: utf-8 -*-
# frozen_string_literal: true

lib = File.expand_path("../lib", __FILE__)
lib = File.expand_path("lib", __dir__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require "celluloid/version"

Expand Down Expand Up @@ -34,6 +33,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
2 changes: 1 addition & 1 deletion examples/basic_usage.rb
@@ -1,6 +1,6 @@
#!/usr/bin/env ruby

$LOAD_PATH.push File.expand_path("../../lib", __FILE__)
$LOAD_PATH.push File.expand_path("../lib", __dir__)
require "celluloid/autostart"

class Counter
Expand Down
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!"
2 changes: 1 addition & 1 deletion examples/futures.rb
@@ -1,6 +1,6 @@
#!/usr/bin/env ruby

$LOAD_PATH.push File.expand_path("../../lib", __FILE__)
$LOAD_PATH.push File.expand_path("../lib", __dir__)
require "celluloid/autostart"
require "digest/sha2"

Expand Down
2 changes: 1 addition & 1 deletion examples/ring.rb
@@ -1,6 +1,6 @@
#!/usr/bin/env ruby

$LOAD_PATH.push File.expand_path("../../lib", __FILE__)
$LOAD_PATH.push File.expand_path("../lib", __dir__)
require "celluloid/autostart"

class Ring
Expand Down
2 changes: 1 addition & 1 deletion examples/simple_pmap.rb
@@ -1,6 +1,6 @@
#!/usr/bin/env ruby

$LOAD_PATH.push File.expand_path("../../lib", __FILE__)
$LOAD_PATH.push File.expand_path("../lib", __dir__)
require "celluloid/autostart"

module Enumerable
Expand Down
2 changes: 1 addition & 1 deletion examples/stack.rb 100644 → 100755
@@ -1,6 +1,6 @@
#!/usr/bin/env ruby

$LOAD_PATH.push File.expand_path("../../lib", __FILE__)
$LOAD_PATH.push File.expand_path("../lib", __dir__)
require "celluloid/autostart"

# This example builds on basic_usage.rb to show two things about #async: the
Expand Down
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
2 changes: 1 addition & 1 deletion examples/timers.rb
@@ -1,6 +1,6 @@
#!/usr/bin/env ruby

$LOAD_PATH.push File.expand_path("../../lib", __FILE__)
$LOAD_PATH.push File.expand_path("../lib", __dir__)
require "celluloid/current"

class TimerExample
Expand Down

0 comments on commit 471b7d8

Please sign in to comment.