/
Program.cs
97 lines (76 loc) · 3.37 KB
/
Program.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
using System;
using System.Linq;
using System.Collections.Generic;
using System.Net.NetworkInformation;
using System.Threading.Tasks;
using System.IO;
using System.Diagnostics;
using Minio;
using DevWeek.Services.Downloader;
using Polly;
namespace DevWeek
{
class Program
{
static void Main(string[] args)
{
Spring.Context.Support.AbstractApplicationContext appContext = null;
var retryOnStartupPolicy = Policy
//.HandleInner<StackExchange.Redis.RedisConnectionException>()
.Handle<Exception>()
.WaitAndRetry(9, retryAttempt =>
TimeSpan.FromSeconds(Math.Pow(2, retryAttempt))
);
//2^9=512 segundos (8,53333 minutos)
retryOnStartupPolicy.Execute(() =>
{
appContext = ContextBuilder.BuildContext();
});
Task.Run(async () => {
while(true)
{
await appContext.GetObject<Services.DataService>().RebuildCache();
System.Threading.Thread.Sleep(TimeSpan.FromMinutes(2));
}
});
#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
Task.Run(async () =>
{
var rabbitMQ = appContext.GetObject<RabbitMQ.Client.IModel>();
string queue = appContext.GetObject<string>("CONFIG:DevWeek:RabbitMQ:DownloadPipeline:Queue");
while (true)
{
RabbitMQ.Client.BasicGetResult result = rabbitMQ.BasicGet(queue, false);
if (result != null)
{
try
{
string messageBody = System.Text.Encoding.UTF8.GetString(result.Body);
Services.Downloader.Download messageObject = Newtonsoft.Json.JsonConvert.DeserializeObject<Services.Downloader.Download>(messageBody);
var pipeline = appContext.GetObject<DevWeek.Services.Downloader.DownloadPipeline>();
var context = new DownloadContext()
{
MediaUrl = messageObject.OriginalMediaUrl,
Download = messageObject
};
await pipeline.Run(context);
rabbitMQ.BasicAck(result.DeliveryTag, false);
}
catch (InvalidOperationException ex) when (ex.Message.Contains("#invalidUrl") || ex.Message.Contains("#invalidId"))
{
rabbitMQ.BasicNack(result.DeliveryTag, false, false);
}
catch (Exception ex)
{
rabbitMQ.BasicNack(result.DeliveryTag, false, true);
}
}
else
System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1));
}
});
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
Console.WriteLine("Hello World!");
}
}
}