Compare commits
2 Commits
ea7ff0b051
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 523441dd2f | |||
| 6e91d188fe |
31
Program.cs
31
Program.cs
@@ -10,26 +10,36 @@ using Serilog.Events;
|
||||
var resetEvent = new ManualResetEvent(false);
|
||||
using var tokenSource = new CancellationTokenSource();
|
||||
|
||||
Console.CancelKeyPress += (sender, eventArgs) =>
|
||||
Console.CancelKeyPress += (_, eventArgs) =>
|
||||
{
|
||||
resetEvent?.Set();
|
||||
tokenSource?.Cancel();
|
||||
Log.Debug("Canceling with CancelKeyPress");
|
||||
Dispose();
|
||||
eventArgs.Cancel = true;
|
||||
Log.CloseAndFlush();
|
||||
};
|
||||
|
||||
AppDomain.CurrentDomain.ProcessExit += (_, _) => Dispose();
|
||||
|
||||
SetupLogging();
|
||||
|
||||
//TODO: check environment variables to see if config file is enabled, otherwise use env vars
|
||||
var config = LoadConfig();
|
||||
Log.Information("Configuration file loaded. Beginning initial grab.");
|
||||
await RunJob(config, tokenSource.Token);
|
||||
Log.Information("Initial grab complete. Initializing schedule.");
|
||||
Task.Run(() => ScheduleJobs(config, tokenSource.Token));
|
||||
|
||||
Log.Verbose("Jobs scheduled. Main Thread waiting.");
|
||||
resetEvent.WaitOne();
|
||||
|
||||
return;
|
||||
|
||||
void Dispose()
|
||||
{
|
||||
resetEvent?.Set();
|
||||
tokenSource?.Cancel();
|
||||
Log.CloseAndFlush();
|
||||
}
|
||||
|
||||
static void SetupLogging()
|
||||
{
|
||||
var minLevel = (Environment.GetEnvironmentVariable("LOG_LEVEL") ?? "info").ToUpperInvariant() switch
|
||||
@@ -63,13 +73,14 @@ static Config LoadConfig()
|
||||
WriteIndented = true
|
||||
};
|
||||
|
||||
//TODO: stop creating dummy config file
|
||||
if (!File.Exists("/config.json"))
|
||||
{
|
||||
Log.Fatal("No config file found, creating dummy config.");
|
||||
CreateConfig(serializationOptions);
|
||||
Environment.Exit(1);
|
||||
}
|
||||
|
||||
Log.Verbose("Loading config from file.");
|
||||
var configFile = File.ReadAllText("/config.json");
|
||||
return JsonSerializer.Deserialize<Config>(configFile, serializationOptions);
|
||||
}
|
||||
@@ -84,6 +95,7 @@ static void CreateConfig(JsonSerializerOptions options)
|
||||
|
||||
static async Task ScheduleJobs(Config config, CancellationToken token)
|
||||
{
|
||||
Log.Debug($"Scheduling job with cron expression '{config.Schedule}'");
|
||||
var schedule = CronExpression.Parse(config.Schedule);
|
||||
|
||||
while (!token.IsCancellationRequested)
|
||||
@@ -95,11 +107,12 @@ static async Task ScheduleJobs(Config config, CancellationToken token)
|
||||
|
||||
Log.Information($"Next scheduled scan at {nextJob.Value}");
|
||||
var delay = nextJob.Value - now;
|
||||
Log.Debug($"{delay} until next scan at {nextJob.Value}");
|
||||
if (delay > TimeSpan.Zero)
|
||||
{
|
||||
await Task.Delay(delay, token);
|
||||
}
|
||||
|
||||
Log.Debug($"Delay {delay} wait is complete, beginning scan now.");
|
||||
await RunJob(config, token);
|
||||
}
|
||||
}
|
||||
@@ -131,11 +144,13 @@ static async Task RecurseDirectory(SftpClient client, string source, string dest
|
||||
|
||||
if (item.IsDirectory)
|
||||
{
|
||||
Log.Verbose($"{item.Name} is a directory");
|
||||
var newPath = Path.Combine(destination, item.Name);
|
||||
await RecurseDirectory(client, item.FullName, newPath, true, token);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Log.Verbose($"{item.Name} is a file");
|
||||
await DownloadFile(client, item, destination, token);
|
||||
Log.Information($"Deleting '{item.Name}'");
|
||||
await client.DeleteAsync(item.FullName, token);
|
||||
@@ -151,9 +166,11 @@ static async Task RecurseDirectory(SftpClient client, string source, string dest
|
||||
static async Task DownloadFile(SftpClient client, ISftpFile item, string destination, CancellationToken token)
|
||||
{
|
||||
Directory.CreateDirectory(destination);
|
||||
Log.Verbose($"Ensuring '{destination}' path exists.");
|
||||
await using var stream = File.Open(Path.Combine(destination, item.Name), FileMode.Create, FileAccess.Write);
|
||||
Log.Information($"Downloading '{item.FullName}' to '{stream.Name}'");
|
||||
await client.DownloadFileAsync(item.FullName, stream, token);
|
||||
Log.Verbose("Download completed.");
|
||||
}
|
||||
|
||||
static SftpClient GetClient(SFtpTarget target)
|
||||
|
||||
Reference in New Issue
Block a user