Processing filesystem messages

The FileSystemChangeMessage will alert us when a filesystem event has taken place. Examples of such would be files being FTP'd into a directory for us, files being deleted and created, and folders being renamed. The most realistic example I can provide is us monitoring a directory for files to arrive, and once they do, triggering our ETL jobs to load the data:

bool ProcessFileSystemMessage([NotNull] FileSystemChangeMessage msg)
{
Console.WriteLine("Received FileSystemChange Message");
RILogManager.Default?.SendInformation("Received FileSystemChange Message");
Console.WriteLine("*********************");
Console.WriteLine("Changed Date = {0}", msg.ChangeDate);
Console.WriteLine("ChangeType = {0}", msg.ChangeType);
Console.WriteLine("FullPath = {0}", msg.FullPath);
Console.WriteLine("OldPath = {0}", msg.OldPath);
Console.WriteLine("Name = {0}", msg.Name);
Console.WriteLine("Old Name = {0}", msg.OldName);
Console.WriteLine("FileSystemEventType {0}", (int)msg.EventType);
Console.WriteLine("*********************");
// This is our new highlighted section
using (var database = new LiteDatabase(connectionString))
{
database.Shrink();
var collection = database.GetCollection<FileSystemChangeMessage>();
collection.EnsureIndex(x => x.ID);
collection.Insert(msg);
}
return true;
}

If you notice the highlighted section previously, this is actually in every processing function, but for brevity I chose not to show it until here. It writes out whatever record we have to our document database (LiteDB) according to message type. We simply connect to our database, reduce the occupied space of the database, find the message type we are interested in, ensure it has an index, and then insert the record.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset