Skip to content

Commit

Permalink
Added support for stopping service when folder removed
Browse files Browse the repository at this point in the history
  • Loading branch information
phatboyg committed Mar 22, 2011
1 parent 29c7760 commit 62f921d
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 21 deletions.
13 changes: 12 additions & 1 deletion src/Topshelf/FileSystem/DirectoryMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,22 @@ void ScheduleFolderChangeNotification(string directory)

_pendingNotifications.Add(directory, _scheduler.Schedule(_idlePeriod, _fiber, () =>
{
_serviceChannel.Send(new ServiceFolderChanged(directory));
SendFolderNotification(directory);
_pendingNotifications.Remove(directory);
}));
}

void SendFolderNotification(string directory)
{
var serviceFolderExists = Directory.Exists(Path.Combine(_baseDirectory, directory));

if (serviceFolderExists)
_serviceChannel.Send(new ServiceFolderChanged(directory));
else
_serviceChannel.Send(new ServiceFolderRemoved(directory));
}

public void Stop()
{
if (_producer != null)
Expand Down
13 changes: 4 additions & 9 deletions src/Topshelf/Hosts/ConsoleRunHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,25 +98,20 @@ void ShutdownCoordinator()
[MethodImpl(MethodImplOptions.Synchronized)]
void HandleCancelKeyPress(object sender, ConsoleCancelEventArgs consoleCancelEventArgs)
{
if (_hasCancelled)
{
consoleCancelEventArgs.Cancel = true;
return;
}

if (consoleCancelEventArgs.SpecialKey == ConsoleSpecialKey.ControlBreak)
{
_log.Error("Control+Break detected, terminating service (not cleanly, use Control+C to exit cleanly)");
return;
}

//Console.CancelKeyPress -= HandleCancelKeyPress;
consoleCancelEventArgs.Cancel = true;

if (_hasCancelled)
return;

_log.Info("Control+C detected, exiting.");
_exit.Set();

consoleCancelEventArgs.Cancel = true;

_hasCancelled = true;
}

Expand Down
1 change: 1 addition & 0 deletions src/Topshelf/Messages/ServiceEventType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ public enum ServiceEventType
Completed,
FolderChanged,
ShelfCreated,
FolderRemoved,
}
}
12 changes: 6 additions & 6 deletions src/Topshelf/Messages/ServiceFolderChanged.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2007-2010 The Apache Software Foundation.
//
// Copyright 2007-2011 The Apache Software Foundation.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use
// this file except in compliance with the License. You may obtain a copy of the
// License at
Expand All @@ -12,9 +12,9 @@
// specific language governing permissions and limitations under the License.
namespace Topshelf.Messages
{
public class ServiceFolderChanged :
ServiceEvent
{
public class ServiceFolderChanged :
ServiceEvent
{
public ServiceFolderChanged(string name)
: base(name)
{
Expand All @@ -24,5 +24,5 @@ public ServiceFolderChanged(string name)
protected ServiceFolderChanged()
{
}
}
}
}
28 changes: 28 additions & 0 deletions src/Topshelf/Messages/ServiceFolderRemoved.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2007-2011 The Apache Software Foundation.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use
// this file except in compliance with the License. You may obtain a copy of the
// License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.
namespace Topshelf.Messages
{
public class ServiceFolderRemoved :
ServiceEvent
{
public ServiceFolderRemoved(string name)
: base(name)
{
EventType = ServiceEventType.FolderRemoved;
}

protected ServiceFolderRemoved()
{
}
}
}
5 changes: 0 additions & 5 deletions src/Topshelf/Messages/ShelfCreated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ public ShelfCreated(string serviceName, Uri address, string pipeName)
EventType = ServiceEventType.ShelfCreated;
}

public ShelfCreated(string serviceName)
: this(serviceName, null, null)
{
}

protected ShelfCreated()
{
}
Expand Down
38 changes: 38 additions & 0 deletions src/Topshelf/Model/ServiceCoordinator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ void CreateCoordinatorChannel()
.UsingConsumer(OnServiceFolderChanged)
.HandleOnFiber(_fiber);
x.AddConsumerOf<ServiceFolderRemoved>()
.UsingConsumer(OnServiceFolderRemoved)
.HandleOnFiber(_fiber);
x.AddConsumerOf<Request<ServiceStatus>>()
.UsingConsumer(Status)
.HandleOnFiber(_fiber);
Expand Down Expand Up @@ -254,6 +258,40 @@ void OnServiceFolderChanged(ServiceFolderChanged message)
OnCreateShelfService(new CreateShelfService(message.ServiceName, ShelfType.Folder, null, new AssemblyName[] {}));
}

void OnServiceFolderRemoved(ServiceFolderRemoved message)
{
_log.InfoFormat("[Topshelf] Folder Removed: {0}", message.ServiceName);

if (_actorCache.Has(message.ServiceName))
{
var actor = _actorCache[message.ServiceName];

_actorCache.Remove(message.ServiceName);
_serviceCache.Remove(message.ServiceName);

ChannelConnection connection = null;
connection = _channel.Connect(x =>
{
x.AddConsumerOf<ServiceStopped>()
.Where(m => m.ServiceName == message.ServiceName)
.UsingConsumer(_ =>
{
actor.Send(new UnloadService(message.ServiceName));
});
x.AddConsumerOf<ServiceUnloaded>()
.Where(m => m.ServiceName == message.ServiceName)
.UsingConsumer(_ =>
{
// actor.Exit(); why timeout?
connection.Dispose();
});
});

actor.Send(new StopService(message.ServiceName));
}
}

void OnServiceFault(ServiceFault message)
{
_log.ErrorFormat("Fault on {0}: {1}", message.ServiceName, message.ToLogString());
Expand Down
3 changes: 3 additions & 0 deletions src/Topshelf/Model/Shelf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ void AddEventForwarders(ConnectionConfigurator x)
x.AddConsumerOf<ServiceFolderChanged>()
.UsingConsumer(m => _controllerChannel.Send(m))
.HandleOnFiber(_fiber);
x.AddConsumerOf<ServiceFolderRemoved>()
.UsingConsumer(m => _controllerChannel.Send(m))
.HandleOnFiber(_fiber);
x.AddConsumerOf<ServiceRunning>()
.UsingConsumer(m => _controllerChannel.Send(m))
.HandleOnFiber(_fiber);
Expand Down
1 change: 1 addition & 0 deletions src/Topshelf/Topshelf.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@
<Compile Include="Messages\ServiceEvent.cs" />
<Compile Include="Messages\ServiceEventType.cs" />
<Compile Include="Messages\ServiceCommand.cs" />
<Compile Include="Messages\ServiceFolderRemoved.cs" />
<Compile Include="Messages\ServicePaused.cs" />
<Compile Include="Messages\ServicePausing.cs" />
<Compile Include="Messages\ServiceCreated.cs" />
Expand Down

0 comments on commit 62f921d

Please sign in to comment.