Skip to content

Commit

Permalink
Merge pull request #32 from mhedgpeth/pause_when_chef_running
Browse files Browse the repository at this point in the history
When chef client is running, don't process jobs
  • Loading branch information
mhedgpeth authored Jun 18, 2017
2 parents a5b7d0e + 422e386 commit befe48a
Showing 9 changed files with 78 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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:

2 changes: 1 addition & 1 deletion build.cake
Original file line number Diff line number Diff line change
@@ -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");
2 changes: 1 addition & 1 deletion src/cafe.Updater/cafe.Updater.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<VersionPrefix>0.9.2</VersionPrefix>
<VersionPrefix>0.10.0</VersionPrefix>
<TargetFramework>netcoreapp1.1</TargetFramework>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<DebugType>portable</DebugType>
27 changes: 26 additions & 1 deletion src/cafe/Options/Server/CafeServerWindowsService.cs
Original file line number Diff line number Diff line change
@@ -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<JobRunner>();
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<JobRunner>();
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);
20 changes: 20 additions & 0 deletions src/cafe/Server/Jobs/JobRunner.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
2 changes: 1 addition & 1 deletion src/cafe/cafe.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<VersionPrefix>0.9.2</VersionPrefix>
<VersionPrefix>0.10.0</VersionPrefix>
<TargetFramework>netcoreapp1.1</TargetFramework>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<DebugType>portable</DebugType>
2 changes: 1 addition & 1 deletion test/cafe.IntegrationTest/cafe.IntegrationTest.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<VersionPrefix>0.9.2</VersionPrefix>
<VersionPrefix>0.10.0</VersionPrefix>
<TargetFramework>netcoreapp1.1</TargetFramework>
<DebugType>portable</DebugType>
<AssemblyName>cafe.IntegrationTest</AssemblyName>
26 changes: 26 additions & 0 deletions test/cafe.Test/Server/Jobs/JobRunnerTest.cs
Original file line number Diff line number Diff line change
@@ -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");
}
}
}
2 changes: 1 addition & 1 deletion test/cafe.Test/cafe.Test.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<VersionPrefix>0.9.2</VersionPrefix>
<VersionPrefix>0.10.0</VersionPrefix>
<TargetFramework>netcoreapp1.1</TargetFramework>
<DebugType>portable</DebugType>
<AssemblyName>cafe.Test</AssemblyName>

0 comments on commit befe48a

Please sign in to comment.