Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/Microsoft/vsts-agent
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanmacfarlane committed May 5, 2016
2 parents ad019f7 + 5fc068a commit 428a78b
Showing 1 changed file with 44 additions and 6 deletions.
50 changes: 44 additions & 6 deletions src/Agent.Worker/JobRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ public async Task<TaskResult> RunAsync(JobRequestMessage message, CancellationTo

// Setup the job server and job server queue.
var jobServer = HostContext.GetService<IJobServer>();
await jobServer.ConnectAsync(ApiUtil.GetVssConnection(message));
var jobServerCredential = ApiUtil.GetVssCredential(message.Environment.SystemConnection);
var jobConnection = ApiUtil.CreateConnection(ReplaceWithConfigUriBase(message.Environment.SystemConnection.Url), jobServerCredential);
await jobServer.ConnectAsync(jobConnection);

var jobServerQueue = HostContext.GetService<IJobServerQueue>();
jobServerQueue.Start(message);
Expand Down Expand Up @@ -67,17 +69,21 @@ public async Task<TaskResult> RunAsync(JobRequestMessage message, CancellationTo
// prefer task definitions url, then TFS url
var taskServer = HostContext.GetService<ITaskServer>();
string taskUrl = jobContext.Variables.System_TaskDefinitionsUri;
Uri taskServerUri;
if (string.IsNullOrEmpty(taskUrl))
{
Trace.Info("Creating task server with tfs server url");
await taskServer.ConnectAsync(ApiUtil.GetVssConnection(message));
taskServerUri = ReplaceWithConfigUriBase(message.Environment.SystemConnection.Url);
Trace.Info($"Creating task server with tfs server url {taskServerUri.ToString()}");
}
else
{
Trace.Info($"Creating task server with {taskUrl}");
await taskServer.ConnectAsync(ApiUtil.CreateConnection(new Uri(taskUrl), ApiUtil.GetVssCredential(message.Environment.SystemConnection)));
taskServerUri = ReplaceWithConfigUriBase(new Uri(taskUrl));
Trace.Info($"Creating task server with {taskServerUri.ToString()}");
}

var taskServerCredential = ApiUtil.GetVssCredential(message.Environment.SystemConnection);
await taskServer.ConnectAsync(ApiUtil.CreateConnection(taskServerUri, taskServerCredential));

// Expand the endpoint data values.
foreach (ServiceEndpoint endpoint in jobContext.Endpoints)
{
Expand Down Expand Up @@ -158,7 +164,7 @@ public async Task<TaskResult> RunAsync(JobRequestMessage message, CancellationTo
{
await stepsRunner.RunAsync(jobContext, steps);
}
catch (OperationCanceledException ex)
catch (OperationCanceledException ex)
{
// set the job to canceled
Trace.Error($"Caught exception: {ex}");
Expand Down Expand Up @@ -196,5 +202,37 @@ public async Task<TaskResult> RunAsync(JobRequestMessage message, CancellationTo
}
}
}

// the hostname (how the agent knows the server) is external to our server
// in other words, an agent may have it's own way (DNS, hostname) of refering
// to the server. it owns that. That's the hostname we will use
private Uri ReplaceWithConfigUriBase(Uri messageUri)
{
AgentSettings settings = HostContext.GetService<IConfigurationStore>().GetSettings();
try
{
var configUri = new Uri(settings.ServerUrl);
Uri result = null;
var configBaseUri = new Uri(configUri.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped));
if (Uri.TryCreate(configBaseUri, messageUri.PathAndQuery, out result))
{
//replace the schema and host portion of messageUri with the host from the
//server URI (which was set at config time)
return result;
}
}
catch (InvalidOperationException ex)
{
//cannot parse the Uri - not a fatal error
Trace.Error(ex);
}
catch (UriFormatException ex)
{
//cannot parse the Uri - not a fatal error
Trace.Error(ex);
}

return messageUri;
}
}
}

0 comments on commit 428a78b

Please sign in to comment.