Skip to content

Commit

Permalink
Added support for dm identity map and action timeouts.
Browse files Browse the repository at this point in the history
  • Loading branch information
snusnu committed Nov 6, 2008
1 parent ec9dcf7 commit 0396f98
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/merb_resource_controller.rb
@@ -1,9 +1,10 @@
# make sure we're running inside Merb
if defined?(Merb::Plugins)

# Merb gives you a Merb::Plugins.config hash...feel free to put your stuff in your piece of it
# Merb gave me a Merb::Plugins.config hash
# i felt free to put my stuff in my piece of it
Merb::Plugins.config[:merb_resource_controller] = {
:orm => :datamapper
:identity_map => true
}

Merb::BootLoader.before_app_loads do
Expand All @@ -12,6 +13,9 @@
require DIR / 'resource_proxy'
require DIR / 'actions'
require DIR / 'resource_controller'
if Merb::Plugins.config[:merb_resource_controller][:identity_map]
require DIR / 'identity_map_support'
end
end

Merb::BootLoader.after_app_loads do
Expand Down
41 changes: 41 additions & 0 deletions lib/merb_resource_controller/identity_map_support.rb
@@ -0,0 +1,41 @@
module Merb
module ResourceController
module DM

# to be extended into controller
module IdentityMapSupport

def enable_identity_map(options = {})
@action_timeout = options[:action_timeout] || 0
# maybe also useful for retries
class_inheritable_accessor :action_timeout
include InstanceMethods
end

module InstanceMethods

# taken from:
# http://datamapper.org/doku.php?id=docs:identity_map
def _call_action(*)
repository do |r| # enable identity_map
Merb.logger.info "Inside #{r.name} repository block"
if self.class.respond_to?(:action_timeout)
return super if self.class.action_timeout < 1
# more information on system_timer:
# http://adam.blog.heroku.com/past/2008/6/17/battling_wedged_mongrels_with_a/
require 'system_timer' # laziness
SystemTimer.timeout(self.class.action_timeout) do
super
end
else
super
end
end
end

end

end
end
end
end
2 changes: 2 additions & 0 deletions spec/integration/app/controllers/application.rb
@@ -1,3 +1,5 @@
class Application < Merb::Controller
extend Merb::ResourceController::Mixin::ClassMethods
extend Merb::ResourceController::DM::IdentityMapSupport
enable_identity_map :action_timeout => 1
end

0 comments on commit 0396f98

Please sign in to comment.