mirror of
https://github.com/actions/runner.git
synced 2025-12-10 12:36:23 +00:00
128 lines
6.9 KiB
C#
128 lines
6.9 KiB
C#
using GitHub.DistributedTask.WebApi;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using GitHub.Services.WebApi;
|
|
|
|
namespace GitHub.Runner.Common
|
|
{
|
|
[ServiceLocator(Default = typeof(JobServer))]
|
|
public interface IJobServer : IRunnerService
|
|
{
|
|
Task ConnectAsync(VssConnection jobConnection);
|
|
|
|
// logging and console
|
|
Task<TaskLog> AppendLogContentAsync(Guid scopeIdentifier, string hubName, Guid planId, int logId, Stream uploadStream, CancellationToken cancellationToken);
|
|
Task AppendTimelineRecordFeedAsync(Guid scopeIdentifier, string hubName, Guid planId, Guid timelineId, Guid timelineRecordId, Guid stepId, IList<string> lines, CancellationToken cancellationToken);
|
|
Task<TaskAttachment> CreateAttachmentAsync(Guid scopeIdentifier, string hubName, Guid planId, Guid timelineId, Guid timelineRecordId, String type, String name, Stream uploadStream, CancellationToken cancellationToken);
|
|
Task<TaskLog> CreateLogAsync(Guid scopeIdentifier, string hubName, Guid planId, TaskLog log, CancellationToken cancellationToken);
|
|
Task<Timeline> CreateTimelineAsync(Guid scopeIdentifier, string hubName, Guid planId, Guid timelineId, CancellationToken cancellationToken);
|
|
Task<List<TimelineRecord>> UpdateTimelineRecordsAsync(Guid scopeIdentifier, string hubName, Guid planId, Guid timelineId, IEnumerable<TimelineRecord> records, CancellationToken cancellationToken);
|
|
Task RaisePlanEventAsync<T>(Guid scopeIdentifier, string hubName, Guid planId, T eventData, CancellationToken cancellationToken) where T : JobEvent;
|
|
Task<Timeline> GetTimelineAsync(Guid scopeIdentifier, string hubName, Guid planId, Guid timelineId, CancellationToken cancellationToken);
|
|
Task<ActionDownloadInfoCollection> ResolveActionDownloadInfoAsync(Guid scopeIdentifier, string hubName, Guid planId, ActionReferenceList actions, CancellationToken cancellationToken);
|
|
}
|
|
|
|
public sealed class JobServer : RunnerService, IJobServer
|
|
{
|
|
private bool _hasConnection;
|
|
private VssConnection _connection;
|
|
private TaskHttpClient _taskClient;
|
|
|
|
public async Task ConnectAsync(VssConnection jobConnection)
|
|
{
|
|
_connection = jobConnection;
|
|
int attemptCount = 5;
|
|
while (!_connection.HasAuthenticated && attemptCount-- > 0)
|
|
{
|
|
try
|
|
{
|
|
await _connection.ConnectAsync();
|
|
break;
|
|
}
|
|
catch (Exception ex) when (attemptCount > 0)
|
|
{
|
|
Trace.Info($"Catch exception during connect. {attemptCount} attemp left.");
|
|
Trace.Error(ex);
|
|
}
|
|
|
|
await Task.Delay(100);
|
|
}
|
|
|
|
_taskClient = _connection.GetClient<TaskHttpClient>();
|
|
_hasConnection = true;
|
|
}
|
|
|
|
private void CheckConnection()
|
|
{
|
|
if (!_hasConnection)
|
|
{
|
|
throw new InvalidOperationException("SetConnection");
|
|
}
|
|
}
|
|
|
|
//-----------------------------------------------------------------
|
|
// Feedback: WebConsole, TimelineRecords and Logs
|
|
//-----------------------------------------------------------------
|
|
|
|
public Task<TaskLog> AppendLogContentAsync(Guid scopeIdentifier, string hubName, Guid planId, int logId, Stream uploadStream, CancellationToken cancellationToken)
|
|
{
|
|
CheckConnection();
|
|
return _taskClient.AppendLogContentAsync(scopeIdentifier, hubName, planId, logId, uploadStream, cancellationToken: cancellationToken);
|
|
}
|
|
|
|
public Task AppendTimelineRecordFeedAsync(Guid scopeIdentifier, string hubName, Guid planId, Guid timelineId, Guid timelineRecordId, Guid stepId, IList<string> lines, CancellationToken cancellationToken)
|
|
{
|
|
CheckConnection();
|
|
return _taskClient.AppendTimelineRecordFeedAsync(scopeIdentifier, hubName, planId, timelineId, timelineRecordId, stepId, lines, cancellationToken: cancellationToken);
|
|
}
|
|
|
|
public Task<TaskAttachment> CreateAttachmentAsync(Guid scopeIdentifier, string hubName, Guid planId, Guid timelineId, Guid timelineRecordId, string type, string name, Stream uploadStream, CancellationToken cancellationToken)
|
|
{
|
|
CheckConnection();
|
|
return _taskClient.CreateAttachmentAsync(scopeIdentifier, hubName, planId, timelineId, timelineRecordId, type, name, uploadStream, cancellationToken: cancellationToken);
|
|
}
|
|
|
|
public Task<TaskLog> CreateLogAsync(Guid scopeIdentifier, string hubName, Guid planId, TaskLog log, CancellationToken cancellationToken)
|
|
{
|
|
CheckConnection();
|
|
return _taskClient.CreateLogAsync(scopeIdentifier, hubName, planId, log, cancellationToken: cancellationToken);
|
|
}
|
|
|
|
public Task<Timeline> CreateTimelineAsync(Guid scopeIdentifier, string hubName, Guid planId, Guid timelineId, CancellationToken cancellationToken)
|
|
{
|
|
CheckConnection();
|
|
return _taskClient.CreateTimelineAsync(scopeIdentifier, hubName, planId, new Timeline(timelineId), cancellationToken: cancellationToken);
|
|
}
|
|
|
|
public Task<List<TimelineRecord>> UpdateTimelineRecordsAsync(Guid scopeIdentifier, string hubName, Guid planId, Guid timelineId, IEnumerable<TimelineRecord> records, CancellationToken cancellationToken)
|
|
{
|
|
CheckConnection();
|
|
return _taskClient.UpdateTimelineRecordsAsync(scopeIdentifier, hubName, planId, timelineId, records, cancellationToken: cancellationToken);
|
|
}
|
|
|
|
public Task RaisePlanEventAsync<T>(Guid scopeIdentifier, string hubName, Guid planId, T eventData, CancellationToken cancellationToken) where T : JobEvent
|
|
{
|
|
CheckConnection();
|
|
return _taskClient.RaisePlanEventAsync(scopeIdentifier, hubName, planId, eventData, cancellationToken: cancellationToken);
|
|
}
|
|
|
|
public Task<Timeline> GetTimelineAsync(Guid scopeIdentifier, string hubName, Guid planId, Guid timelineId, CancellationToken cancellationToken)
|
|
{
|
|
CheckConnection();
|
|
return _taskClient.GetTimelineAsync(scopeIdentifier, hubName, planId, timelineId, includeRecords: true, cancellationToken: cancellationToken);
|
|
}
|
|
|
|
//-----------------------------------------------------------------
|
|
// Action download info
|
|
//-----------------------------------------------------------------
|
|
public Task<ActionDownloadInfoCollection> ResolveActionDownloadInfoAsync(Guid scopeIdentifier, string hubName, Guid planId, ActionReferenceList actions, CancellationToken cancellationToken)
|
|
{
|
|
CheckConnection();
|
|
return _taskClient.ResolveActionDownloadInfoAsync(scopeIdentifier, hubName, planId, actions, cancellationToken: cancellationToken);
|
|
}
|
|
}
|
|
}
|