Skip to content

Commit

Permalink
Moved logic to drain queues after each task under knob "AGENT_DRAIN_Q…
Browse files Browse the repository at this point in the history
…UEUES_AFTER_TASK" (#4176)

* Added knob "DrainQueuesAfterTask" with default "false"

* move logic to drain queues behind knob DrainQueuesAfterTask

* added mocking of GetScopedEnvironment method in step execution context

* added mocking of GetScopedEnvironment method in step execution context

* changed default value for knob "AGENT_DRAIN_QUEUES_AFTER_TASK" to true

* implementd AsBooleanStrict and check in JobRunner

* Revert "implementd AsBooleanStrict and check in JobRunner"

This reverts commit a1c0c67.

* Reversed the knob
  • Loading branch information
sergey-koryshev authored Mar 7, 2023
1 parent c0bd968 commit 84a6f86
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
7 changes: 7 additions & 0 deletions src/Agent.Sdk/Knob/AgentKnobs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -410,5 +410,12 @@ public class AgentKnobs
new RuntimeKnobSource("AGENT_USE_NODE"),
new EnvironmentKnobSource("AGENT_USE_NODE"),
new BuiltInDefaultKnobSource(string.Empty));

public static readonly Knob DisableDrainQueuesAfterTask = new Knob(
nameof(DisableDrainQueuesAfterTask),
"Forces the agent to disable draining queues after each task",
new RuntimeKnobSource("AGENT_DISABLE_DRAIN_QUEUES_AFTER_TASK"),
new EnvironmentKnobSource("AGENT_DISABLE_DRAIN_QUEUES_AFTER_TASK"),
new BuiltInDefaultKnobSource("false"));
}
}
22 changes: 13 additions & 9 deletions src/Agent.Worker/StepsRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Microsoft.TeamFoundation.DistributedTask.Expressions;
using Pipelines = Microsoft.TeamFoundation.DistributedTask.Pipelines;
using Microsoft.VisualStudio.Services.CircuitBreaker;
using Agent.Sdk.Knob;

namespace Microsoft.VisualStudio.Services.Agent.Worker
{
Expand Down Expand Up @@ -309,16 +310,19 @@ private async Task RunStepAsync(IStep step, CancellationToken jobCancellationTok
step.ExecutionContext.Section(StringUtil.Loc("StepFinishing", step.DisplayName));
step.ExecutionContext.Complete();

try
{
// We need to drain the queues after a task just in case if
// there are a lot of items since it can cause some UI hangs.
await JobServerQueue.DrainQueues();
}
catch (Exception ex)
if (!AgentKnobs.DisableDrainQueuesAfterTask.GetValue(step.ExecutionContext).AsBoolean())
{
Trace.Error($"Error has occurred while draining queues, it can cause some UI glitches but it doesn't affect a pipeline execution itself: {ex}");
step.ExecutionContext.Error(ex);
try
{
// We need to drain the queues after a task just in case if
// there are a lot of items since it can cause some UI hangs.
await JobServerQueue.DrainQueues();
}
catch (Exception ex)
{
Trace.Error($"Error has occurred while draining queues, it can cause some UI glitches but it doesn't affect a pipeline execution itself: {ex}");
step.ExecutionContext.Error(ex);
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/Test/L0/Worker/StepsRunnerL0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Xunit;
using Microsoft.TeamFoundation.DistributedTask.Expressions;
using Pipelines = Microsoft.TeamFoundation.DistributedTask.Pipelines;
using Agent.Sdk;

namespace Microsoft.VisualStudio.Services.Agent.Tests.Worker
{
Expand Down Expand Up @@ -426,6 +427,7 @@ public async Task SetStepTarget()
stepContext.Object.Result = r;
}
});
stepContext.Setup(x => x.GetScopedEnvironment()).Returns(new SystemEnvironment());
step.Setup(x => x.ExecutionContext).Returns(stepContext.Object);

// Act.
Expand Down Expand Up @@ -460,6 +462,7 @@ private Mock<IStep> CreateStep(TaskResult result, IExpressionNode condition, Boo
}
});
stepContext.Object.Result = result;
stepContext.Setup(x => x.GetScopedEnvironment()).Returns(new SystemEnvironment());
step.Setup(x => x.ExecutionContext).Returns(stepContext.Object);

return step;
Expand Down

0 comments on commit 84a6f86

Please sign in to comment.