diff --git a/Kudu.Core/Deployment/DeploymentManager.cs b/Kudu.Core/Deployment/DeploymentManager.cs index 3ceffd52..e737419e 100644 --- a/Kudu.Core/Deployment/DeploymentManager.cs +++ b/Kudu.Core/Deployment/DeploymentManager.cs @@ -439,12 +439,21 @@ internal IEnumerable PurgeDeployments(IEnumerable re if (results.Any()) { var toDelete = new List(); - toDelete.AddRange(GetPurgeTemporaryDeployments(results)); + //toDelete.AddRange(GetPurgeTemporaryDeployments(results)); toDelete.AddRange(GetPurgeFailedDeployments(results)); toDelete.AddRange(GetPurgeObsoleteDeployments(results)); if (toDelete.Any()) { + var purgeTempDeployments = GetPurgeTemporaryDeployments(results); + foreach (DeployResult temp in purgeTempDeployments) + { + Console.WriteLine("We used to Remove {0}, {1}, received at {2}, but not doing now", + temp.Id.Substring(0, Math.Min(temp.Id.Length, 9)), + temp.Status, + temp.ReceivedTime); + } + var tracer = _traceFactory.GetTracer(); using (tracer.Step("Purge deployment items")) { @@ -452,6 +461,10 @@ internal IEnumerable PurgeDeployments(IEnumerable re { _status.Delete(delete.Id, _environment); + Console.WriteLine("Remove {0}, {1}, received at {2}", + delete.Id.Substring(0, Math.Min(delete.Id.Length, 9)), + delete.Status, + delete.ReceivedTime); tracer.Trace("Remove {0}, {1}, received at {2}", delete.Id.Substring(0, Math.Min(delete.Id.Length, 9)), delete.Status, @@ -559,7 +572,12 @@ IDeploymentStatusFile GetOrCreateStatusFile(ChangeSet changeSet, ITracer tracer, { // Create the status file and store information about the commit statusFile = _status.Create(id, _environment); + Console.WriteLine("id=" + id + ": In GetOrCreateStatusFile. statusFile was NULL. So, created it."); + }else + { + Console.WriteLine("id=" + id + ": In GetOrCreateStatusFile. statusFile already exists"); } + statusFile.Message = changeSet.Message; statusFile.Author = changeSet.AuthorName; statusFile.Deployer = deployer; diff --git a/Kudu.Core/Deployment/DeploymentStatusFile.cs b/Kudu.Core/Deployment/DeploymentStatusFile.cs index 877e90da..f1e789d9 100644 --- a/Kudu.Core/Deployment/DeploymentStatusFile.cs +++ b/Kudu.Core/Deployment/DeploymentStatusFile.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using System.Threading.Tasks; using System.Xml.Linq; using Kudu.Contracts.Infrastructure; using Kudu.Core.Infrastructure; @@ -32,12 +33,23 @@ private DeploymentStatusFile(string id, IEnvironment environment, IOperationLock { Initialize(document); } + + // Ensure that the status file is created before we enter this code block + Task ensureLogFileExists = Task.Run(() => + OperationManager.Attempt(() => + { + if (!FileSystemHelpers.FileExists(_statusFile)) + { + throw new FileNotFoundException("Status file doesn't exist. Will wait for 1 second and retry"); + } + }, 5, 250)); } public static DeploymentStatusFile Create(string id, IEnvironment environment, IOperationLock statusLock) { string path = Path.Combine(environment.DeploymentsPath, id); + Console.WriteLine("id=" +id + ": Creating statusFile at " + path); FileSystemHelpers.EnsureDirectory(path); DateTime utcNow = DateTime.UtcNow; @@ -53,11 +65,14 @@ public static DeploymentStatusFile Open(string id, IEnvironment environment, IAn return statusLock.LockOperation(() => { string path = Path.Combine(environment.DeploymentsPath, id, StatusFile); - if (!FileSystemHelpers.FileExists(path)) + + if(!FileSystemHelpers.FileExists(path)) { + Console.WriteLine("id=" +id + ": DeploymentStatusFile.open. path=" + path +". FileNotFound"); return null; } + Console.WriteLine("id=" + id + ": DeploymentStatusFile.open. path=" + path + ". FileExists"); try { XDocument document = null; diff --git a/Kudu.Core/Deployment/FetchDeploymentManager.cs b/Kudu.Core/Deployment/FetchDeploymentManager.cs index 1e03c2a6..b35591bb 100644 --- a/Kudu.Core/Deployment/FetchDeploymentManager.cs +++ b/Kudu.Core/Deployment/FetchDeploymentManager.cs @@ -348,6 +348,9 @@ public static async Task PerformBackgroundDeployment( var hooksLock = new LockFile(hooksLockPath, traceFactory); var deploymentLock = DeploymentLockFile.GetInstance(deploymentLockPath, traceFactory); + Console.WriteLine("deploymentLockPath=" + deploymentLockPath); + Console.WriteLine((deploymentLock == null) ? "deploymentLock is null" : "deploymentLock is not null"); + var analytics = new Analytics(settings, new ServerConfiguration(), traceFactory); var deploymentStatusManager = new DeploymentStatusManager(environment, analytics, statusLock); var siteBuilderFactory = new SiteBuilderFactory(new BuildPropertyProvider(), environment, _httpContextAccessor); diff --git a/Kudu.Core/Environment.cs b/Kudu.Core/Environment.cs index 89ed2d0a..6a169ac6 100644 --- a/Kudu.Core/Environment.cs +++ b/Kudu.Core/Environment.cs @@ -125,8 +125,17 @@ public Environment( RootPath = rootPath; SiteRootPath = Path.Combine(rootPath, Constants.SiteFolder); + if(k8seAppName == null) + { + _tempPath = Path.GetTempPath(); + Console.WriteLine("k8seAppName is null. tempPath=" + _tempPath); + } + else + { + _tempPath = Path.Combine(Path.GetTempPath(), k8seAppName); + Console.WriteLine("k8seAppName is " + k8seAppName +". tempPath=" + _tempPath); + } - _tempPath = Path.GetTempPath(); _repositoryPath = repositoryPath; _zipTempPath = Path.Combine(_tempPath, Constants.ZipTempPath); _webRootPath = Path.Combine(SiteRootPath, Constants.WebRoot); diff --git a/Kudu.Core/Infrastructure/DeploymentLockFile.cs b/Kudu.Core/Infrastructure/DeploymentLockFile.cs index 2d35088a..54f37ed9 100644 --- a/Kudu.Core/Infrastructure/DeploymentLockFile.cs +++ b/Kudu.Core/Infrastructure/DeploymentLockFile.cs @@ -1,6 +1,7 @@ using System; using System.Diagnostics; using System.Threading; +using Kudu.Contracts.Infrastructure; using Kudu.Contracts.SourceControl; using Kudu.Core.Helpers; using Kudu.Core.SourceControl; @@ -11,49 +12,22 @@ namespace Kudu.Core.Infrastructure /// /// Specific to deployment lock. /// - public class DeploymentLockFile : AllSafeLinuxLock + public class DeploymentLockFile { private static readonly ReaderWriterLockSlim rwl = new ReaderWriterLockSlim(); private readonly string _path; - private static DeploymentLockFile _deploymentLockFile; + private static IOperationLock _deploymentLockFile; //private static AllSafeLinuxLock _linuxLock; - public static DeploymentLockFile GetInstance(string path, ITraceFactory traceFactory) + public static IOperationLock GetInstance(string path, ITraceFactory traceFactory) { if (_deploymentLockFile == null) { - _deploymentLockFile = new DeploymentLockFile(path,traceFactory); + _deploymentLockFile = new NoOpLock(); } return _deploymentLockFile; } - - private DeploymentLockFile(string path, ITraceFactory traceFactory) : base(path,traceFactory) - { - _path = path; - /* - if (!OSDetector.IsOnWindows()) - { - _linuxLock = new LinuxLockFile(path+"_2",traceFactory); - _isLinux = true; - } - */ - } - - - public void OnLockAcquired() - { - IRepositoryFactory repositoryFactory = RepositoryFactory; - if (repositoryFactory != null) - { - IRepository repository = repositoryFactory.GetRepository(); - if (repository != null) - { - // Clear any left over repository-related lock since we have the actual lock - repository.ClearLock(); - } - } - } } } \ No newline at end of file diff --git a/Kudu.Core/Infrastructure/LockFile.cs b/Kudu.Core/Infrastructure/LockFile.cs index 8199c75a..f8c37415 100644 --- a/Kudu.Core/Infrastructure/LockFile.cs +++ b/Kudu.Core/Infrastructure/LockFile.cs @@ -24,13 +24,13 @@ public virtual bool IsHeld public LockFile(string path) { - _lock = new WindowsLockFile(path); + _lock = new NoOpLock(); } public LockFile(string path, ITraceFactory traceFactory, bool ensureLock = false) { - _lock = new WindowsLockFile(path,traceFactory,ensureLock); + _lock = new NoOpLock(); } diff --git a/Kudu.Core/Infrastructure/NoOpLock.cs b/Kudu.Core/Infrastructure/NoOpLock.cs new file mode 100644 index 00000000..8f6c47b1 --- /dev/null +++ b/Kudu.Core/Infrastructure/NoOpLock.cs @@ -0,0 +1,35 @@ +using Kudu.Contracts.Infrastructure; +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; + +namespace Kudu.Core.Infrastructure +{ + public class NoOpLock : IOperationLock + { + public bool IsHeld => false; + + public OperationLockInfo LockInfo => new OperationLockInfo(); + + public void InitializeAsyncLocks() + { + // + } + + public bool Lock(string operationName) + { + return true; + } + + public Task LockAsync(string operationName) + { + return Task.Run(() => true); + } + + public void Release() + { + // + } + } +} \ No newline at end of file diff --git a/Kudu.Services.Web/KubeMiddleware.cs b/Kudu.Services.Web/KubeMiddleware.cs index a6d65c39..b2d70f2a 100644 --- a/Kudu.Services.Web/KubeMiddleware.cs +++ b/Kudu.Services.Web/KubeMiddleware.cs @@ -61,10 +61,17 @@ public async Task Invoke(HttpContext context, IEnvironment environment, IServerC } // Cache the App Environment for this request - context.Items.TryAdd("environment", GetEnvironment(homeDir, appName, null, null, appNamenamespace, appType)); + if (!context.Items.ContainsKey("environment")) + { + Console.WriteLine("KubeMiddlware: adding environment. appName=" + appName + ",appType=" + appType); + context.Items.TryAdd("environment", GetEnvironment(homeDir, appName, null, null, appNamenamespace, appType)); + } // Cache the appName for this request - context.Items.TryAdd("appName", appName); + if (!context.Items.ContainsKey("appName")) { + Console.WriteLine("KubeMiddlware: appName=" + appName); + context.Items.TryAdd("appName", appName); + } // Add All AppSettings to the context. K8SEDeploymentHelper.UpdateContextWithAppSettings(context); diff --git a/Kudu.Services.Web/KuduWebUtil.cs b/Kudu.Services.Web/KuduWebUtil.cs index 6604f78f..954cc03d 100644 --- a/Kudu.Services.Web/KuduWebUtil.cs +++ b/Kudu.Services.Web/KuduWebUtil.cs @@ -36,7 +36,7 @@ internal static class KuduWebUtil private static Dictionary _namedLocks; - private static DeploymentLockFile _deploymentLock; + private static IOperationLock _deploymentLock; // // This method initializes status,ssh,hooks & deployment locks used by Kudu to ensure @@ -81,9 +81,7 @@ private static void SetupLocks(ITraceFactory traceFactory, IEnvironment environm var statusLockPath = Path.Combine(lockPath, Constants.StatusLockFile); var sshKeyLockPath = Path.Combine(lockPath, Constants.SSHKeyLockFile); var hooksLockPath = Path.Combine(lockPath, Constants.HooksLockFile); - _deploymentLock = DeploymentLockFile.GetInstance(deploymentLockPath, traceFactory); - _deploymentLock.InitializeAsyncLocks(); - + _deploymentLock = new NoOpLock(); var statusLock = new LockFile(statusLockPath, traceFactory); statusLock.InitializeAsyncLocks(); var sshKeyLock = new LockFile(sshKeyLockPath, traceFactory); @@ -470,7 +468,7 @@ internal static void EnsureDotNetCoreEnvironmentVariable(IEnvironment environmen /// /// /// - internal static DeploymentLockFile GetDeploymentLock(ITraceFactory traceFactory, IEnvironment environment) + internal static IOperationLock GetDeploymentLock(ITraceFactory traceFactory, IEnvironment environment) { if (_namedLocks == null || _deploymentLock == null) { diff --git a/Kudu.Services.Web/Startup.cs b/Kudu.Services.Web/Startup.cs index 816fe09f..79a360c2 100644 --- a/Kudu.Services.Web/Startup.cs +++ b/Kudu.Services.Web/Startup.cs @@ -241,12 +241,6 @@ public void ConfigureServices(IServiceCollection services) services.AddScoped(); - services.AddScoped( - sp => KuduWebUtil.GetDeploymentLock(traceFactory, environment).RepositoryFactory = - new RepositoryFactory( - sp.GetRequiredService(), sp.GetRequiredService(), - sp.GetRequiredService())); - services.AddScoped(); // Git server