public void RemoveAllLocks(ServerTaskId serverTaskId) { foreach (var entry in journalEntries.Values) { entry.RemoveLock(serverTaskId); } }
public void RegisterPackageUse(PackageIdentity package, ServerTaskId deploymentTaskId, ulong packageSizeBytes) { try { using (AcquireSemaphore()) { journalRepository.Load(); journalRepository.Cache.IncrementCacheAge(); var age = journalRepository.Cache.CacheAge; if (journalRepository.TryGetJournalEntry(package, out var entry)) { entry.AddUsage(deploymentTaskId, age); entry.AddLock(deploymentTaskId, age); } else { entry = new JournalEntry(package, packageSizeBytes); entry.AddUsage(deploymentTaskId, age); entry.AddLock(deploymentTaskId, age); journalRepository.AddJournalEntry(entry); } log.Verbose($"Registered package use/lock for {package} and task {deploymentTaskId}"); journalRepository.Commit(); } } catch (Exception ex) { //We need to ensure that an issue with the journal doesn't interfere with the deployment. log.Info($"Unable to register package use for retention.{Environment.NewLine}{ex.ToString()}"); } }
public void WhenAJournalEntryIsCommittedAndRetrieved_ThenItShouldBeEquivalentToTheOriginal() { var journalPath = Path.Combine(testDir, "PackageRetentionJournal.json"); var thePackage = CreatePackageIdentity("TestPackage", "0.0.1"); var cacheAge = new CacheAge(10); var serverTaskId = new ServerTaskId("TaskID-1"); var journalEntry = new JournalEntry(thePackage, 1); journalEntry.AddLock(serverTaskId, cacheAge); journalEntry.AddUsage(serverTaskId, cacheAge); var writeRepository = new JsonJournalRepository(TestCalamariPhysicalFileSystem.GetPhysicalFileSystem(), new StaticJsonJournalPathProvider(journalPath), Substitute.For <ILog>()); writeRepository.AddJournalEntry(journalEntry); writeRepository.Commit(); var readRepository = new JsonJournalRepository(TestCalamariPhysicalFileSystem.GetPhysicalFileSystem(), new StaticJsonJournalPathProvider(journalPath), Substitute.For <ILog>()); readRepository.Load(); readRepository.TryGetJournalEntry(thePackage, out var retrieved).Should().BeTrue(); retrieved.Package.Should().BeEquivalentTo(journalEntry.Package); retrieved.GetLockDetails().Should().BeEquivalentTo(journalEntry.GetLockDetails()); retrieved.GetUsageDetails().Should().BeEquivalentTo(journalEntry.GetUsageDetails()); }
public ReleasePackageLockCommand(IVariables variables, IManagePackageCache journal, ILog log) { this.variables = variables; this.log = log; this.journal = journal; Options.Add("taskId=", "Id of the task that is using the package", v => taskId = new ServerTaskId(v)); }
public void WhenPackageIsRegistered_ThenUsageIsRecorded() { var thePackage = CreatePackageIdentity("Package", "1.0"); var deploymentOne = new ServerTaskId("Deployment-1"); packageJournal.RegisterPackageUse(thePackage, deploymentOne, 1); Assert.AreEqual(1, journalRepository.GetUsage(thePackage).Count()); }
public void WhenPackageUsageIsRegistered_ThenALockExists() { var thePackage = CreatePackageIdentity("Package", "1.0"); var theDeployment = new ServerTaskId("Deployment-1"); packageJournal.RegisterPackageUse(thePackage, theDeployment, 1); Assert.IsTrue(journalRepository.HasLock(thePackage)); }
public void RemoveAllLocks(ServerTaskId serverTaskId) { using (AcquireSemaphore()) { log.Verbose($"Releasing package locks for task {serverTaskId}"); journalRepository.Load(); journalRepository.RemoveAllLocks(serverTaskId); journalRepository.Commit(); } }
public void WhenPackageUsageIsDeregistered_ThenNoLocksExist() { var thePackage = CreatePackageIdentity("Package", "1.0"); var theDeployment = new ServerTaskId("Deployment-1"); packageJournal.RegisterPackageUse(thePackage, theDeployment, 1); packageJournal.RemoveAllLocks(theDeployment); Assert.IsFalse(journalRepository.HasLock(thePackage)); }
public void WhenOnePackageIsRegisteredForTwoDeployments_ThenTwoSeparateUsagesAreRecorded() { var thePackage = CreatePackageIdentity("Package1", "1.0"); var deploymentOne = new ServerTaskId("Deployment-1"); var deploymentTwo = new ServerTaskId("Deployment-2"); packageJournal.RegisterPackageUse(thePackage, deploymentOne, 1); packageJournal.RegisterPackageUse(thePackage, deploymentTwo, 1); Assert.AreEqual(2, journalRepository.GetUsage(thePackage).Count()); }
public void WhenTwoPackagesAreRegisteredAgainstTheSameDeployment_ThenTwoSeparateUsagesAreRecorded() { var package1 = CreatePackageIdentity("Package1", "1.0"); var package2 = CreatePackageIdentity("Package2", "1.0"); var theDeployment = new ServerTaskId("Deployment-1"); packageJournal.RegisterPackageUse(package1, theDeployment, 1); packageJournal.RegisterPackageUse(package2, theDeployment, 1); Assert.AreEqual(1, journalRepository.GetUsage(package1).Count()); Assert.AreEqual(1, journalRepository.GetUsage(package2).Count()); }
public void WhenPackageIsRegisteredForTwoDeploymentsAndDeregisteredForOne_ThenALockExists() { var thePackage = CreatePackageIdentity("Package", "1.0"); var deploymentOne = new ServerTaskId("Deployment-1"); var deploymentTwo = new ServerTaskId("Deployment-2"); packageJournal.RegisterPackageUse(thePackage, deploymentOne, 1); packageJournal.RegisterPackageUse(thePackage, deploymentTwo, 1); packageJournal.RemoveAllLocks(deploymentOne); Assert.IsTrue(journalRepository.HasLock(thePackage)); }
static InMemoryJournalRepository SeedJournal() { var journalRepo = new InMemoryJournalRepository(); var journal = new PackageJournal(journalRepo, Substitute.For <ILog>(), new TestCalamariPhysicalFileSystem(), Substitute.For <IRetentionAlgorithm>(), new SystemSemaphoreManager()); var serverTask = new ServerTaskId("ServerTasks-1"); for (var i = 0; i < 50000; i++) { var package = new PackageIdentity(new PackageId($"Package-{i % 100}"), new SemanticVersion(1, 0, i), new PackagePath($"C:\\{i}")); journal.RegisterPackageUse(package, serverTask, 100); } journal.RemoveAllLocks(serverTask); return(journalRepo); }
public RegisterPackageUseCommand(ILog log, IManagePackageCache journal, ICalamariFileSystem fileSystem) { this.log = log; this.journal = journal; this.fileSystem = fileSystem; Options.Add("packageId=", "Package ID of the used package", v => packageId = new PackageId(v)); Options.Add("packageVersionFormat=", $"[Optional] Format of version. Options {string.Join(", ", Enum.GetNames(typeof(VersionFormat)))}. Defaults to `{VersionFormat.Semver}`.", v => { if (!Enum.TryParse(v, out VersionFormat format)) { throw new CommandException($"The provided version format `{format}` is not recognised."); } versionFormat = format; }); Options.Add("packageVersion=", "Package version of the used package", v => packageVersion = VersionFactory.TryCreateVersion(v, versionFormat)); Options.Add("packagePath=", "Path to the package", v => packagePath = new PackagePath(v)); Options.Add("taskId=", "Id of the task that is using the package", v => taskId = new ServerTaskId(v)); }
public void RemoveLock(ServerTaskId deploymentTaskId) { locks.RemoveAll(l => l.DeploymentTaskId == deploymentTaskId); }
public void AddLock(ServerTaskId deploymentTaskId, CacheAge cacheAge) { locks.Add(new UsageDetails(deploymentTaskId, cacheAge)); }
public void AddUsage(ServerTaskId deploymentTaskId, CacheAge cacheAge) { usages.Add(new UsageDetails(deploymentTaskId, cacheAge)); }
/// <summary> Defaults DateTime to DateTime.Now </summary> public UsageDetails(ServerTaskId deploymentTaskId, CacheAge cacheAge) : this(deploymentTaskId, cacheAge, DateTime.Now) { }
public UsageDetails(ServerTaskId deploymentTaskId, CacheAge cacheAgeAtUsage, DateTime dateTime) { CacheAgeAtUsage = cacheAgeAtUsage; DateTime = dateTime; DeploymentTaskId = deploymentTaskId; }