private static CloudJob GetJob(Project proj, string jobID) { var api = new JobsApi(); var job = api.GetJob(proj.Owner.Name, proj.Name, jobID.ToString()); return(job); }
public async Task <string> WatchJobStatusAsync(Action <string> progressAction = default, System.Threading.CancellationToken cancelToken = default) { var api = new JobsApi(); var proj = this.Project; var jobId = this.JobID; var cloudJob = api.GetJob(proj.Owner.Name, proj.Name, jobId); var status = cloudJob.Status; var startTime = status.StartedAt; while (status.FinishedAt <= status.StartedAt) { var currentSeconds = Math.Round((DateTime.UtcNow - startTime).TotalSeconds); // wait 5 seconds before calling api to re-check the status var totalDelaySeconds = status.Status == JobStatusEnum.Created ? 3 : 5; var running = status.RunsPending + status.RunsRunning; var done = status.RunsFailed + status.RunsCompleted + status.RunsCancelled; var total = running + done; for (int i = 0; i < totalDelaySeconds; i++) { // suspended by user cancelToken.ThrowIfCancellationRequested(); var timer = GetUserFriendlyTimeCounter(TimeSpan.FromSeconds(currentSeconds)); var message = total > 1 ? $"{status.Status}: [{done}/{total}]\n{timer}": $"{status.Status}: [{timer}]"; progressAction?.Invoke(message); await Task.Delay(1000); currentSeconds++; } // suspended by user cancelToken.ThrowIfCancellationRequested(); // update status await Task.Delay(1000); cloudJob = api.GetJob(proj.Owner.Name, proj.Name, jobId); status = cloudJob.Status; //_simulation = new Simulation(proj, simuId); } this.CloudJob = cloudJob; // suspended by user cancelToken.ThrowIfCancellationRequested(); var totalTime = status.FinishedAt - startTime; var finishMessage = status.Status.ToString(); //progressAction?.Invoke($"Task: {status.Status}"); finishMessage = $"{finishMessage}: [{GetUserFriendlyTimeCounter(totalTime)}]"; progressAction?.Invoke(finishMessage); return(finishMessage); string GetUserFriendlyTimeCounter(TimeSpan timeDelta) { string format = @"hh\:mm\:ss"; if (timeDelta.Days > 0) { format = @"d\ hh\:mm\:ss"; } else if (timeDelta.Hours > 0) { format = @"hh\:mm\:ss"; } else if (timeDelta.Minutes > 0) { format = @"mm\:ss"; } else { format = @"ss"; } return(timeDelta.ToString(format)); } }