Created
July 18, 2016 08:13
-
-
Save Horusiath/4f252ceef5eae489928142da98192e4d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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