private void MoveToPermanentStorage(string sessionId, string sourceFile, string fileName, string blobSasUri) { if (string.IsNullOrWhiteSpace(blobSasUri)) { var sessionDirectory = GetLogsFolderForSession(sessionId); var collectFileName = Path.Combine(sessionDirectory, fileName); AppendToMonitoringLog($"Copying file from temp folders to [{collectFileName}]", true); try { FileSystemHelpers.CopyFile(sourceFile, collectFileName); AppendToMonitoringLog($"Copied file from temp folders to [{collectFileName}]", true); } catch (Exception ex) { Logger.LogCpuMonitoringErrorEvent("Failed while moving file to Permanent FileSystem storage", ex, sessionId); } } else { try { string relativeFilePath = Path.Combine("Monitoring", "Logs", sessionId, fileName); Lease lease = Infrastructure.LeaseManager.TryGetLease(relativeFilePath, blobSasUri); if (lease == null) { // This instance is already running this collector Logger.LogCpuMonitoringVerboseEvent($"Could not get lease to upload the memory dump - {relativeFilePath}", sessionId); } AppendToMonitoringLog($"Copying {fileName} from temp folders to Blob Storage", true); var accessCondition = AccessCondition.GenerateLeaseCondition(lease.Id); var taskToUpload = Task.Run(() => { FileStream fileStream = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); var blockBlob = BlobController.GetBlobForFile(relativeFilePath, blobSasUri); blockBlob.UploadFromStream(fileStream, accessCondition); }); while (!taskToUpload.IsCompleted) { lease.Renew(); Logger.LogCpuMonitoringVerboseEvent($"Renewing lease to the blob file", sessionId); Thread.Sleep(Infrastructure.Settings.LeaseRenewalTime); } lease.Release(); AppendToMonitoringLog($"Copied {fileName} from temp folders to Blob Storage", true); } catch (Exception ex) { Logger.LogCpuMonitoringErrorEvent("Failed copying file to blob storage", ex, sessionId); } } }
protected async Task RunProcessWhileKeepingLeaseAliveAsync(Lease lease, string command, string args, string sessionId, CancellationToken token) { try { command = ExpandVariables(command); Logger.LogDiagnostic("Calling {0}: {1} {2}", Name, command, args); // Call Diagnostic tool var toolProcess = Infrastructure.RunProcess(command, args, sessionId); double secondsWaited = 0; var processStartTime = DateTime.UtcNow; while (!toolProcess.HasExited) { // We don't want the lease to expire while we're waiting for the collector to finish running await Task.Delay(Infrastructure.Settings.LeaseRenewalTime); lease.Renew(); secondsWaited = secondsWaited + Infrastructure.Settings.LeaseRenewalTime.TotalSeconds; if (secondsWaited > 120) { secondsWaited = 0; Logger.LogSessionVerboseEvent($"Waiting for process {command} {args} to finish. Process running for {DateTime.UtcNow.Subtract(processStartTime).TotalSeconds} seconds", sessionId); } if (token != CancellationToken.None) { if (token.IsCancellationRequested) { Logger.LogSessionVerboseEvent($"Kill tool process [{command} {args}] because cancellation is requested", sessionId); try { toolProcess.Kill(); Logger.LogSessionVerboseEvent($"Tool process [{command} {args}] killed because cancellation is requested", sessionId); } catch (Exception) { //no-op } token.ThrowIfCancellationRequested(); } } } //var errorStream = toolProcess.StandardError; //var errorText = errorStream.ReadToEnd(); //var outputStream = toolProcess.StandardOutput; //var outputText = outputStream.ReadToEnd(); Logger.LogDiagnostic("Exit Code: {0}", toolProcess.ExitCode); //Logger.LogDiagnostic("Error Text: {0}", errorText); //Logger.LogDiagnostic("Output Text: {0}", outputText); //if (!string.IsNullOrEmpty(errorText)) if (toolProcess.ExitCode != 0) { var errorCode = toolProcess.ExitCode; toolProcess.Dispose(); var errorMessage = String.Format( "Process {0} exited with error code {1}.", //" Error message: {2}", command, errorCode); //, //string.IsNullOrWhiteSpace(errorText) ? outputText : errorText); Logger.LogDiagnostic("Throwing DiagnosticToolError: " + errorMessage); throw new DiagnosticToolErrorException(errorMessage); } } catch (AggregateException ae) { foreach (var e in ae.InnerExceptions) { if (e is ApplicationException) { Logger.LogDiagnostic(e.Message); } } throw; } }