2015-04-18 16:50:19 +02:00
|
|
|
#include "PeriodicTask.h"
|
|
|
|
|
|
|
|
using std::function;
|
|
|
|
using std::cerr;
|
|
|
|
using std::endl;
|
|
|
|
|
|
|
|
namespace blockstore {
|
|
|
|
namespace caching {
|
|
|
|
|
|
|
|
PeriodicTask::PeriodicTask(function<void ()> task, double intervalSec) : _thread(), _task(task), _intervalSec(intervalSec) {
|
|
|
|
_thread = boost::thread([this]() {
|
2015-04-26 12:32:20 +02:00
|
|
|
boost::chrono::nanoseconds interval((uint64_t)(UINT64_C(1000000000) * _intervalSec));
|
|
|
|
try {
|
2015-04-18 16:50:19 +02:00
|
|
|
while(true) {
|
|
|
|
boost::this_thread::sleep_for(interval);
|
|
|
|
_task();
|
|
|
|
}
|
2015-04-26 12:32:20 +02:00
|
|
|
} catch (const boost::thread_interrupted &e) {
|
|
|
|
//Do nothing, exit thread.
|
2015-09-29 20:01:51 +02:00
|
|
|
} catch (const std::exception &e) {
|
|
|
|
//TODO Think about logging
|
|
|
|
cerr << "PeriodicTask crashed: " << e.what() << endl;
|
2015-04-26 12:32:20 +02:00
|
|
|
} catch (...) {
|
|
|
|
//TODO Think about logging
|
|
|
|
cerr << "PeriodicTask crashed" << endl;
|
|
|
|
}
|
2015-04-18 16:50:19 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
PeriodicTask::~PeriodicTask() {
|
|
|
|
_thread.interrupt();
|
|
|
|
_thread.join();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|