Skip to content

Commit 3b8475d

Browse files
authored
Skip adding line to console line queue if the queue is backed up. (#1592)
* Skip adding line to console line queue if the queue is backed up. * .
1 parent ba9766d commit 3b8475d

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

src/Runner.Common/JobServerQueue.cs

+18-12
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
using GitHub.DistributedTask.WebApi;
2-
using GitHub.Runner.Common.Util;
31
using System;
4-
using System.Collections.Generic;
52
using System.Collections.Concurrent;
3+
using System.Collections.Generic;
64
using System.IO;
75
using System.Linq;
86
using System.Threading;
97
using System.Threading.Tasks;
10-
using Pipelines = GitHub.DistributedTask.Pipelines;
8+
using GitHub.DistributedTask.WebApi;
119
using GitHub.Runner.Sdk;
10+
using Pipelines = GitHub.DistributedTask.Pipelines;
1211

1312
namespace GitHub.Runner.Common
1413
{
@@ -76,6 +75,7 @@ public sealed class JobServerQueue : RunnerService, IJobServerQueue
7675
// at the same time we can cut the load to server after the build run for more than 60s
7776
private int _webConsoleLineAggressiveDequeueCount = 0;
7877
private const int _webConsoleLineAggressiveDequeueLimit = 4 * 60;
78+
private const int _webConsoleLineQueueSizeLimit = 1024;
7979
private bool _webConsoleLineAggressiveDequeue = true;
8080
private bool _firstConsoleOutputs = true;
8181

@@ -161,8 +161,20 @@ public async Task ShutdownAsync()
161161

162162
public void QueueWebConsoleLine(Guid stepRecordId, string line, long? lineNumber)
163163
{
164-
Trace.Verbose("Enqueue web console line queue: {0}", line);
165-
_webConsoleLineQueue.Enqueue(new ConsoleLineInfo(stepRecordId, line, lineNumber));
164+
// We only process 500 lines of the queue everytime.
165+
// If the queue is backing up due to slow Http request or flood of output from step,
166+
// we will drop the output to avoid extra memory consumption from the runner since the live console feed is best effort.
167+
if (!string.IsNullOrEmpty(line) && _webConsoleLineQueue.Count < _webConsoleLineQueueSizeLimit)
168+
{
169+
Trace.Verbose("Enqueue web console line queue: {0}", line);
170+
if (line.Length > 1024)
171+
{
172+
Trace.Verbose("Web console line is more than 1024 chars, truncate to first 1024 chars");
173+
line = $"{line.Substring(0, 1024)}...";
174+
}
175+
176+
_webConsoleLineQueue.Enqueue(new ConsoleLineInfo(stepRecordId, line, lineNumber));
177+
}
166178
}
167179

168180
public void QueueFileUpload(Guid timelineId, Guid timelineRecordId, string type, string name, string path, bool deleteSource)
@@ -230,12 +242,6 @@ private async Task ProcessWebConsoleLinesQueueAsync(bool runOnce = false)
230242
stepRecordIds.Add(lineInfo.StepRecordId);
231243
}
232244

233-
if (!string.IsNullOrEmpty(lineInfo.Line) && lineInfo.Line.Length > 1024)
234-
{
235-
Trace.Verbose("Web console line is more than 1024 chars, truncate to first 1024 chars");
236-
lineInfo.Line = $"{lineInfo.Line.Substring(0, 1024)}...";
237-
}
238-
239245
stepsConsoleLines[lineInfo.StepRecordId].Add(new TimelineRecordLogLine(lineInfo.Line, lineInfo.LineNumber));
240246
linesCounter++;
241247

0 commit comments

Comments
 (0)