-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileWatcher.cs
101 lines (86 loc) · 3.46 KB
/
FileWatcher.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using System;
using System.IO;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace DumpLoader
{
public class FileWatcher
{
private ServerOptions _options;
public FileWatcher(ServerOptions options)
{
try
{
this._options = options;
LogConsole.LogInformation($"Create directories in [{Directory.GetCurrentDirectory()}]", null);
List<string> directories = new List<string>() {"done", "error", "watch"};
foreach (string path in directories)
{
Directory.CreateDirectory(path);
LogConsole.LogInformation($"Directory [{path}]...", "OK");
}
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = Path.Combine(Directory.GetCurrentDirectory(), "watch");
watcher.Filter = "*.bak";
watcher.NotifyFilter = NotifyFilters.LastWrite;
watcher.Changed += new FileSystemEventHandler(FileChanged);
watcher.EnableRaisingEvents = true;
LogConsole.LogInformation($"Starting watcher in [{watcher.Path} ({watcher.Filter})]...", "OK");
Console.ReadKey(false);
LogConsole.LogInformation($"Stopping watcher...", "OK");
Console.WriteLine();
}
catch (Exception ex)
{
LogConsole.LogError(ex);
}
}
private void FileChanged(object sender, FileSystemEventArgs e)
{
var tokenSource = new CancellationTokenSource();
try
{
if(!FileReady(e.FullPath)) return;
LogConsole.LogInformation("Importing...", Path.GetFileNameWithoutExtension(e.FullPath));
var taskLoading = Task.Run(() => {
while(!tokenSource.IsCancellationRequested) {
Console.Write(".");
Thread.Sleep(2000);
}
}, tokenSource.Token);
var restoreDumpTask = Task.Run(() => { new ImportDump(this._options, e.FullPath); });
restoreDumpTask.Wait();
tokenSource.Cancel();
MoveFile(e.FullPath, "done");
LogConsole.LogInformation($"Dump {Path.GetFileNameWithoutExtension(e.FullPath)}...", "OK");
}
catch (Exception ex)
{
tokenSource.Cancel();
MoveFile(e.FullPath, "error", reason: ex.Message);
LogConsole.LogError(ex);
}
}
private void MoveFile(string source, string destination, string reason = null)
{
var newPath = Path.Combine(Directory.GetCurrentDirectory(), destination);
File.Move(source, Path.Combine(newPath, Path.GetFileName(source)), true);
if (!string.IsNullOrEmpty(reason)) File.WriteAllText(Path.Combine(newPath, Path.GetFileNameWithoutExtension(source) + ".txt"), reason);
}
private bool FileReady(string path)
{
try
{
using(var file = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read))
{
return true;
}
}
catch (IOException)
{
return false;
}
}
}
}