Set timeout on sending live console log. (#1903)

This commit is contained in:
Tingluo Huang
2022-05-20 21:31:21 -04:00
committed by GitHub
parent d7694774a4
commit 0b73794267
3 changed files with 18 additions and 14 deletions

View File

@@ -1,5 +1,4 @@
using GitHub.DistributedTask.WebApi; using System;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
@@ -9,6 +8,7 @@ using System.Net.WebSockets;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using GitHub.DistributedTask.WebApi;
using GitHub.Runner.Sdk; using GitHub.Runner.Sdk;
using GitHub.Services.Common; using GitHub.Services.Common;
using GitHub.Services.WebApi; using GitHub.Services.WebApi;
@@ -140,8 +140,8 @@ namespace GitHub.Runner.Common
public void InitializeWebsocketClient(ServiceEndpoint serviceEndpoint) public void InitializeWebsocketClient(ServiceEndpoint serviceEndpoint)
{ {
this._serviceEndpoint = serviceEndpoint; this._serviceEndpoint = serviceEndpoint;
InitializeWebsocketClient(TimeSpan.Zero); InitializeWebsocketClient(TimeSpan.Zero);
} }
public ValueTask DisposeAsync() public ValueTask DisposeAsync()
@@ -163,9 +163,9 @@ namespace GitHub.Runner.Common
private void InitializeWebsocketClient(TimeSpan delay) private void InitializeWebsocketClient(TimeSpan delay)
{ {
if (_serviceEndpoint.Authorization != null && if (_serviceEndpoint.Authorization != null &&
_serviceEndpoint.Authorization.Parameters.TryGetValue(EndpointAuthorizationParameters.AccessToken, out var accessToken) && _serviceEndpoint.Authorization.Parameters.TryGetValue(EndpointAuthorizationParameters.AccessToken, out var accessToken) &&
!string.IsNullOrEmpty(accessToken)) !string.IsNullOrEmpty(accessToken))
{ {
if (_serviceEndpoint.Data.TryGetValue("FeedStreamUrl", out var feedStreamUrl) && !string.IsNullOrEmpty(feedStreamUrl)) if (_serviceEndpoint.Data.TryGetValue("FeedStreamUrl", out var feedStreamUrl) && !string.IsNullOrEmpty(feedStreamUrl))
{ {
@@ -177,7 +177,7 @@ namespace GitHub.Runner.Common
var userAgentValues = new List<ProductInfoHeaderValue>(); var userAgentValues = new List<ProductInfoHeaderValue>();
userAgentValues.AddRange(UserAgentUtility.GetDefaultRestUserAgent()); userAgentValues.AddRange(UserAgentUtility.GetDefaultRestUserAgent());
userAgentValues.AddRange(HostContext.UserAgents); userAgentValues.AddRange(HostContext.UserAgents);
this._websocketClient.Options.SetRequestHeader("User-Agent", string.Join(" ", userAgentValues.Select(x=>x.ToString()))); this._websocketClient.Options.SetRequestHeader("User-Agent", string.Join(" ", userAgentValues.Select(x => x.ToString())));
this._websocketConnectTask = ConnectWebSocketClient(feedStreamUrl, delay); this._websocketConnectTask = ConnectWebSocketClient(feedStreamUrl, delay);
} }
@@ -201,7 +201,7 @@ namespace GitHub.Runner.Common
await this._websocketClient.ConnectAsync(new Uri(feedStreamUrl), default(CancellationToken)); await this._websocketClient.ConnectAsync(new Uri(feedStreamUrl), default(CancellationToken));
Trace.Info($"Successfully started websocket client."); Trace.Info($"Successfully started websocket client.");
} }
catch(Exception ex) catch (Exception ex)
{ {
Trace.Info("Exception caught during websocket client connect, fallback of HTTP would be used now instead of websocket."); Trace.Info("Exception caught during websocket client connect, fallback of HTTP would be used now instead of websocket.");
Trace.Error(ex); Trace.Error(ex);
@@ -231,7 +231,7 @@ namespace GitHub.Runner.Common
// ...in other words, if websocket client is null, we will skip sending to websocket and just use rest api calls to send data // ...in other words, if websocket client is null, we will skip sending to websocket and just use rest api calls to send data
if (_websocketClient != null) if (_websocketClient != null)
{ {
var linesWrapper = startLine.HasValue? new TimelineRecordFeedLinesWrapper(stepId, lines, startLine.Value): new TimelineRecordFeedLinesWrapper(stepId, lines); var linesWrapper = startLine.HasValue ? new TimelineRecordFeedLinesWrapper(stepId, lines, startLine.Value) : new TimelineRecordFeedLinesWrapper(stepId, lines);
var jsonData = StringUtil.ConvertToJson(linesWrapper); var jsonData = StringUtil.ConvertToJson(linesWrapper);
try try
{ {
@@ -242,7 +242,7 @@ namespace GitHub.Runner.Common
{ {
var lastChunk = i + (1 * 1024) >= jsonDataBytes.Length; var lastChunk = i + (1 * 1024) >= jsonDataBytes.Length;
var chunk = new ArraySegment<byte>(jsonDataBytes, i, Math.Min(1 * 1024, jsonDataBytes.Length - i)); var chunk = new ArraySegment<byte>(jsonDataBytes, i, Math.Min(1 * 1024, jsonDataBytes.Length - i));
await _websocketClient.SendAsync(chunk, WebSocketMessageType.Text, endOfMessage:lastChunk, cancellationToken); await _websocketClient.SendAsync(chunk, WebSocketMessageType.Text, endOfMessage: lastChunk, cancellationToken);
} }
pushedLinesViaWebsocket = true; pushedLinesViaWebsocket = true;
@@ -274,7 +274,7 @@ namespace GitHub.Runner.Common
} }
} }
if (!pushedLinesViaWebsocket) if (!pushedLinesViaWebsocket && !cancellationToken.IsCancellationRequested)
{ {
if (startLine.HasValue) if (startLine.HasValue)
{ {

View File

@@ -299,7 +299,11 @@ namespace GitHub.Runner.Common
{ {
try try
{ {
await _jobServer.AppendTimelineRecordFeedAsync(_scopeIdentifier, _hubName, _planId, _jobTimelineId, _jobTimelineRecordId, stepRecordId, batch.Select(logLine => logLine.Line).ToList(), batch[0].LineNumber, default(CancellationToken)); // Give at most 60s for each request.
using (var timeoutTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(60)))
{
await _jobServer.AppendTimelineRecordFeedAsync(_scopeIdentifier, _hubName, _planId, _jobTimelineId, _jobTimelineRecordId, stepRecordId, batch.Select(logLine => logLine.Line).ToList(), batch[0].LineNumber, timeoutTokenSource.Token);
}
if (_firstConsoleOutputs) if (_firstConsoleOutputs)
{ {

View File

@@ -48,7 +48,7 @@
<Target Name="Test" DependsOnTargets="GenerateConstant"> <Target Name="Test" DependsOnTargets="GenerateConstant">
<Exec Command="dotnet build Test/Test.csproj -c $(BUILDCONFIG) /p:PackageRuntime=$(PackageRuntime)" ConsoleToMSBuild="true" /> <Exec Command="dotnet build Test/Test.csproj -c $(BUILDCONFIG) /p:PackageRuntime=$(PackageRuntime)" ConsoleToMSBuild="true" />
<Exec Command="dotnet test Test/Test.csproj --no-build --logger:trx" ConsoleToMSBuild="true" /> <Exec Command="dotnet test Test/Test.csproj -c $(BUILDCONFIG) --no-build --logger:trx" ConsoleToMSBuild="true" />
</Target> </Target>
<Target Name="Layout" DependsOnTargets="Clean;Build"> <Target Name="Layout" DependsOnTargets="Clean;Build">