Skip to content

Commit

Permalink
minor tweaks to files
Browse files Browse the repository at this point in the history
  • Loading branch information
phatboyg committed Aug 10, 2010
1 parent 67567b1 commit 694144c
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 118 deletions.
46 changes: 23 additions & 23 deletions src/Topshelf.Host/TopshelfHostService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,33 @@
// specific language governing permissions and limitations under the License.
namespace Topshelf.Host
{
using System;
using System.IO;
using System.Linq;
using FileSystem;
using Shelving;
using System;
using System.IO;
using System.Linq;
using FileSystem;
using Shelving;


public class TopshelfHostService
{
ShelfMaker _shelfMaker;
public class TopshelfHostService
{
ShelfMaker _shelfMaker;

public void Start()
{
_shelfMaker = new ShelfMaker();
_shelfMaker.MakeShelf("TopShelf.DirectoryWatcher", typeof(DirectoryMonitorBootstrapper));
public void Start()
{
_shelfMaker = new ShelfMaker();
_shelfMaker.MakeShelf("TopShelf.DirectoryWatcher", typeof(DirectoryMonitorBootstrapper));

string serviceDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Services");
string serviceDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Services");

Directory.GetDirectories(serviceDir)
.ToList()
.ConvertAll(Path.GetFileName)
.ForEach(dir => _shelfMaker.MakeShelf(dir));
}
Directory.GetDirectories(serviceDir)
.ToList()
.ConvertAll(x => Path.GetFileName(x))
.ForEach(dir => _shelfMaker.MakeShelf(dir));
}

public void Stop()
{
_shelfMaker.Dispose();
}
}
public void Stop()
{
_shelfMaker.Dispose();
}
}
}
2 changes: 1 addition & 1 deletion src/Topshelf.Resharper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -312,4 +312,4 @@ specific language governing permissions and limitations under the License.
<ExtraRule Prefix="_" Suffix="" Style="aaBb" />
</PredefinedRule>
</Naming2>
</CodeStyleSettings>
</CodeStyleSettings>
69 changes: 35 additions & 34 deletions src/Topshelf/Bottles/BottleService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2007-2010 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,41 +12,42 @@
// specific language governing permissions and limitations under the License.
namespace Topshelf.Bottles
{
using System;
using System.Configuration;
using System.IO;
using Magnum.FileSystem;
using Directory = Magnum.FileSystem.Directory;
using System;
using System.Configuration;
using System.IO;
using Magnum.FileSystem;


public class BottleService
{
IDisposable _cleanup;
FileSystem _fs;
BottleWatcher _watcher;
public class BottleService
{
IDisposable _cleanup;
FileSystem _fs;
BottleWatcher _watcher;

public void Start()
{
//TODO: how to find the services dir
//TODO: how to get the bottles dir
//TODO: do we need a custom config?
string baseDir = ConfigurationManager.AppSettings["BottlesDirectory"] ?? Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Bottles");
_fs = new DotNetFileSystem();
Directory bottlesDir = _fs.GetDirectory(baseDir);
_watcher = new BottleWatcher();
_cleanup = _watcher.Watch(bottlesDir.Name.GetPath(), CopyToServices);
}
public void Start()
{
//TODO: how to find the services dir
//TODO: how to get the bottles dir
//TODO: do we need a custom config?
string baseDir = ConfigurationManager.AppSettings["BottlesDirectory"]
?? Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Bottles");
_fs = new DotNetFileSystem();
Magnum.FileSystem.Directory bottlesDir = _fs.GetDirectory(baseDir);
_watcher = new BottleWatcher();
_cleanup = _watcher.Watch(bottlesDir.Name.GetPath(), CopyToServices);
}

void CopyToServices(Directory obj)
{
string serviceName = obj.Name.GetName();
Directory targetDir = _fs.GetDirectory("Services").GetChildDirectory(serviceName);
obj.CopyTo(targetDir.Name);
}
public void Stop()
{
_watcher = null;
_cleanup.Dispose();
}

public void Stop()
{
_watcher = null;
_cleanup.Dispose();
}
}
void CopyToServices(Magnum.FileSystem.Directory obj)
{
string serviceName = obj.Name.GetName();
Magnum.FileSystem.Directory targetDir = _fs.GetDirectory("Services").GetChildDirectory(serviceName);
obj.CopyTo(targetDir.Name);
}
}
}
27 changes: 14 additions & 13 deletions src/Topshelf/Bottles/BottleServiceBootstrapper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2007-2010 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,17 +12,18 @@
// specific language governing permissions and limitations under the License.
namespace Topshelf.Bottles
{
using Configuration.Dsl;
using Shelving;
using Configuration.Dsl;
using Shelving;


public class BottleServiceBootstrapper :
Bootstrapper<BottleService>
{
public void InitializeHostedService(IServiceConfigurator<BottleService> cfg)
{
cfg.HowToBuildService(name => new BottleService());
cfg.WhenStarted(s => s.Start());
cfg.WhenStopped(s => s.Stop());
}
}
public class BottleServiceBootstrapper :
Bootstrapper<BottleService>
{
public void InitializeHostedService(IServiceConfigurator<BottleService> cfg)
{
cfg.HowToBuildService(name => new BottleService());
cfg.WhenStarted(s => s.Start());
cfg.WhenStopped(s => s.Stop());
}
}
}
92 changes: 45 additions & 47 deletions src/Topshelf/Bottles/BottleWatcher.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2007-2010 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,57 +12,55 @@
// specific language governing permissions and limitations under the License.
namespace Topshelf.Bottles
{
using System;
using System.Collections.Generic;
using Magnum.Channels;
using Magnum.Extensions;
using Magnum.Fibers;
using Magnum.FileSystem;
using Magnum.FileSystem.Events;
using Magnum.FileSystem.Zip;
using System;
using Magnum.Channels;
using Magnum.Extensions;
using Magnum.Fibers;
using Magnum.FileSystem;
using Magnum.FileSystem.Events;
using Magnum.FileSystem.Zip;


public class BottleWatcher
{
ChannelAdapter _eventChannel;
Action<Directory> _actionToTake;
PollingFileSystemEventProducer _watcher;
Scheduler _scheduler;
Func<Fiber> _fiberFactory = () => new SynchronousFiber();
public BottleWatcher()
{
}
public class BottleWatcher
{
Action<Directory> _actionToTake;
ChannelAdapter _eventChannel;
Func<Fiber> _fiberFactory = () => new SynchronousFiber();
Scheduler _scheduler;
PollingFileSystemEventProducer _watcher;

public IDisposable Watch(string directoryToWatch, Action<Directory> actionToTake)
{
if (!System.IO.Directory.Exists(directoryToWatch))
System.IO.Directory.CreateDirectory(directoryToWatch);
public IDisposable Watch(string directoryToWatch, Action<Directory> actionToTake)
{
if (!System.IO.Directory.Exists(directoryToWatch))
System.IO.Directory.CreateDirectory(directoryToWatch);

_actionToTake = actionToTake;
_eventChannel = new ChannelAdapter();
_eventChannel.Connect(x => x.AddConsumerOf<FileCreated>().UsingConsumer(ProcessNewFile));
_actionToTake = actionToTake;
_eventChannel = new ChannelAdapter();
_eventChannel.Connect(x => x.AddConsumerOf<FileCreated>().UsingConsumer(ProcessNewFile));

_scheduler = new TimerScheduler(_fiberFactory());
_watcher = new PollingFileSystemEventProducer(directoryToWatch, _eventChannel, _scheduler, _fiberFactory(), 1.Seconds());
_scheduler = new TimerScheduler(_fiberFactory());
_watcher = new PollingFileSystemEventProducer(directoryToWatch, _eventChannel, _scheduler, _fiberFactory(),
1.Seconds());


return _watcher;
}
return _watcher;
}

void ProcessNewFile(FileCreated message)
{
if (message.Path.EndsWith("bottle"))
{
Directory dir = new ZipFileDirectory(PathName.GetPathName(message.Path));
try
{
_actionToTake(dir);
}
catch (Exception ex)
{
string msg = "There was an error processing the bottle '{0}'".FormatWith(message.Path);
throw new BottleException(msg, ex);
}
}
}
}
void ProcessNewFile(FileCreated message)
{
if (message.Path.EndsWith("bottle"))
{
Directory dir = new ZipFileDirectory(PathName.GetPathName(message.Path));
try
{
_actionToTake(dir);
}
catch (Exception ex)
{
string msg = "There was an error processing the bottle '{0}'".FormatWith(message.Path);
throw new BottleException(msg, ex);
}
}
}
}
}

0 comments on commit 694144c

Please sign in to comment.