Implemented Queues and folder monitors.

This commit is contained in:
Patrick Fic
2020-01-17 10:01:06 -08:00
parent 2b3d9ac76b
commit 572f409176
13 changed files with 388 additions and 41 deletions

View File

@@ -0,0 +1,85 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BodyshopUploader.Models;
namespace BodyshopUploader.Utils
{
public class CIECAMonitor : FileSystemWatcher
{
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
public CIECAMonitor()
{
Init();
}
public CIECAMonitor(String inDirectoryPath)
: base(inDirectoryPath)
{
Init();
}
public CIECAMonitor(String inDirectoryPath, string inFilter)
: base(inDirectoryPath, inFilter)
{
Init();
}
private void Init()
{
IncludeSubdirectories = false;
// Eliminate duplicates when timestamp doesn't change
Filter = "*.env";
NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite;
//NotifyFilter = NotifyFilters.FileName | NotifyFilters.Size; // The default also has NotifyFilters.LastWrite
EnableRaisingEvents = true;
Created += Watcher_Created;
Changed += Watcher_Changed;
//Deleted += Watcher_Deleted;
//Renamed += Watcher_Renamed;
Error += Wathcer_Error;
logger.Debug("CIECA Folder watcher started for path: {0}", Path);
}
private void Wathcer_Error(object sender, ErrorEventArgs e)
{
logger.Error("CIECA monitor encountered an error. Trying to restart. {0}", e.ToString());
((FileSystemWatcher)sender).EnableRaisingEvents = true;
}
public void Watcher_Created(object source, FileSystemEventArgs inArgs)
{
logger.Trace("New CIECA fileset detected | {0}", inArgs.FullPath, inArgs.ChangeType);
JobProcessingQueue.Enqueue(new DTO_QueueItem() { FilePath = inArgs.FullPath });
}
public void Watcher_Changed(object sender, FileSystemEventArgs inArgs)
{
logger.Trace("Updated CIECA fileset detected | {0}", inArgs.FullPath);
JobProcessingQueue.Enqueue(new DTO_QueueItem() { FilePath = inArgs.FullPath });
}
public void Watcher_Deleted(object sender, FileSystemEventArgs inArgs)
{
}
public void Watcher_Renamed(object sender, RenamedEventArgs inArgs)
{
}
private void InitializeComponent()
{
((System.ComponentModel.ISupportInitialize)(this)).BeginInit();
//
// FolderMonitor
//
this.IncludeSubdirectories = false;
((System.ComponentModel.ISupportInitialize)(this)).EndInit();
}
}
}

View File

@@ -0,0 +1,101 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using BodyshopUploader.Utils;
using BodyshopUploader.Models;
namespace BodyshopUploader.Utils
{
public static class JobProcessingQueue
{
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
private static Queue<DTO_QueueItem> _jobs = new Queue<DTO_QueueItem>();
private static bool _delegateQueuedOrRunning = false;
public static void Enqueue(DTO_QueueItem item)
{
lock (_jobs)
{
//See if the job is already in the queue based on the file name. If it is, ignore it.
if (!(_jobs.Any(_ => _.FilePath == item.FilePath)))
{
logger.Debug("Adding job to the queue. | {0}", item.FilePath);
_jobs.Enqueue(item);
if (!_delegateQueuedOrRunning)
{
_delegateQueuedOrRunning = true;
ThreadPool.UnsafeQueueUserWorkItem(ProcessQueuedItems, null);
}
}
else
{
logger.Debug("Ignoring job {0}, it's already in the queue.", item.FilePath);
}
}
}
private static void ProcessQueuedItems(object ignored)
{
while (true)
{
DTO_QueueItem item;
lock (_jobs)
{
if (_jobs.Count == 0)
{
_delegateQueuedOrRunning = false;
break;
}
//Only peek at the first item in the queue so that it does not get added again while it is still getting processed.
item = _jobs.Peek();
}
try
{
Thread.Sleep(1000);//Allow a small amount of time to pass before processing the queue item so that any writes can finish.
DecodeQueueItemJob(item);
UpsertQueueItem(item);
}
catch
{
ThreadPool.UnsafeQueueUserWorkItem(ProcessQueuedItems, null);
throw;
}
}
}
private static void DecodeQueueItemJob(DTO_QueueItem item)
{
//Process the job.
logger.Info("Should process the job here. {0}", item.FilePath);
}
private static void UpsertQueueItem(DTO_QueueItem item)
{
//Save the job to the DB.
logger.Info("Should upsert the job graphqlly here. {0}", item.FilePath);
_jobs.Dequeue();
}
private static void MoveFile(string FullPath)
{
try
{
System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(FullPath) + @"\Processed\");
File.Move(FullPath, System.IO.Path.GetDirectoryName(FullPath) + @"\Processed\" + System.IO.Path.GetFileNameWithoutExtension(FullPath) + DateTime.Now.Ticks.ToString() + ".bak");
}
catch (Exception Ex)
{
logger.Error(Ex, "Can't move file {0} - it's gone!", FullPath);
}
}
}
}

View File

@@ -11,8 +11,10 @@ namespace BodyshopUploader.Utils
{
public void Execute(object parameter)
{
new Views.Main().Show();
}
var m = App.Current.Windows.OfType<Views.Main>().FirstOrDefault();
m?.Show();
m?.Focus();
}
public bool CanExecute(object parameter)
{