Skip to content

Commit

Permalink
Set timeout on sending live console log. (actions#1903)
Browse files Browse the repository at this point in the history
  • Loading branch information
TingluoHuang authored May 21, 2022
1 parent d769477 commit 0b73794
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
24 changes: 12 additions & 12 deletions src/Runner.Common/JobServer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using GitHub.DistributedTask.WebApi;
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand All @@ -9,6 +8,7 @@
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using GitHub.DistributedTask.WebApi;
using GitHub.Runner.Sdk;
using GitHub.Services.Common;
using GitHub.Services.WebApi;
Expand Down Expand Up @@ -140,8 +140,8 @@ private async Task CheckNetworkEndpointsAsync(int attemptsLeft)

public void InitializeWebsocketClient(ServiceEndpoint serviceEndpoint)
{
this._serviceEndpoint = serviceEndpoint;
InitializeWebsocketClient(TimeSpan.Zero);
this._serviceEndpoint = serviceEndpoint;
InitializeWebsocketClient(TimeSpan.Zero);
}

public ValueTask DisposeAsync()
Expand All @@ -163,9 +163,9 @@ private void CheckConnection()

private void InitializeWebsocketClient(TimeSpan delay)
{
if (_serviceEndpoint.Authorization != null &&
_serviceEndpoint.Authorization.Parameters.TryGetValue(EndpointAuthorizationParameters.AccessToken, out var accessToken) &&
!string.IsNullOrEmpty(accessToken))
if (_serviceEndpoint.Authorization != null &&
_serviceEndpoint.Authorization.Parameters.TryGetValue(EndpointAuthorizationParameters.AccessToken, out var accessToken) &&
!string.IsNullOrEmpty(accessToken))
{
if (_serviceEndpoint.Data.TryGetValue("FeedStreamUrl", out var feedStreamUrl) && !string.IsNullOrEmpty(feedStreamUrl))
{
Expand All @@ -177,7 +177,7 @@ private void InitializeWebsocketClient(TimeSpan delay)
var userAgentValues = new List<ProductInfoHeaderValue>();
userAgentValues.AddRange(UserAgentUtility.GetDefaultRestUserAgent());
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);
}
Expand All @@ -201,7 +201,7 @@ private async Task ConnectWebSocketClient(string feedStreamUrl, TimeSpan delay)
await this._websocketClient.ConnectAsync(new Uri(feedStreamUrl), default(CancellationToken));
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.Error(ex);
Expand Down Expand Up @@ -231,7 +231,7 @@ public async Task AppendTimelineRecordFeedAsync(Guid scopeIdentifier, string hub
// ...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)
{
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);
try
{
Expand All @@ -242,7 +242,7 @@ public async Task AppendTimelineRecordFeedAsync(Guid scopeIdentifier, string hub
{
var lastChunk = i + (1 * 1024) >= jsonDataBytes.Length;
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;
Expand Down Expand Up @@ -274,7 +274,7 @@ public async Task AppendTimelineRecordFeedAsync(Guid scopeIdentifier, string hub
}
}

if (!pushedLinesViaWebsocket)
if (!pushedLinesViaWebsocket && !cancellationToken.IsCancellationRequested)
{
if (startLine.HasValue)
{
Expand Down
6 changes: 5 additions & 1 deletion src/Runner.Common/JobServerQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,11 @@ private async Task ProcessWebConsoleLinesQueueAsync(bool runOnce = false)
{
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)
{
Expand Down
2 changes: 1 addition & 1 deletion src/dir.proj
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@

<Target Name="Test" DependsOnTargets="GenerateConstant">
<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 Name="Layout" DependsOnTargets="Clean;Build">
Expand Down

0 comments on commit 0b73794

Please sign in to comment.