internal IEnumerable <AgentInformation> GetAgentInfos <TAgent>(ExecutionScopeOptions scope) where TAgent : IAgent { var agents = Enumerable.Empty <AgentInformation>(); if (scope.HasFlag(ExecutionScopeOptions.Local)) { agents = agents.Concat(GetLocalAgentInfos <TAgent>()); } if (scope.HasFlag(ExecutionScopeOptions.Remote)) { agents = agents.Concat(GetRemoteAgentInfos <TAgent>()); } return(agents); }
public IEnumerable <Tuple <AgentInformation, TAgent> > GetAgents <TAgent>(ExecutionScopeOptions scope, bool ignoreAgentState = false) where TAgent : IAgent { var agents = Enumerable.Empty <Tuple <AgentInformation, TAgent> >(); if (scope.HasFlag(ExecutionScopeOptions.Local)) { agents = agents.Concat(GetLocalAgentInfos <TAgent>().Select(info => Tuple.Create((AgentInformation)info, (TAgent)info.Agent))); } if (scope.HasFlag(ExecutionScopeOptions.Remote)) { agents = agents.Concat(GetRemoteAgentInfos <TAgent>().Select(info => Tuple.Create((AgentInformation)info, (TAgent)info.ServiceClientFactory.CreateServiceClient <TAgent>()))); } if (!ignoreAgentState) { agents = agents.Where(a => a.Item1.LastKnownState == AgentState.Activated); } return(agents); }
public IObservable <T> ObserveAny <TAgent, T>(string propertyName, ExecutionScopeOptions scope = ExecutionScopeOptions.All, bool ignoreAgentState = false) where TAgent : IAgent { if (string.IsNullOrEmpty(propertyName)) { throw new ArgumentNullException("propertyName"); } return(ObserveFirstUnsafe <TAgent, T>(propertyName, scope, ignoreAgentState) .Select( r => { if (!r.IsLeft) { throw r.Right; } else { return r.Left; } }) .Retry()); }
public async Task <ExecutionResult <TResult> > TryExecuteOnFirst <TAgent, TResult>(Func <TAgent, Task <TResult> > operation, ExecutionScopeOptions scope = ExecutionScopeOptions.All, CancellationToken?ct = null, bool ignoreAgentState = false) where TAgent : IAgent { if (operation == null) { throw new ArgumentNullException("operation"); } ct = ct ?? CancellationToken.None; ExecutionResult <TResult> result = null; foreach (var agentInfo in GetAgentInfos <TAgent>(scope).Where(info => ignoreAgentState || (info.IsReachable && info.LastKnownState == AgentState.Activated))) { if (ct.Value.IsCancellationRequested) { result = new ExecutionResult <TResult> { AgentId = agentInfo.AgentId, IsCanceled = true }; break; } else { result = await TryExecuteOnOne(agentInfo.AgentId, operation, ignoreAgentState).ConfigureAwait(false); if (result.IsSuccessful) { break; } } } if (result == null) { result = new ExecutionResult <TResult> { Exception = new InvalidOperationException(string.Format("An agent that implements the contract '{0}' was not found.", typeof(TAgent).AssemblyQualifiedName)) } } ; return(result); } }
public Task <ExecutionResult <TResult> > TryExecuteOnFirst <TAgent, TResult>(Func <TAgent, TResult> operation, ExecutionScopeOptions scope = ExecutionScopeOptions.All, CancellationToken?ct = null, bool ignoreAgentState = false) where TAgent : IAgent { return(TryExecuteOnFirst <TAgent, TResult>(a => Task.Run(() => operation(a), ct ?? CancellationToken.None), scope, ct, ignoreAgentState)); }
public async Task <ExecutionResult> TryExecuteOnFirst <TAgent>(Func <TAgent, Task> operation, ExecutionScopeOptions scope = ExecutionScopeOptions.All, CancellationToken?ct = null, bool ignoreAgentState = false) where TAgent : IAgent { return(await TryExecuteOnFirst <TAgent, object>(async a => { await operation(a).ConfigureAwait(false); return (object)null; }, scope, ct, ignoreAgentState).ConfigureAwait(false)); }
public IObservable <Tuple <AgentInformation, IObservable <T> > > ObserveAll <TAgent, T>(string propertyName, ExecutionScopeOptions scope = ExecutionScopeOptions.All, bool ignoreAgentState = false) where TAgent : IAgent { return(ObserveAllUnsafe <TAgent, T>(propertyName, scope, ignoreAgentState).Select(t => Tuple.Create(t.Item1, t.Item2.SelectLeft(left => left)))); }
private IObservable <Tuple <AgentInformation, IObservable <Either <T, Exception> > > > ObserveAllUnsafe <TAgent, T>(string propertyName, ExecutionScopeOptions scope = ExecutionScopeOptions.All, bool ignoreAgentState = false) where TAgent : IAgent { if (string.IsNullOrEmpty(propertyName)) { throw new ArgumentNullException("propertyName"); } var currentAgents = GetAgentInfos <TAgent>(scope); if (!ignoreAgentState) { currentAgents = currentAgents.Where(a => a.IsReachable && a.LastKnownState == AgentState.Activated); } var newAgentsSource = this.AgentDataSource .Where(a => (scope.HasFlag(a.IsLocal ? ExecutionScopeOptions.Local : ExecutionScopeOptions.Remote)) && a.Contracts.Contains(typeof(TAgent).AssemblyQualifiedName)); if (!ignoreAgentState) { newAgentsSource = newAgentsSource.Where(a => a.IsReachable && a.LastKnownState == AgentState.Activated); } return(Observable .Merge(currentAgents.ToObservable(), newAgentsSource) .Distinct(a => a.AgentId) .Select(a => Tuple.Create(a, ObserveOneUnsafe <T>(a.AgentId, propertyName, ignoreAgentState)))); }
public IEnumerable <Task <ExecutionResult <TResult> > > TryExecuteOnAll <TAgent, TResult>(Func <TAgent, Task <TResult> > operation, ExecutionScopeOptions scope = ExecutionScopeOptions.All, bool ignoreAgentState = false) where TAgent : IAgent { if (operation == null) { throw new ArgumentNullException("operation"); } return(this.GetAgentInfos <TAgent>(scope) .Where(info => ignoreAgentState || (info.IsReachable && info.LastKnownState == AgentState.Activated)) .Select(info => TryExecuteOnOne(info.AgentId, operation, ignoreAgentState))); }
public IEnumerable <Task <ExecutionResult> > TryExecuteOnAll <TAgent>(Func <TAgent, Task> operation, ExecutionScopeOptions scope = ExecutionScopeOptions.All, bool ignoreAgentState = false) where TAgent : IAgent { return(TryExecuteOnAll <TAgent, object>(async a => { await operation(a).ConfigureAwait(false); return (object)null; }, scope, ignoreAgentState) .Select(async t => { return (ExecutionResult)await t.ConfigureAwait(false); })); }
public IEnumerable <Task <ExecutionResult> > TryExecuteOnAll <TAgent>(Action <TAgent> operation, ExecutionScopeOptions scope = ExecutionScopeOptions.All, CancellationToken?ct = null, bool ignoreAgentState = false) where TAgent : IAgent { return(TryExecuteOnAll <TAgent>(a => Task.Run(() => operation(a), ct ?? CancellationToken.None), scope, ignoreAgentState)); }
public IObservable <Tuple <AgentInformation, T> > ObserveAnyWithAgentInfo <TAgent, T>(string propertyName, ExecutionScopeOptions scope = ExecutionScopeOptions.All, bool ignoreAgentState = false) where TAgent : IAgent { if (string.IsNullOrEmpty(propertyName)) { throw new ArgumentNullException("propertyName"); } return(ObserveFirstUnsafeWithAgentInfo <TAgent, T>(propertyName, scope, ignoreAgentState) .Select( t => { if (!t.Item2.IsLeft) { throw t.Item2.Right; } else { return Tuple.Create(t.Item1, t.Item2.Left); } }) .Retry()); }
public IObservable <T> ObserveFirst <TAgent, T>(string propertyName, ExecutionScopeOptions scope = ExecutionScopeOptions.All, bool ignoreAgentState = false) where TAgent : IAgent { return(ObserveFirstUnsafe <TAgent, T>(propertyName, scope, ignoreAgentState).SelectLeft(left => left)); }
private IObservable <Tuple <AgentInformation, Either <T, Exception> > > ObserveFirstUnsafeWithAgentInfo <TAgent, T>(string propertyName, ExecutionScopeOptions scope = ExecutionScopeOptions.All, bool ignoreAgentState = false) where TAgent : IAgent { if (string.IsNullOrEmpty(propertyName)) { throw new ArgumentNullException("propertyName"); } return(ObserveAllUnsafe <TAgent, T>(propertyName, scope, ignoreAgentState) .Take(1) .SelectMany(t => t.Item2.Select(data => Tuple.Create(t.Item1, data)))); }
public IObservable <Tuple <AgentInformation, T> > ObserveFirstWithAgentInfo <TAgent, T>(string propertyName, ExecutionScopeOptions scope = ExecutionScopeOptions.All, bool ignoreAgentState = false) where TAgent : IAgent { return(ObserveFirstUnsafeWithAgentInfo <TAgent, T>(propertyName, scope, ignoreAgentState) .Where(t => t.Item2.IsLeft) .Select(t => Tuple.Create(t.Item1, t.Item2.Left))); }
private IObservable <Either <T, Exception> > ObserveFirstUnsafe <TAgent, T>(string propertyName, ExecutionScopeOptions scope = ExecutionScopeOptions.All, bool ignoreAgentState = false) where TAgent : IAgent { if (string.IsNullOrEmpty(propertyName)) { throw new ArgumentNullException("propertyName"); } return(ObserveFirstUnsafeWithAgentInfo <TAgent, T>(propertyName, scope, ignoreAgentState) .Select(t => t.Item2)); }