Skip to content

Commit

Permalink
Support combined Cache task (#2320)
Browse files Browse the repository at this point in the history
* Support combined Cache task

* Update ID for the combined Cache task

* Removing the prefix on cache hit var

* Added debug statement for caching var if the value exists

* Adding code to return Save Cahce if build is not successful

* Checking if the agent.jobstatus key exists

* PR Comments

* Simplify logic to skip save if job status not successful

* Always set cacheHitVar; either to true or false
  • Loading branch information
johnterickson authored and TingluoHuang committed Jul 3, 2019
1 parent 13a2f48 commit c78bd4a
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ namespace Agent.Plugins.PipelineCache
{
public static class PipelineCachePluginConstants
{
public static readonly Guid SaveCacheTaskId = new Guid("F2333FED-1017-4A2F-AAC6-02A185279E1E");
public static readonly Guid RestoreCacheTaskId = new Guid("D53CCAB4-555E-4494-9D06-11DB043FB4A9");
public static readonly Guid CacheTaskId = new Guid("D53CCAB4-555E-4494-9D06-11DB043FB4A9");
}
}
19 changes: 8 additions & 11 deletions src/Agent.Plugins/PipelineCache/PipelineCacheServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ internal async Task DownloadAsync(
IEnumerable<string> key,
string path,
string salt,
string variableToSetOnHit,
string cacheHitVariable,
CancellationToken cancellationToken)
{
VssConnection connection = context.VssConnection;
Expand All @@ -115,11 +115,7 @@ internal async Task DownloadAsync(
// Send results to CustomerIntelligence
context.PublishTelemetry(area: PipelineArtifactConstants.AzurePipelinesAgent, feature: PipelineArtifactConstants.PipelineCache, record: cacheRecord);

if (result == null)
{
return;
}
else
if (result != null)
{
context.Output($"Manifest ID is: {result.ManifestId.ValueString}");
PipelineCacheActionRecord downloadRecord = clientTelemetry.CreateRecord<PipelineCacheActionRecord>((level, uri, type) =>
Expand All @@ -134,11 +130,12 @@ await clientTelemetry.MeasureActionAsync(
// Send results to CustomerIntelligence
context.PublishTelemetry(area: PipelineArtifactConstants.AzurePipelinesAgent, feature: PipelineArtifactConstants.PipelineCache, record: downloadRecord);

if (!string.IsNullOrEmpty(variableToSetOnHit))
{
context.SetVariable($"{PipelineArtifactConstants.PipelineCache}.{variableToSetOnHit}", "True");
}
Console.WriteLine("Cache restored.");
context.Output("Cache restored.");
}

if (!string.IsNullOrEmpty(cacheHitVariable))
{
context.SetVariable(cacheHitVariable, result != null ? "true" : "false");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ namespace Agent.Plugins.PipelineCache
{
public abstract class PipelineCacheTaskPluginBase : IAgentTaskPlugin
{
public abstract Guid Id { get; }
public Guid Id => PipelineCachePluginConstants.CacheTaskId;

public string Stage => "main";
public abstract String Stage { get; }

public async Task RunAsync(AgentTaskPluginExecutionContext context, CancellationToken token)
{
Expand Down Expand Up @@ -58,7 +58,7 @@ protected static class PipelineCacheTaskPluginConstants
public static readonly string Key = "key"; // this needs to match the input in the task.
public static readonly string Path = "path";
public static readonly string PipelineId = "pipelineId";
public static readonly string VariableToSetOnCacheHit = "cacheHitVar";
public static readonly string CacheHitVariable = "cacheHitVar";
public static readonly string Salt = "salt";

}
Expand Down
20 changes: 3 additions & 17 deletions src/Agent.Plugins/PipelineCache/RestorePipelineCacheV0.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,13 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Threading;
using System.Threading.Tasks;
using Agent.Sdk;
using Microsoft.TeamFoundation.Build.WebApi;
using Microsoft.TeamFoundation.DistributedTask.WebApi;
using Microsoft.VisualStudio.Services.Agent.Util;
using Microsoft.VisualStudio.Services.BlobStore.Common;
using Microsoft.VisualStudio.Services.BlobStore.WebApi;
using Microsoft.VisualStudio.Services.Common;
using Microsoft.VisualStudio.Services.Content.Common.Tracing;
using Microsoft.VisualStudio.Services.PipelineCache.WebApi;
using Microsoft.VisualStudio.Services.WebApi;
using Newtonsoft.Json;

namespace Agent.Plugins.PipelineCache
{
public class RestorePipelineCacheV0 : PipelineCacheTaskPluginBase
{
public override Guid Id => PipelineCachePluginConstants.RestoreCacheTaskId;
public override string Stage => "main";

protected override async Task ProcessCommandInternalAsync(
AgentTaskPluginExecutionContext context,
Expand All @@ -33,15 +20,14 @@ protected override async Task ProcessCommandInternalAsync(
new[] { '\n' },
StringSplitOptions.RemoveEmptyEntries
);
string variableToSetOnHit = context.GetInput(PipelineCacheTaskPluginConstants.VariableToSetOnCacheHit, required: false);

PipelineCacheServer server = new PipelineCacheServer();
await server.DownloadAsync(
context,
key,
path,
salt,
variableToSetOnHit,
context.GetInput(PipelineCacheTaskPluginConstants.CacheHitVariable, required: false),
token);
}
}
Expand Down
31 changes: 17 additions & 14 deletions src/Agent.Plugins/PipelineCache/SavePipelineCacheV0.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,14 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Threading;
using System.Threading.Tasks;
using Agent.Sdk;
using Microsoft.TeamFoundation.Build.WebApi;
using Microsoft.TeamFoundation.DistributedTask.WebApi;
using Microsoft.VisualStudio.Services.Agent.Util;
using Microsoft.VisualStudio.Services.BlobStore.Common;
using Microsoft.VisualStudio.Services.BlobStore.WebApi;
using Microsoft.VisualStudio.Services.Common;
using Microsoft.VisualStudio.Services.Content.Common.Tracing;
using Microsoft.VisualStudio.Services.PipelineCache.WebApi;
using Microsoft.VisualStudio.Services.WebApi;
using Newtonsoft.Json;

namespace Agent.Plugins.PipelineCache
{
public class SavePipelineCacheV0 : PipelineCacheTaskPluginBase
{
public override Guid Id => PipelineCachePluginConstants.SaveCacheTaskId;
public override string Stage => "post";

protected override async Task ProcessCommandInternalAsync(
AgentTaskPluginExecutionContext context,
Expand All @@ -29,6 +17,21 @@ protected override async Task ProcessCommandInternalAsync(
string salt,
CancellationToken token)
{
TaskResult? jobStatus = null;
if (context.Variables.TryGetValue("agent.jobstatus", out VariableValue jobStatusVar))
{
if (Enum.TryParse<TaskResult>(jobStatusVar?.Value ?? string.Empty, true, out TaskResult result))
{
jobStatus = result;
}
}

if (!TaskResult.Succeeded.Equals(jobStatus))
{
context.Warning($"Skipping because the job status was not 'Succeeded'.");
return;
}

string[] key = keyStr.Split(
new[] { '\n' },
StringSplitOptions.RemoveEmptyEntries
Expand Down

0 comments on commit c78bd4a

Please sign in to comment.