public AgentConnection(Distributed.Dispatcher dispatcher, EndpointConnectionInfo info) { using (Trace.Log()) { connection = new HubConnection($"http://{info.signalrUrl}/", dispatcher.Identifier, dispatcher.SignalrUrl, dispatcher.WebUrl, "DispatcherHub"); connection.StateChanged += s => { currentState = s.NewState; StateChanged?.Invoke(s); if (currentState == ConnectionState.Disconnected) { if (s.OldState == ConnectionState.Connecting) { if (retryCount++ > MaxRetryCount) { connection.Stop(); dispatcher.ReleaseAgent(info.name); } } if (disposed) { stopped.Set(); } } }; connection.Proxy.On <string, bool>("SetAgentState", (id, active) => SetAgentState?.Invoke(id, active)); connection.Proxy.On <TaskItem, TaskResult>("CompleteTask", (taskItem, taskResult) => TaskCompleted?.Invoke(taskItem, taskResult)); } }
private async void InitializeForJobAsync(Job job) { using (Trace.Log()) { initialized = false; activeJob = job; InitializationResult initResult; try { initResult = await connection.Initialize(job.Config).ConfigureAwait(true); if (initResult.success) { Console.WriteLine($"Agent initialized {Identifier}"); initialized = true; Capacity = initResult.capacity; CapacityChanged?.Invoke(this, Capacity); RequestTasks(); return; } } catch (Exception e) { initResult = new InitializationResult { success = false, errorMessage = e.ToString() }; } Console.WriteLine($"Agent initialization error {Identifier}"); Console.WriteLine(initResult.errorMessage); dispatcher.ReleaseAgent(Identifier); } }