Skip to content
This repository has been archived by the owner on Feb 2, 2021. It is now read-only.

Commit

Permalink
Updated based on PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mikary committed Oct 27, 2014
1 parent 72a8e25 commit ad1883b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 27 deletions.
12 changes: 9 additions & 3 deletions src/Microsoft.AspNet.SignalR.DCrank.Crank/Agent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public void StopWorker(int workerId)
}
}

public void StopWorkers()
public async Task StopWorkers()
{
var keys = _workers.Keys.ToList();

Expand All @@ -182,12 +182,18 @@ public void StopWorkers()
AgentWorker worker;
if (_workers.TryGetValue(key, out worker))
{
worker.Worker.Stop();
Runner.LogAgent("Agent stopped Worker {0}.", key);
await worker.Worker.Stop();
await Runner.LogAgent("Agent stopped Worker {0}.", key);
}
}
TotalConnectionsRequested = 0;
ApplyingLoad = false;

// Wait for workers to terminate
while (_workers.Count > 0)
{
await Task.Delay(1000);
}
}

public async Task Pong(int id, int value)
Expand Down
22 changes: 8 additions & 14 deletions src/Microsoft.AspNet.SignalR.DCrank.Crank/CommandLineRunner.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -35,28 +36,21 @@ public async Task Run()
var writeStatusTask = WriteConnectionStatus(writeStatusCts.Token);

// Wait until all connections are connected
var connectedConnections = _agent.GetWorkerStatus().Select(status => status.Value.ConnectedCount).Sum();
while (connectedConnections < _agent.TotalConnectionsRequested)
while (_agent.GetWorkerStatus().Aggregate(0, (state, status) => state + status.Value.ConnectedCount) <
_agent.TotalConnectionsRequested)

This comment has been minimized.

Copy link
@moozzyk

moozzyk Oct 29, 2014

nit: call it runningConnectedConnectionsSum or something along this lines instead of state - state does not mean anything...

{
await Task.Delay(1000);
connectedConnections = _agent.GetWorkerStatus().Select(status => status.Value.ConnectedCount).Sum();
}

// Stay connected for the duration of the send phase
await Task.Delay(TimeSpan.FromSeconds(_sendDurationSeconds));

// Disconnect
_agent.StopWorkers();

// Wait for workers to terminate
while (_agent.GetWorkerStatus().Count > 0)
{
await Task.Delay(1000);
}
await _agent.StopWorkers();

// Stop writing worker status information
writeStatusCts.Cancel();
writeStatusTask.Wait();
await writeStatusTask;
}

private async Task WriteConnectionStatus(CancellationToken cancellationToken)
Expand All @@ -68,7 +62,7 @@ await Task.Run(async () =>
var statusDictionary = _agent.GetWorkerStatus();
foreach (var key in statusDictionary.Keys)
{
Console.WriteLine("({0}) {1}", key, JsonConvert.SerializeObject(statusDictionary[key]));
Trace.WriteLine(string.Format("({0}) {1}", key, JsonConvert.SerializeObject(statusDictionary[key])));
}
await Task.Delay(1000);
}
Expand All @@ -82,12 +76,12 @@ public Task PongWorker(int workerId, int value)

public async Task LogAgent(string format, params object[] arguments)
{
Console.WriteLine(format, arguments);
Trace.WriteLine(string.Format(format, arguments));
}

public async Task LogWorker(int workerId, string format, params object[] arguments)
{
Console.WriteLine("({0}) {1}", workerId, string.Format(format, arguments));
Trace.WriteLine(string.Format("({0}) {1}", workerId, string.Format(format, arguments)));
}
}
}
33 changes: 23 additions & 10 deletions src/Microsoft.AspNet.SignalR.DCrank.Crank/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Threading;
using CmdLine;

namespace Microsoft.AspNet.SignalR.DCrank.Crank
Expand All @@ -14,19 +13,13 @@ static void Main(string[] args)
switch (arguments.Mode.ToLowerInvariant())
{
case "commandline":
var commandLineAgent = new Agent();
var commandLineRunner = new CommandLineRunner(commandLineAgent, arguments);
commandLineRunner.Run().Wait();
StartCommandLine(arguments);
break;
case "agent":
var hubAgent = new Agent();
var hubRunner = new HubRunner(hubAgent, arguments.ControllerUrl);
hubRunner.Run().Wait();
StartAgent(arguments);
break;
case "worker":
ThreadPool.SetMinThreads(32, 32);
var worker = new Worker(arguments.ParentPid);
worker.Run().Wait();
StartWorker(arguments);
break;
default:
throw new ArgumentException(string.Format("Invalid value for Mode \"{0}\"", arguments.Mode));
Expand All @@ -42,5 +35,25 @@ static void Main(string[] args)
Console.WriteLine(ex.Message);
}
}

private static void StartCommandLine(DCrankArguments arguments)
{
var agent = new Agent();
var runner = new CommandLineRunner(agent, arguments);
runner.Run().Wait();
}

private static void StartAgent(DCrankArguments arguments)
{
var agent = new Agent();
var runner = new HubRunner(agent, arguments.ControllerUrl);
runner.Run().Wait();
}

private static void StartWorker(DCrankArguments arguments)
{
var worker = new Worker(arguments.ParentPid);
worker.Run().Wait();
}
}
}

0 comments on commit ad1883b

Please sign in to comment.