Skip to content

Commit

Permalink
NotificationThread> Reduce phrase sync frequency, gracefully handle T…
Browse files Browse the repository at this point in the history
…hrift exceptions

Closes #240
  • Loading branch information
pschlan committed Jun 5, 2024
1 parent 161b995 commit 5b3552c
Showing 1 changed file with 33 additions and 11 deletions.
44 changes: 33 additions & 11 deletions chronos/NotificationThread.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* chronos, the cron-job.org execution daemon
* Copyright (C) 2020 Patrick Schlangen <patrick@schlangen.me>
* Copyright (C) 2020-2024 Patrick Schlangen <patrick@schlangen.me>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
Expand All @@ -11,6 +11,7 @@

#include "NotificationThread.h"

#include <chrono>
#include <iomanip>
#include <iostream>
#include <sstream>
Expand All @@ -35,6 +36,12 @@

#include "ChronosMaster.h"

namespace {

constexpr int PHRASE_SYNC_INTERVAL_SECONDS = 300;

} // anon ns

class Mail
{
struct HeaderItem
Expand Down Expand Up @@ -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)
Expand All @@ -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);

Expand Down

0 comments on commit 5b3552c

Please sign in to comment.