Skip to content

Commit

Permalink
file container trace. (#477)
Browse files Browse the repository at this point in the history
  • Loading branch information
TingluoHuang authored Aug 7, 2016
1 parent 8f1aa53 commit fd50b21
Show file tree
Hide file tree
Showing 8 changed files with 246 additions and 74 deletions.
2 changes: 1 addition & 1 deletion src/Agent.Listener/_project.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"version": "1.0.0-*"
},
"Microsoft.Win32.Registry": "4.0.0",
"vss-api-netcore": "0.5.22-private",
"vss-api-netcore": "0.5.23-private",
"System.ServiceProcess.ServiceController": "4.1.0",
"System.IO.Compression.ZipFile": "4.0.1",
"System.IO.FileSystem.AccessControl": "4.0.0",
Expand Down
50 changes: 45 additions & 5 deletions src/Agent.Worker/AsyncCommandContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,32 @@ public interface IAsyncCommandContext : IAgentService
Task Task { get; set; }
void InitializeCommandContext(IExecutionContext context, string name);
void Output(string message);
void Debug(string message);
Task WaitAsync();
}

public class AsyncCommandContext : AgentService, IAsyncCommandContext
{
private class OutputMessage
{
public OutputMessage(OutputType type, string message)
{
Type = type;
Message = message;
}

public OutputType Type { get; }
public String Message { get; }
}

private enum OutputType
{
Info,
Debug,
}

private IExecutionContext _executionContext;
private readonly ConcurrentQueue<string> _outputQueue = new ConcurrentQueue<string>();
private readonly ConcurrentQueue<OutputMessage> _outputQueue = new ConcurrentQueue<OutputMessage>();

public string Name { get; private set; }
public Task Task { get; set; }
Expand All @@ -30,7 +49,12 @@ public void InitializeCommandContext(IExecutionContext context, string name)

public void Output(string message)
{
_outputQueue.Enqueue(message);
_outputQueue.Enqueue(new OutputMessage(OutputType.Info, message));
}

public void Debug(string message)
{
_outputQueue.Enqueue(new OutputMessage(OutputType.Debug, message));
}

public async Task WaitAsync()
Expand All @@ -39,12 +63,20 @@ public async Task WaitAsync()
// start flushing output queue
Trace.Info("Start flush buffered output.");
_executionContext.Section($"Async Command Start: {Name}");
string output;
OutputMessage output;
while (!this.Task.IsCompleted)
{
while (_outputQueue.TryDequeue(out output))
{
_executionContext.Output(output);
switch (output.Type)
{
case OutputType.Info:
_executionContext.Output(output.Message);
break;
case OutputType.Debug:
_executionContext.Debug(output.Message);
break;
}
}

await Task.WhenAny(Task.Delay(TimeSpan.FromMilliseconds(500)), this.Task);
Expand All @@ -54,7 +86,15 @@ public async Task WaitAsync()
Trace.Verbose("Command task has finished, flush out all remaining buffered output.");
while (_outputQueue.TryDequeue(out output))
{
_executionContext.Output(output);
switch (output.Type)
{
case OutputType.Info:
_executionContext.Output(output.Message);
break;
case OutputType.Debug:
_executionContext.Debug(output.Message);
break;
}
}

_executionContext.Section($"Async Command End: {Name}");
Expand Down
Loading

0 comments on commit fd50b21

Please sign in to comment.