Skip to content

Instantly share code, notes, and snippets.

@Papipo
Last active August 29, 2015 14:03
Show Gist options
  • Save Papipo/151d0acd4f69e81454ba to your computer and use it in GitHub Desktop.
Save Papipo/151d0acd4f69e81454ba to your computer and use it in GitHub Desktop.
Hahahacking
module EventSourcing
class AggregateManager
include Celluloid
def instance_of(klass, id)
# So, in order to remove it when terminated, now I use supervise_as(),
# But I have to remove the exlusive mode because it can't be used with
# the blocks that supervise_as() uses under the hood.
# But now I don't think access to this method is serialized as it was
# with __exclusive__
klass.supervise_as(id) unless Celluloid::Actor[id] && Celluloid::Actor[id].alive?
Celluloid::Actor[id]
end
end
end
module EventSourcing
class AggregateManager
include Celluloid
exclusive
#On this first version I do that with the __exclusive mode__.
#The problem with this approach is that when I terminate an aggregate,
#due to inactivity timeout for example, it isn't removed from the registry.
def instance_of(klass, id)
Celluloid::Actor[id] = klass.send(:new) unless Celluloid::Actor[id] && Celluloid::Actor[id].alive?
Celluloid::Actor[id]
end
end
end

Explanation

The idea here is that aggregate instances (actors) are singletons. For a given aggregate id, only one actor can be running concurrently. So, I need to have a single actor that serializes access to aggregates, that's the AggregateManager.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment