public static DeploymentStatusFile Open(string id, IEnvironment environment, IAnalytics analytics, IOperationLock statusLock) { // status complete file is created infrequently at deployment completion time // once deployment is completed, it is highly unlikely its status will change // this helps optimize status read operation avoid taking status lock DeploymentStatusFile statusComplete = null; OperationManager.SafeExecute(() => { string path = Path.Combine(environment.DeploymentsPath, id, StatusCompleteFile); if (FileSystemHelpers.FileExists(path)) { if (ScmHostingConfigurations.DeploymentStatusCompleteFileEnabled) { statusComplete = new DeploymentStatusFile(id, environment, statusLock, FileSystemCache.ReadXml(path)); } else { FileSystemHelpers.DeleteFile(path); } } }); return(statusComplete ?? statusLock.LockOperation(() => { string path = Path.Combine(environment.DeploymentsPath, id, StatusFile); if (!FileSystemHelpers.FileExists(path)) { return null; } try { XDocument document = null; using (var stream = FileSystemHelpers.OpenRead(path)) { document = XDocument.Load(stream); } return new DeploymentStatusFile(id, environment, statusLock, document); } catch (Exception ex) { // in the scenario where w3wp is abruptly terminated while xml is being written, // we may end up with corrupted xml. we will handle the error and remove the problematic directory. analytics.UnexpectedException(ex); FileSystemHelpers.DeleteDirectorySafe(Path.GetDirectoryName(path), ignoreErrors: true); // it is ok to return null as callers already handle null. return null; } }, "Getting deployment status", DeploymentStatusManager.LockTimeout)); }