-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotifier.cs
More file actions
82 lines (71 loc) · 2.29 KB
/
Copy pathNotifier.cs
File metadata and controls
82 lines (71 loc) · 2.29 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ProcessesMonitoring
{
public static class Notifier
{
private static HttpClient m_pClient = new HttpClient();
private static string m_sUrl;
private static string rotationMessage = "";
private static bool keepRotating = true;
public static void setDestinationUrl(string url)
{
m_sUrl = url;
}
public static void start(int rotationTimeMS)
{
Task oAsyncTaskRotation = Task.Factory.StartNew(() => startRotationThread(rotationTimeMS));
}
public static void startRotationThread(int rotationTimeMS)
{
while (keepRotating)
{
notifyRotational();
Thread.Sleep(rotationTimeMS);
}
}
public static void Notify(string message)
{
if (ConfigurationManager.AppSettings["SlackEnabled"] == "1")
{
string content = $"{{\"text\":\"{message}\"}}";
string res = sendPost(content).Result;
}
}
public static void AppendForNextNotify(string message) {
lock (rotationMessage)
{
rotationMessage += message + Environment.NewLine;
}
}
private static void notifyRotational()
{
string message = "";
lock (rotationMessage)
{
message = rotationMessage;
rotationMessage = "";
}
if (!string.IsNullOrEmpty(message)) {
Notify(message);
}
}
private static async Task<string> sendPost(string jsonedContent)
{
if (string.IsNullOrEmpty(m_sUrl))
{
return "Url was not set.";
}
var httpContent = new StringContent(jsonedContent, Encoding.UTF8, "application/json");
var result = await m_pClient.PostAsync(m_sUrl, httpContent);
var received_json = result.Content.ReadAsStringAsync().Result;
return received_json;
}
}
}