forked from pschlan/cron-job.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNotificationThread.h
86 lines (72 loc) · 2.29 KB
/
NotificationThread.h
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
/*
* chronos, the cron-job.org execution daemon
* Copyright (C) 2020 Patrick Schlangen <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
*/
#ifndef _NOTIFICATIONTHREAD_H_
#define _NOTIFICATIONTHREAD_H_
#include <condition_variable>
#include <memory>
#include <mutex>
#include <queue>
#include <string>
#include <thread>
#include <unordered_map>
#include "Notification.h"
class ChronosMasterClient;
class Mail;
namespace apache { namespace thrift {
namespace protocol {
class TProtocol;
}
namespace transport {
class TTransport;
}
} }
namespace Chronos
{
class NotificationThread
{
public:
NotificationThread();
~NotificationThread();
private:
NotificationThread(const NotificationThread &other) = delete;
NotificationThread(NotificationThread &&other) = delete;
NotificationThread &operator=(const NotificationThread &other) = delete;
NotificationThread &operator=(NotificationThread &&other) = delete;
public:
static NotificationThread *getInstance();
void run();
void stopThread();
void addNotification(Notification &¬ification);
private:
void syncPhrases();
std::string getPhrase(const std::string &lang, const std::string &key) const;
std::string formatDate(const std::string &lang, const uint64_t date) const;
std::string formatStatus(const std::string &lang, const Notification ¬ification) const;
void processNotification(const Notification ¬ification);
void sendMail(const Mail &mail) const;
private:
bool stop = false;
static NotificationThread *instance;
std::mutex queueMutex;
std::condition_variable queueSignal;
std::queue<Notification> queue;
std::shared_ptr<apache::thrift::transport::TTransport> masterSocket;
std::shared_ptr<apache::thrift::transport::TTransport> masterTransport;
std::shared_ptr<apache::thrift::protocol::TProtocol> masterProtocol;
std::shared_ptr<ChronosMasterClient> masterClient;
std::string defaultLang;
std::string mailFrom;
std::string mailSender;
std::string smtpServer;
std::unordered_map<std::string, std::unordered_map<std::string, std::string>> phrases;
};
};
#endif