Permalink
Browse files

Update eventmachine to 1.0.0 and use epoll

Instead of using a custom thread and queue, use `EM.defer`.

Change-Id: I6be18129b45b95d4d35feb5d5621301a0b6d5612
  • Loading branch information...
1 parent 97cacde commit 2b0b2107375296afd60de65e7d2604a199623d56 @pietern pietern committed Oct 29, 2012
Showing with 50 additions and 39 deletions.
  1. +1 −1 Gemfile
  2. +2 −8 Gemfile.lock
  3. +3 −3 bin/dea
  4. +10 −4 lib/dea/droplet.rb
  5. +9 −17 lib/dea/instance_registry.rb
  6. +6 −6 spec/dea/instance_registry_spec.rb
  7. +19 −0 spec/support/em.rb
View
2 Gemfile
@@ -1,6 +1,6 @@
source "https://rubygems.org"
-gem "eventmachine", :git => "https://github.com/cloudfoundry/eventmachine.git", :branch => "release-0.12.11-cf"
+gem "eventmachine"
gem "em-http-request", "~> 1.0.0.beta.3", :require => "em-http"
gem "em-warden-client", :git => "https://github.com/cloudfoundry/warden.git"
View
10 Gemfile.lock
@@ -1,11 +1,4 @@
GIT
- remote: https://github.com/cloudfoundry/eventmachine.git
- revision: 2806c630d8631d5dcf9fb2555f665b829052aabe
- branch: release-0.12.11-cf
- specs:
- eventmachine (0.12.11.cloudfoundry.3)
-
-GIT
remote: https://github.com/cloudfoundry/steno.git
revision: 9334fecdf342800f7e247f2236b66080e4168ec8
specs:
@@ -72,6 +65,7 @@ GEM
http_parser.rb (>= 0.5.1)
em-socksify (0.1.0)
eventmachine
+ eventmachine (1.0.0)
ffi (1.0.11)
hashie (1.2.0)
http_parser.rb (0.5.3)
@@ -120,7 +114,7 @@ DEPENDENCIES
ci_reporter
em-http-request (~> 1.0.0.beta.3)
em-warden-client!
- eventmachine!
+ eventmachine
grape!
nats
rack
View
6 bin/dea
@@ -22,10 +22,10 @@ rescue => e
end
bootstrap = Dea::Bootstrap.new(config)
+
+EM.epoll
+
EM.run do
bootstrap.setup
bootstrap.start
end
-
-
-
View
14 lib/dea/droplet.rb
@@ -73,7 +73,7 @@ def download(uri, &blk)
end
end
- def destroy(&blk)
+ def destroy(&callback)
dir_to_remove = droplet_dirname + ".deleted." + Time.now.to_i.to_s
# Rename first to both prevent a new instance from referencing a file
@@ -82,11 +82,17 @@ def destroy(&blk)
logger.debug("Renaming #{droplet_dirname} to #{dir_to_remove}")
File.rename(droplet_dirname, dir_to_remove)
- EM.defer do
+ operation = lambda do
logger.debug("Removing #{dir_to_remove}")
- FileUtils.rm_rf(dir_to_remove)
- blk.call if blk
+
+ begin
+ FileUtils.rm_rf(dir_to_remove)
+ rescue => e
+ logger.log_exception(e)
+ end
end
+
+ EM.defer(operation, callback)
end
private
View
26 lib/dea/instance_registry.rb
@@ -158,29 +158,21 @@ def reap_crash(instance_id, &blk)
end
def destroy_crash_artifacts(instance_id, &callback)
- @reap_crash_queue ||= Queue.new
- @reap_crash_thread ||= Thread.new do
- loop do
- crash_path, callback = @reap_crash_queue.pop
+ crash_path = File.join(config.crashes_path, instance_id)
- if crash_path.nil?
- break
- end
+ return if crash_path.nil?
- logger.debug2("Removing path #{crash_path}")
+ operation = lambda do
+ logger.debug2("Removing path #{crash_path}")
- begin
- FileUtils.rm_rf(crash_path)
- rescue => e
- logger.log_exception(e)
- end
-
- EM.next_tick(&callback) if callback
+ begin
+ FileUtils.rm_rf(crash_path)
+ rescue => e
+ logger.log_exception(e)
end
end
- crash_path = File.join(config.crashes_path, instance_id)
- @reap_crash_queue.push([crash_path, callback])
+ EM.defer(operation, callback)
end
def disk_pressure?
View
12 spec/dea/instance_registry_spec.rb
@@ -97,7 +97,7 @@
em do
instance_registry.reap_orphaned_crashes
- EM.add_timer(0.01) do
+ after_defers_finish do
instance.should be_reaped
done
@@ -111,7 +111,7 @@
em do
instance_registry.reap_orphaned_crashes
- EM.add_timer(0.01) do
+ after_defers_finish do
instance.should_not be_reaped
done
@@ -150,7 +150,7 @@
em do
instance_registry.reap_crashes
- EM.add_timer(0.01) do
+ after_defers_finish do
instances[0].should_not be_reaped
instances[1].should be_reaped
@@ -169,7 +169,7 @@
em do
instance_registry.reap_crashes
- EM.add_timer(0.01) do
+ after_defers_finish do
instances[0].should_not be_reaped
instances[1].should be_reaped
instances[2].should be_reaped
@@ -202,7 +202,7 @@
em do
instance_registry.reap_crashes_under_disk_pressure
- EM.add_timer(0.01) do
+ after_defers_finish do
instances[0].should be_reaped
instances[1].should_not be_reaped
@@ -222,7 +222,7 @@
em do
instance_registry.reap_crashes_under_disk_pressure
- EM.add_timer(0.01) do
+ after_defers_finish do
instances[0].should be_reaped
instances[1].should be_reaped
View
19 spec/support/em.rb
@@ -7,11 +7,14 @@ def em(options = {})
raise "no block given" unless block_given?
timeout = options[:timeout] ||= 0.1
+ ::EM.epoll
+
::EM.run {
quantum = 0.005
::EM.set_quantum(quantum * 1000) # Lowest possible timer resolution
::EM.set_heartbeat_interval(quantum) # Timeout connections asap
::EM.add_timer(timeout) { raise "timeout" }
+
yield
}
end
@@ -26,6 +29,22 @@ def done
}
end
+ def after_defers_finish
+ raise "reactor not running" if !::EM.reactor_running?
+
+ timer = nil
+
+ check = lambda do
+ if ::EM.defers_finished?
+ timer.cancel
+
+ yield
+ end
+ end
+
+ timer = ::EM::PeriodicTimer.new(0.01, &check)
+ end
+
module HttpServer
attr_writer :blk

0 comments on commit 2b0b210

Please sign in to comment.