2021-04-29 15:35:57 -04:00
|
|
|
// Copyright Epic Games, Inc. All Rights Reserved.
|
|
|
|
|
|
2022-03-23 14:50:23 -04:00
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2021-04-29 15:10:34 -04:00
|
|
|
using Google.Protobuf;
|
|
|
|
|
using Google.Protobuf.WellKnownTypes;
|
2022-06-07 15:53:33 -04:00
|
|
|
using Horde.Build.Agents;
|
|
|
|
|
using Horde.Build.Agents.Leases;
|
|
|
|
|
using Horde.Build.Jobs;
|
|
|
|
|
using Horde.Build.Logs;
|
2022-03-16 11:18:39 -04:00
|
|
|
using Horde.Build.Utilities;
|
2022-03-23 14:50:23 -04:00
|
|
|
using HordeCommon;
|
|
|
|
|
using HordeCommon.Rpc.Tasks;
|
2021-04-29 15:10:34 -04:00
|
|
|
|
2022-06-07 15:53:33 -04:00
|
|
|
namespace Horde.Build.Tasks
|
2021-04-29 15:10:34 -04:00
|
|
|
{
|
2021-10-19 16:12:44 -04:00
|
|
|
using JobId = ObjectId<IJob>;
|
|
|
|
|
using LeaseId = ObjectId<ILease>;
|
2021-10-25 23:38:55 -04:00
|
|
|
using LogId = ObjectId<ILogFile>;
|
2021-10-19 16:12:44 -04:00
|
|
|
|
2021-04-29 15:10:34 -04:00
|
|
|
class RestartTaskSource : TaskSourceBase<RestartTask>
|
|
|
|
|
{
|
2021-10-17 15:30:49 -04:00
|
|
|
public override string Type => "Restart";
|
|
|
|
|
|
2022-01-26 16:37:31 -05:00
|
|
|
public override TaskSourceFlags Flags => TaskSourceFlags.AllowWhenDisabled | TaskSourceFlags.AllowDuringDowntime;
|
|
|
|
|
|
2022-03-23 14:50:23 -04:00
|
|
|
readonly ILogFileService _logService;
|
2021-04-29 15:10:34 -04:00
|
|
|
|
2022-03-23 14:50:23 -04:00
|
|
|
public RestartTaskSource(ILogFileService logService)
|
2021-04-29 15:10:34 -04:00
|
|
|
{
|
2022-03-23 14:50:23 -04:00
|
|
|
_logService = logService;
|
2021-10-25 23:38:55 -04:00
|
|
|
|
2022-03-23 14:50:23 -04:00
|
|
|
OnLeaseStartedProperties.Add(nameof(RestartTask.LogId), x => new LogId(x.LogId));
|
2021-04-29 15:10:34 -04:00
|
|
|
}
|
|
|
|
|
|
2022-03-29 13:49:41 -04:00
|
|
|
public override async Task<Task<AgentLease?>> AssignLeaseAsync(IAgent agent, CancellationToken cancellationToken)
|
2021-04-29 15:10:34 -04:00
|
|
|
{
|
2022-03-23 14:50:23 -04:00
|
|
|
if (!agent.RequestRestart)
|
2021-04-29 15:10:34 -04:00
|
|
|
{
|
2022-03-28 19:03:36 -04:00
|
|
|
return Skip(cancellationToken);
|
2021-04-29 15:10:34 -04:00
|
|
|
}
|
2022-03-23 14:50:23 -04:00
|
|
|
if (agent.Leases.Count > 0)
|
2021-04-29 15:10:34 -04:00
|
|
|
{
|
2022-03-28 19:03:36 -04:00
|
|
|
return await DrainAsync(cancellationToken);
|
2021-04-29 15:10:34 -04:00
|
|
|
}
|
|
|
|
|
|
2022-10-26 07:27:13 -04:00
|
|
|
ILogFile log = await _logService.CreateLogFileAsync(JobId.Empty, agent.SessionId, LogType.Json, cancellationToken: cancellationToken);
|
2021-04-29 15:10:34 -04:00
|
|
|
|
2022-03-23 14:50:23 -04:00
|
|
|
RestartTask task = new RestartTask();
|
|
|
|
|
task.LogId = log.Id.ToString();
|
2021-04-29 15:10:34 -04:00
|
|
|
|
2022-03-23 14:50:23 -04:00
|
|
|
byte[] payload = Any.Pack(task).ToByteArray();
|
2021-04-29 15:10:34 -04:00
|
|
|
|
2022-03-28 19:03:36 -04:00
|
|
|
return Lease(new AgentLease(LeaseId.GenerateNewId(), "Restart", null, null, log.Id, LeaseState.Pending, null, true, payload));
|
2021-04-29 15:10:34 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|