Skip to content

Instantly share code, notes, and snippets.

@Horusiath
Created July 18, 2016 08:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Horusiath/4f252ceef5eae489928142da98192e4d to your computer and use it in GitHub Desktop.
Save Horusiath/4f252ceef5eae489928142da98192e4d to your computer and use it in GitHub Desktop.
public class ModulesActor : ReceiveActor, IUnboundedStash
{
private Dictionary<string, Tuple<Props, ModuleState>> _moduleRecipes;
public Stash Stash { get; set; }
public ModulesActor()
{
Ready();
}
public override void AroundPreStart()
{
_moduleRecipes = new Dictionary<string, Tuple<Props, ModuleState>>();
}
public override void AroundPostStop()
{
_moduleRecipes = null;
}
protected void Ready()
{
Receive<LoadModules>(modules => Load());
Receive<GetModuleMeta>(whois =>
{
Sender.Tell(GetModuleMeta(whois.ModuleName));
});
Receive<CheckModuleState>(state =>
{
Sender.Tell(GetModuleState(state.ModuleName));
});
}
protecte void WaitingForLoading()
{
Receive<ModulesLoaded>(c =>
{
_moduleRecipes = c.Modules;
// unstash all messages stashed so far
Stash.UnstashAll();
Become(Ready);
});
// stash any incomming message untill modules loaded will come
ReceiveAny(x => Stash.Stash(x));
}
private ModuleState GetModuleState(string moduleName)
{
if (!_moduleRecipes.ContainsKey(moduleName))
return ModuleState.NOT_IMPLEMENTED;
return _moduleRecipes[moduleName].Item2;
}
protected override SupervisorStrategy SupervisorStrategy()
{
return new OneForOneStrategy(1,
// maxNumberOfRetries
TimeSpan.FromSeconds(30),
// withinTimeRange
exception => // localOnlyDecider
{
//Error that we cannot recover from, stop the failing actor
if (exception is NotSupportedException) return Directive.Stop;
// Error related to configuration setting
if (exception is ConfigurationErrorsException) {
return Directive.Escalate;
}
//In all other cases, just stop the failing actor
return Directive.Stop;
});
}
private ModuleMeta GetModuleMeta(string moduleName)
{
if (!_moduleRecipes.ContainsKey(moduleName)) return null;
var moduleMeta = new ModuleMeta(_moduleRecipes[moduleName].Item1,
_moduleRecipes[moduleName].Item2);
return moduleMeta;
}
private static void Load()
{
var loader = Context.ActorOf(Props.Create(() => new ModulesLoadingActor()));
loader.Tell(new LoadModules());
Become(WaitingForLoading);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment