-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
60 lines (50 loc) · 1.53 KB
/
Program.cs
File metadata and controls
60 lines (50 loc) · 1.53 KB
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
using Serilog;
using OfficeToPdf.Models;
using OfficeToPdf.Services;
using OfficeToPdf.Workers;
namespace OfficeToPdf
{
class Program
{
static int Main(string[] args)
{
if (args.Length < 2)
{
Console.WriteLine("Usage: OfficeToPdf <input-folder> <output-folder> [maxWorkers]");
return 1;
}
LoggingService.Initialize();
var input = args[0];
var output = args[1];
int maxWorkers = args.Length > 2 ? int.Parse(args[2]) : Math.Max(1, Environment.ProcessorCount / 2);
var queue = new JobQueue();
var files = Directory.EnumerateFiles(input, "*.*", SearchOption.TopDirectoryOnly)
.Where(f => new[] { ".doc", ".docx", ".xls", ".xlsx", ".xlsm" }
.Contains(Path.GetExtension(f).ToLowerInvariant()));
foreach (var file in files)
{
var outPath = Path.Combine(output, Path.GetFileNameWithoutExtension(file) + ".pdf");
queue.Enqueue(new FileJob(file, outPath));
}
Log.Information("Queued {Count} files. Starting {Workers} workers...", files.Count(), maxWorkers);
using var cts = new CancellationTokenSource();
var workers = Enumerable.Range(1, maxWorkers)
.Select(i => new OfficeWorker(queue, cts.Token, i, 2, 60))
.ToList();
foreach (var w in workers) w.Start();
while (!queue.IsEmpty)
{
Thread.Sleep(1000);
if (Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Q)
{
Log.Warning("Manual cancel received.");
cts.Cancel();
break;
}
}
workers.ForEach(w => w.StopAndWait());
LoggingService.Shutdown();
return 0;
}
}
}