-
Notifications
You must be signed in to change notification settings - Fork 946
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Upload unfinished job/step logs from runner on cancellation timeout #255
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -568,6 +568,10 @@ await processChannel.SendAsync( | |
{ | ||
Trace.Info("worker process has been killed."); | ||
} | ||
|
||
// When worker doesn't exit within cancel timeout, the runner will kill the worker process and worker won't finish upload job logs. | ||
// The runner will try to upload these logs at this time. | ||
await TryUploadUnfinishedLogs(message); | ||
} | ||
|
||
Trace.Info($"finish job request for job {message.JobId} with result: {resultOnAbandonOrCancel}"); | ||
|
@@ -712,6 +716,116 @@ public async Task RenewJobRequestAsync(int poolId, long requestId, Guid lockToke | |
} | ||
} | ||
|
||
// Best effort upload any logs for this job. | ||
private async Task TryUploadUnfinishedLogs(Pipelines.AgentJobRequestMessage message) | ||
{ | ||
Trace.Entering(); | ||
|
||
var logFolder = Path.Combine(HostContext.GetDirectory(WellKnownDirectory.Diag), PagingLogger.PagingFolder); | ||
if (!Directory.Exists(logFolder)) | ||
{ | ||
return; | ||
} | ||
|
||
var logs = Directory.GetFiles(logFolder); | ||
if (logs.Length == 0) | ||
{ | ||
return; | ||
} | ||
|
||
try | ||
{ | ||
var systemConnection = message.Resources.Endpoints.SingleOrDefault(x => string.Equals(x.Name, WellKnownServiceEndpointNames.SystemVssConnection)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pull the cred from the job message, we will use this cred to talk to actions service and upload the log. |
||
ArgUtil.NotNull(systemConnection, nameof(systemConnection)); | ||
|
||
var jobServer = HostContext.GetService<IJobServer>(); | ||
VssCredentials jobServerCredential = VssUtil.GetVssCredential(systemConnection); | ||
VssConnection jobConnection = VssUtil.CreateConnection(systemConnection.Url, jobServerCredential); | ||
|
||
await jobServer.ConnectAsync(jobConnection); | ||
|
||
var timeline = await jobServer.GetTimelineAsync(message.Plan.ScopeIdentifier, message.Plan.PlanType, message.Plan.PlanId, message.Timeline.Id, CancellationToken.None); | ||
|
||
var updatedRecords = new List<TimelineRecord>(); | ||
var logPages = new Dictionary<Guid, Dictionary<int, string>>(); | ||
var logRecords = new Dictionary<Guid, TimelineRecord>(); | ||
foreach (var log in logs) | ||
{ | ||
var logName = Path.GetFileNameWithoutExtension(log); | ||
var logPageSeperator = logName.IndexOf('_'); | ||
var logRecordId = Guid.Empty; | ||
var pageNumber = 0; | ||
if (logPageSeperator < 0) | ||
{ | ||
Trace.Warning($"log file '{log}' doesn't follow naming convension 'GUID_INT'."); | ||
continue; | ||
} | ||
else | ||
{ | ||
if (!Guid.TryParse(logName.Substring(0, logPageSeperator), out logRecordId)) | ||
{ | ||
Trace.Warning($"log file '{log}' doesn't follow naming convension 'GUID_INT'."); | ||
continue; | ||
} | ||
|
||
if (!int.TryParse(logName.Substring(logPageSeperator + 1), out pageNumber)) | ||
{ | ||
Trace.Warning($"log file '{log}' doesn't follow naming convension 'GUID_INT'."); | ||
continue; | ||
} | ||
} | ||
|
||
var record = timeline.Records.FirstOrDefault(x => x.Id == logRecordId); | ||
if (record != null) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We only upload log files to timeline records for this job. |
||
{ | ||
if (!logPages.ContainsKey(record.Id)) | ||
{ | ||
logPages[record.Id] = new Dictionary<int, string>(); | ||
logRecords[record.Id] = record; | ||
} | ||
|
||
logPages[record.Id][pageNumber] = log; | ||
} | ||
} | ||
|
||
foreach (var pages in logPages) | ||
{ | ||
var record = logRecords[pages.Key]; | ||
if (record.Log == null) | ||
{ | ||
// Create the log | ||
record.Log = await jobServer.CreateLogAsync(message.Plan.ScopeIdentifier, message.Plan.PlanType, message.Plan.PlanId, new TaskLog(String.Format(@"logs\{0:D}", record.Id)), default(CancellationToken)); | ||
|
||
// Need to post timeline record updates to reflect the log creation | ||
updatedRecords.Add(record.Clone()); | ||
} | ||
|
||
for (var i = 1; i <= pages.Value.Count; i++) | ||
{ | ||
var logFile = pages.Value[i]; | ||
// Upload the contents | ||
using (FileStream fs = File.Open(logFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) | ||
{ | ||
var logUploaded = await jobServer.AppendLogContentAsync(message.Plan.ScopeIdentifier, message.Plan.PlanType, message.Plan.PlanId, record.Log.Id, fs, default(CancellationToken)); | ||
} | ||
|
||
Trace.Info($"Uploaded unfinished log '{logFile}' for current job."); | ||
IOUtil.DeleteFile(logFile); | ||
} | ||
} | ||
|
||
if (updatedRecords.Count > 0) | ||
{ | ||
await jobServer.UpdateTimelineRecordsAsync(message.Plan.ScopeIdentifier, message.Plan.PlanType, message.Plan.PlanId, message.Timeline.Id, updatedRecords, CancellationToken.None); | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
// Ignore any error during log upload since it's best effort | ||
Trace.Error(ex); | ||
} | ||
} | ||
|
||
// TODO: We need send detailInfo back to DT in order to add an issue for the job | ||
private async Task CompleteJobRequestAsync(int poolId, Pipelines.AgentJobRequestMessage message, Guid lockToken, TaskResult result, string detailInfo = null) | ||
{ | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we don't really need a new guid for each page, we can leverage the timeline record id for each steps, those are already unique per step.