From 5f66e80db77def7a49731195ba9cb7cece0d47b7 Mon Sep 17 00:00:00 2001 From: randomdan Date: Tue, 24 Dec 2013 18:24:21 +0000 Subject: Execeute updates of network, services and events on a schedule and on start up Includes fix for correctly handling threaded DB connection commits --- p2pvr/daemon/daemon.cpp | 2 +- p2pvr/lib/dbClient.h | 1 - p2pvr/lib/maintenance.cpp | 60 ++++++++++++++++++++++++++++++++++++++ p2pvr/lib/maintenance.h | 20 +++++++++++++ p2pvr/lib/maintenance/network.cpp | 6 ++-- p2pvr/lib/maintenance/services.cpp | 6 ++-- 6 files changed, 87 insertions(+), 8 deletions(-) diff --git a/p2pvr/daemon/daemon.cpp b/p2pvr/daemon/daemon.cpp index 8597a25..6f0d326 100644 --- a/p2pvr/daemon/daemon.cpp +++ b/p2pvr/daemon/daemon.cpp @@ -29,7 +29,7 @@ class P2PvrDaemon : public Daemon { auto adapter = ic->createObjectAdapterWithEndpoints(Adapter, Endpoint); adapter->add(new LocalDevices(adapter, timer), ic->stringToIdentity("Devices")); adapter->add(new GlobalDevices(), ic->stringToIdentity("GlobalDevices")); - adapter->add(new Maintenance(), ic->stringToIdentity("Maintenance")); + adapter->add(new Maintenance(adapter, timer), ic->stringToIdentity("Maintenance")); adapter->add(new SI(), ic->stringToIdentity("SI")); adapter->add(new Schedules(), ic->stringToIdentity("Schedules")); adapter->activate(); diff --git a/p2pvr/lib/dbClient.h b/p2pvr/lib/dbClient.h index 58cce0a..cf18669 100644 --- a/p2pvr/lib/dbClient.h +++ b/p2pvr/lib/dbClient.h @@ -21,7 +21,6 @@ class DatabaseClient : public virtual CommonObjects { static void SqlMergeColumnsInserter(SqlMergeTask * merge, const std::string & name, bool key); - protected: class TxHelper { public: TxHelper(const DatabaseClient *); diff --git a/p2pvr/lib/maintenance.cpp b/p2pvr/lib/maintenance.cpp index eb1b570..2bc026e 100644 --- a/p2pvr/lib/maintenance.cpp +++ b/p2pvr/lib/maintenance.cpp @@ -1,8 +1,37 @@ #include +#include +#include #include "maintenance.h" #include +#include "bindTimerTask.h" #include +time_t Maintenance::periodUpdateNetwork; +time_t Maintenance::periodUpdateServices; +time_t Maintenance::periodUpdateEvents; + +DECLARE_OPTIONS(Maintenance, "P2PVR Maintenance options") +("p2pvr.maintenance.periodUpdateNetwork", Options::value(&periodUpdateNetwork, 86400 * 7), + "Period between automated updates of DVB network (1 week)") +("p2pvr.maintenance.periodUpdateServices", Options::value(&periodUpdateServices, 86400 * 7), + "Period between automated updates of DVB services (1 week)") +("p2pvr.maintenance.periodUpdateEvents", Options::value(&periodUpdateEvents, 3600 * 12), + "Period between automated updates of DVB events (12 hours)") +END_OPTIONS(Maintenance); + +Maintenance::Maintenance(Ice::ObjectAdapterPtr a, IceUtil::TimerPtr t) : + adapter(a), + timer(t), + clientCheck(new BindTimerTask(boost::bind(&Maintenance::ScheduledUpdate, this))), + lastUpdateNetwork(0), + lastUpdateServices(0), + lastUpdateEvents(0), + updateRunning(false) +{ + timer->scheduleRepeated(clientCheck, IceUtil::Time::seconds(5 * 60)); + ScheduledUpdate(); +} + void Maintenance::UpdateAll(const Ice::Current & ice) { @@ -22,3 +51,34 @@ Maintenance::UpdateAll(short type, const Ice::Current & ice) UpdateEvents(type, ice); } +void +Maintenance::ScheduledUpdate() +{ + Logger()->messagebf(LOG_DEBUG, "%s: triggered", __PRETTY_FUNCTION__); + if (!updateRunning) { + std::thread update([this] { + ScopeObject notRunning([this]{ updateRunning = false; }); + updateRunning = true; + auto si = P2PVR::MaintenancePrx::checkedCast(adapter->createProxy(adapter->getCommunicator()->stringToIdentity("Maintenance"))); + time_t now = time(NULL); + if (lastUpdateNetwork < now - periodUpdateNetwork) { + Logger()->messagebf(LOG_INFO, "%s: updating network", __PRETTY_FUNCTION__); + si->UpdateNetwork(FE_OFDM); + time(&lastUpdateNetwork); + } + if (lastUpdateServices < now - periodUpdateServices) { + Logger()->messagebf(LOG_INFO, "%s: updating services", __PRETTY_FUNCTION__); + si->UpdateServices(FE_OFDM); + time(&lastUpdateServices); + } + if (lastUpdateEvents < now - periodUpdateEvents) { + Logger()->messagebf(LOG_INFO, "%s: updating events", __PRETTY_FUNCTION__); + si->UpdateEvents(FE_OFDM); + time(&lastUpdateEvents); + } + Logger()->messagebf(LOG_DEBUG, "%s: completed", __PRETTY_FUNCTION__); + }); + update.detach(); + } +} + diff --git a/p2pvr/lib/maintenance.h b/p2pvr/lib/maintenance.h index 9083ded..563455a 100644 --- a/p2pvr/lib/maintenance.h +++ b/p2pvr/lib/maintenance.h @@ -6,6 +6,8 @@ class Maintenance : public P2PVR::Maintenance, public DatabaseClient { public: + Maintenance(Ice::ObjectAdapterPtr, IceUtil::TimerPtr); + void UpdateAll(const Ice::Current &); void UpdateAll(short type, const Ice::Current &); void UpdateNetwork(short type, const Ice::Current &); @@ -13,6 +15,24 @@ class Maintenance : public P2PVR::Maintenance, public DatabaseClient { void UpdateProgramAssociations(short type, const Ice::Current &); void UpdateProgramMaps(short type, const Ice::Current &); void UpdateEvents(short type, const Ice::Current &); + + INITOPTIONS; + + private: + void ScheduledUpdate(); + + Ice::ObjectAdapterPtr adapter; + IceUtil::TimerPtr timer; + IceUtil::TimerTaskPtr clientCheck; + + time_t lastUpdateNetwork; + time_t lastUpdateServices; + time_t lastUpdateEvents; + bool updateRunning; + + static time_t periodUpdateNetwork; + static time_t periodUpdateServices; + static time_t periodUpdateEvents; }; #endif diff --git a/p2pvr/lib/maintenance/network.cpp b/p2pvr/lib/maintenance/network.cpp index 6704b7b..e947456 100644 --- a/p2pvr/lib/maintenance/network.cpp +++ b/p2pvr/lib/maintenance/network.cpp @@ -11,7 +11,7 @@ class SiNetworkInformationMerger : public SiNetworkInformationParser { public: - SiNetworkInformationMerger(CommonObjects * co) : commonObjects(co) { } + SiNetworkInformationMerger(DatabaseClient * co) : commonObjects(co) { } bool HandleTable(DVBSI::NetworkPtr n) { @@ -26,6 +26,7 @@ class SiNetworkInformationMerger : public SiNetworkInformationParser { } } + DatabaseClient::TxHelper tx(commonObjects); SqlMergeTask mergeNetwork("postgres", "networks"); CreateColumns(boost::bind(&DatabaseClient::SqlMergeColumnsInserter, &mergeNetwork, _1, _2)); std::vector networks = { n }; @@ -59,7 +60,7 @@ class SiNetworkInformationMerger : public SiNetworkInformationParser { return false; } private: - CommonObjects * commonObjects; + DatabaseClient * commonObjects; }; void @@ -75,7 +76,6 @@ Maintenance::UpdateNetwork(short type, const Ice::Current & ice) throw std::runtime_error("bad proxy(s)"); } const auto transports = si->GetAllDeliveries(type); - TxHelper tx(this); // Attempt to just download fresh data BOOST_FOREACH(const auto & transport, transports) { P2PVR::TunerPrx tuner; diff --git a/p2pvr/lib/maintenance/services.cpp b/p2pvr/lib/maintenance/services.cpp index ba958a8..06f2faf 100644 --- a/p2pvr/lib/maintenance/services.cpp +++ b/p2pvr/lib/maintenance/services.cpp @@ -11,7 +11,7 @@ class SiServicesMerger : public SiServicesParser { public: - SiServicesMerger(CommonObjects * co) : commonObjects(co) { } + SiServicesMerger(DatabaseClient * co) : commonObjects(co) { } bool HandleTable(DVBSI::TransportStreamPtr ts) { @@ -23,6 +23,7 @@ class SiServicesMerger : public SiServicesParser { s->RunningStatus, s->FreeCaMode); } + DatabaseClient::TxHelper tx(commonObjects); SqlMergeTask mergeServices("postgres", "services"); CreateColumns(boost::bind(&DatabaseClient::SqlMergeColumnsInserter, &mergeServices, _1, _2)); // Don't change the list of services available from the network @@ -35,7 +36,7 @@ class SiServicesMerger : public SiServicesParser { } private: - CommonObjects * commonObjects; + DatabaseClient * commonObjects; }; void @@ -57,7 +58,6 @@ Maintenance::UpdateServices(short type, const Ice::Current & ice) throw std::runtime_error("no delivery methods"); } - TxHelper tx(this); Logger()->messagebf(LOG_DEBUG, "%s: Getting a tuner", __PRETTY_FUNCTION__); auto tuner = devs->GetTunerAny(type, deliveries.front(), time(NULL) + 90); Logger()->messagebf(LOG_DEBUG, "%s: Fetching service list", __PRETTY_FUNCTION__); -- cgit v1.2.3