Created
December 22, 2013 15:40
-
-
Save omerfarukz/8084311 to your computer and use it in GitHub Desktop.
manual file changes detection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Depthloy.Common.Cache; | |
using Depthloy.Common.Helpers; | |
using Depthloy.Common.Interfaces.Cache; | |
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Threading; | |
namespace Depthloy.Server.WebServices | |
{ | |
public class DepthloyFileSystemWatcher | |
{ | |
private string _cacheKey; | |
private string _directory; | |
private Thread _thread; | |
private bool _isRunning; | |
public bool IsEnabled { get; set; } | |
public int Interval { get; set; } | |
public int CacheTimeInSeconds { get; set; } | |
public event EventHandler<DepthloyFileSystemWatcherEventArgs> OnFilesChanged; | |
public ICache Cache { get; set; } | |
public DepthloyFileSystemWatcher(string directory) | |
{ | |
Interval = 3000; | |
CacheTimeInSeconds = 600; // 10 min | |
Cache = new InMemoryCache(); | |
_directory = directory; | |
_cacheKey = "DirectoryFilesDateLastWrite_" + _directory; | |
_thread = new Thread(new ThreadStart(ThreadTarget)); | |
_thread.Start(); | |
} | |
private void ThreadTarget() | |
{ | |
while (true) | |
{ | |
try | |
{ | |
if (IsEnabled && !_isRunning) | |
{ | |
_isRunning = true; | |
var files = ScanFiles(); | |
if (Cache.Contains(_cacheKey)) | |
{ | |
var cachedFiles = Cache.Get<List<FileDateLastWriteData>>(_cacheKey, null); | |
var differences = DetectDifferences(files, cachedFiles); | |
if (differences != null && differences.Count > 0) | |
{ | |
if (OnFilesChanged != null) | |
{ | |
OnFilesChanged(null, new DepthloyFileSystemWatcherEventArgs(differences)); | |
} | |
} | |
} | |
Cache.Set(_cacheKey, files, CacheTimeInSeconds); | |
_isRunning = false; | |
} | |
Thread.Sleep(Interval); | |
} | |
catch (Exception) | |
{ | |
//TODO: Log | |
} | |
} | |
} | |
private List<DifferenceResult<FileDateLastWriteData>> DetectDifferences(List<FileDateLastWriteData> currentFiles, List<FileDateLastWriteData> cachedFiles) | |
{ | |
var detector = new GenericDifferenceDetector<FileDateLastWriteData>(); | |
detector.CompareFunc = (Source, Destination) => { return Source.FullPath.CompareTo(Destination.FullPath); }; | |
detector.DetectChangesFunc = (Source, Destination) => { return Source.DateLastWrite == Destination.DateLastWrite; }; | |
detector.SortFunc = (Source) => { return Source.FullPath; }; | |
var diffResult = detector.GetDifferences(currentFiles, cachedFiles); | |
if (diffResult != null && diffResult.Count > 0) | |
{ | |
return diffResult; | |
} | |
return null; | |
} | |
private List<FileDateLastWriteData> ScanFiles() | |
{ | |
var r = new List<FileDateLastWriteData>(); | |
foreach (var filePath in IOHelper.GetFilesInDirectory(_directory, true)) | |
{ | |
r.Add(GetFileInfo(filePath)); | |
} | |
return r; | |
} | |
private FileDateLastWriteData GetFileInfo(string filePath) | |
{ | |
return new FileDateLastWriteData(filePath, File.GetLastWriteTime(filePath)); | |
} | |
public void Start() | |
{ | |
IsEnabled = true; | |
} | |
public void Stop() | |
{ | |
IsEnabled = false; | |
} | |
} | |
public class FileDateLastWriteData | |
{ | |
public string FullPath { get; set; } | |
public DateTime DateLastWrite { get; set; } | |
public FileDateLastWriteData(string fullPath, DateTime dateLastWrite) | |
{ | |
FullPath = fullPath; | |
DateLastWrite = dateLastWrite; | |
} | |
} | |
public class DepthloyFileSystemWatcherEventArgs : EventArgs | |
{ | |
public List<DifferenceResult<FileDateLastWriteData>> DifferenceResult { get; private set; } | |
public DepthloyFileSystemWatcherEventArgs(List<DifferenceResult<FileDateLastWriteData>> differences) | |
{ | |
this.DifferenceResult = differences; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment