diff --git a/chronos/NotificationThread.cpp b/chronos/NotificationThread.cpp index f37a89de..ecf1bd36 100644 --- a/chronos/NotificationThread.cpp +++ b/chronos/NotificationThread.cpp @@ -1,6 +1,6 @@ /* * chronos, the cron-job.org execution daemon - * Copyright (C) 2020 Patrick Schlangen + * Copyright (C) 2020-2024 Patrick Schlangen * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -11,6 +11,7 @@ #include "NotificationThread.h" +#include #include #include #include @@ -35,6 +36,12 @@ #include "ChronosMaster.h" +namespace { + +constexpr int PHRASE_SYNC_INTERVAL_SECONDS = 300; + +} // anon ns + class Mail { struct HeaderItem @@ -241,6 +248,7 @@ void NotificationThread::run() std::cout << "NotificationThread::run(): Entered" << std::endl; decltype(queue) tempQueue; + time_t tLastPhraseSync = 0; stop = false; while(!stop) @@ -259,20 +267,34 @@ void NotificationThread::run() std::cout << "NotificationThread::run(): " << numNotifications << " notification jobs fetched" << std::endl; time_t tStart = time(nullptr); - if(!tempQueue.empty()) + while(!tempQueue.empty()) { - masterTransport->open(); - - syncPhrases(); - - while(!tempQueue.empty()) + try { - Notification notification = std::move(tempQueue.front()); - tempQueue.pop(); - processNotification(notification); + masterTransport->open(); + + if(phrases.empty() || (tLastPhraseSync + PHRASE_SYNC_INTERVAL_SECONDS < time(nullptr))) + { + syncPhrases(); + tLastPhraseSync = time(nullptr); + } + + while(!tempQueue.empty()) + { + Notification notification = std::move(tempQueue.front()); + tempQueue.pop(); + processNotification(notification); + } + + masterTransport->close(); } + catch (const apache::thrift::TException &ex) + { + std::cerr << "NotificationThread::run(): Caught thrift exception: " << ex.what() << std::endl; - masterTransport->close(); + // Sleep a bit to avoid too frequent retries + std::this_thread::sleep_for(std::chrono::milliseconds(250)); + } } time_t tEnd = time(nullptr);