From 422e386d12de224a11db1b6de4fc5e866c36273e Mon Sep 17 00:00:00 2001 From: Michael Hedgpeth Date: Sat, 17 Jun 2017 21:44:00 -0500 Subject: [PATCH] When chef client is running, don't process jobs --- README.md | 2 +- build.cake | 2 +- src/cafe.Updater/cafe.Updater.csproj | 2 +- .../Server/CafeServerWindowsService.cs | 27 ++++++++++++++++++- src/cafe/Server/Jobs/JobRunner.cs | 20 ++++++++++++++ src/cafe/cafe.csproj | 2 +- .../cafe.IntegrationTest.csproj | 2 +- test/cafe.Test/Server/Jobs/JobRunnerTest.cs | 26 ++++++++++++++++++ test/cafe.Test/cafe.Test.csproj | 2 +- 9 files changed, 78 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 4c14fbf..fd12e02 100644 --- a/README.md +++ b/README.md @@ -77,7 +77,7 @@ We probably want to schedule Chef to run every 30 minutes or so. To do this we e "ChefInterval": 1800, "Port": 59320 } -``` +``` And restart the cafe service: diff --git a/build.cake b/build.cake index d8184f6..509dacc 100644 --- a/build.cake +++ b/build.cake @@ -9,7 +9,7 @@ var target = Argument("target", "FullBuild"); var configuration = Argument("configuration", "Debug"); var buildNumber = Argument("buildNumber", "0"); -var version = "0.9.2." + buildNumber; +var version = "0.10.0." + buildNumber; var cafeDirectory = Directory("./src/cafe"); var cafeProject = cafeDirectory + File("cafe.csproj"); diff --git a/src/cafe.Updater/cafe.Updater.csproj b/src/cafe.Updater/cafe.Updater.csproj index 83762a3..b7e2013 100644 --- a/src/cafe.Updater/cafe.Updater.csproj +++ b/src/cafe.Updater/cafe.Updater.csproj @@ -1,6 +1,6 @@  - 0.9.2 + 0.10.0 netcoreapp1.1 true portable diff --git a/src/cafe/Options/Server/CafeServerWindowsService.cs b/src/cafe/Options/Server/CafeServerWindowsService.cs index eca170e..06dde3f 100644 --- a/src/cafe/Options/Server/CafeServerWindowsService.cs +++ b/src/cafe/Options/Server/CafeServerWindowsService.cs @@ -48,7 +48,8 @@ public void Start(string[] startupArguments, ServiceStoppedCallback serviceStopp Initialize(); ReactToChangesToServerConfiguration(); - + ReactToChangesToChefClientRunning(); + _webHost.Start(); } @@ -59,6 +60,30 @@ private static void ReactToChangesToServerConfiguration() watcher.EnableRaisingEvents = true; } + private static void ReactToChangesToChefClientRunning() + { + var clientRunningFile = "chef-client-running.pid"; + var clientRunningFolder = $"{ServerSettings.Instance.InstallRoot}/chef/cache"; + Logger.Info($"Listening for chef client to be running by listening for pid file {clientRunningFile} in {clientRunningFolder}"); + FileSystemWatcher watcher = new FileSystemWatcher(clientRunningFolder) { Filter = clientRunningFile }; + watcher.Created += PauseRunner; + watcher.Deleted += ResumeRunner; + } + + private static void ResumeRunner(object sender, FileSystemEventArgs e) + { + var runner = StructureMapResolver.Container.GetInstance(); + Logger.Info($"Since file {e.FullPath} has been deleted and therefore the chef client has stopped running, resuming processing of jobs"); + runner.Pause(); + } + + private static void PauseRunner(object sender, FileSystemEventArgs e) + { + var runner = StructureMapResolver.Container.GetInstance(); + Logger.Info($"Since file {e.FullPath} has been created and therefore the chef client is running, pausing any processing of jobs so we don't interfere with Chef's activities"); + runner.Pause(); + } + private static void OnServerConfigurationChanged(object sender, FileSystemEventArgs args) { Presenter.ShowMessage("Server configuration changed, so resetting Chef Interval", Logger); diff --git a/src/cafe/Server/Jobs/JobRunner.cs b/src/cafe/Server/Jobs/JobRunner.cs index c0f07fb..c7a5bdc 100644 --- a/src/cafe/Server/Jobs/JobRunner.cs +++ b/src/cafe/Server/Jobs/JobRunner.cs @@ -29,6 +29,11 @@ public void ProcessQueue() Logger.Debug("Processing tasks for scheduler"); lock (_processLocker) // since queues are being manipulated here, don't let this happen multiple times { + if (IsPaused) + { + Logger.Debug("Since this runner is paused, jobs will not be processed"); + return; + } if (_queuedRuns.Count == 0) { Logger.Debug("There is nothing to do right now"); @@ -97,5 +102,20 @@ public void Dispose() { _timer.Dispose(); } + + public void Pause() + { + Logger.Info("Pausing processing jobs"); + IsPaused = true; + } + + public void Resume() + { + Logger.Info("Resuming processing jobs"); + IsPaused = false; + ProcessQueue(); + } + + public bool IsPaused { get; private set; } } } \ No newline at end of file diff --git a/src/cafe/cafe.csproj b/src/cafe/cafe.csproj index 465f8cc..83445f5 100644 --- a/src/cafe/cafe.csproj +++ b/src/cafe/cafe.csproj @@ -1,6 +1,6 @@  - 0.9.2 + 0.10.0 netcoreapp1.1 true portable diff --git a/test/cafe.IntegrationTest/cafe.IntegrationTest.csproj b/test/cafe.IntegrationTest/cafe.IntegrationTest.csproj index 1890f18..61cbbdd 100644 --- a/test/cafe.IntegrationTest/cafe.IntegrationTest.csproj +++ b/test/cafe.IntegrationTest/cafe.IntegrationTest.csproj @@ -1,6 +1,6 @@  - 0.9.2 + 0.10.0 netcoreapp1.1 portable cafe.IntegrationTest diff --git a/test/cafe.Test/Server/Jobs/JobRunnerTest.cs b/test/cafe.Test/Server/Jobs/JobRunnerTest.cs index b8ad69f..6c020d8 100644 --- a/test/cafe.Test/Server/Jobs/JobRunnerTest.cs +++ b/test/cafe.Test/Server/Jobs/JobRunnerTest.cs @@ -127,5 +127,31 @@ public void Schedule_ShouldImmediatelyProcessTasks() .BeTrue( "because scheduling a scheduled task should immediately process it to make manually submitted tasks faster"); } + + [Fact] + public void Pause_ShouldNotProcessNewJobs() + { + var task = new FakeJobRun(); + var runner = CreateJobRunner(); + + runner.Pause(); + + runner.Enqueue(task); + + task.WasRunCalled.Should().BeFalse("because the runner is paused"); + } + + [Fact] + public void Resume_ShouldStartProcessingJobsAgain() + { + var task = new FakeJobRun(); + var runner = CreateJobRunner(); + + runner.Pause(); + runner.Enqueue(task); + runner.Resume(); + + task.WasRunCalled.Should().BeTrue("because runner resumed"); + } } } \ No newline at end of file diff --git a/test/cafe.Test/cafe.Test.csproj b/test/cafe.Test/cafe.Test.csproj index 3133f4a..3ac0078 100644 --- a/test/cafe.Test/cafe.Test.csproj +++ b/test/cafe.Test/cafe.Test.csproj @@ -1,6 +1,6 @@  - 0.9.2 + 0.10.0 netcoreapp1.1 portable cafe.Test