#include "../../shared/DatabaseWithRetry.h" #include "../../shared/GlobalConfigFile.h" #include "WestonDatabaseMonitor.h" ///////////////////////////////////////////////////////////////////// // WestonExcludeList ///////////////////////////////////////////////////////////////////// WestonExcludeList::WestonExcludeList() : BroadcastMessage(WestonDatabaseMonitor::getExcludeListChannel()) { } ///////////////////////////////////////////////////////////////////// // WestonDatabaseMonitor ///////////////////////////////////////////////////////////////////// WestonDatabaseMonitor::WestonDatabaseMonitor(DataNodeManager *dataNodeManager) : ThreadClass("WestonDatabaseMonitor"), _incoming(getName()), _dataNodeManager(dataNodeManager) { startThread(); } WestonDatabaseMonitor::~WestonDatabaseMonitor() { Request *r = new Request(NULL); r->callbackId = mtQuit; _incoming.newRequest(r); waitForThread(); } static std::string getExternalName() { // Please see MiscSupport.h. geHostName() and getShortHostName() are now // available as standard library routines. Don't copy the code below. std::string result; char hostName[100]; const bool error = gethostname(hostName, 100); if (error) result = "unknown"; else { // If the host name is too long, according to the man page, it will be // truncated. Whether or not the truncated name is NULL terminated is // explicitly not defined. hostName[99] = 0; // Say "fogell" not "fogell.trade-ideas.com" result = explode(".", hostName)[0]; } const std::string port = getConfigItem("listen_port"); if (!port.empty()) result += ':' + port; return result; } //#include void WestonDatabaseMonitor::threadFunction() { ThreadMonitor &tm = ThreadMonitor::find(); tm.setState("init"); DatabaseWithRetry masterDatabase(DatabaseWithRetry::MASTER, getName()); DatabaseWithRetry slaveDatabase(DatabaseWithRetry::SLAVE, getName()); DatabaseWithRetry database(false, getName()); time_t nextCheckTime = 0; const std::string deadManTimerSql = "UPDATE monitor_alive SET last_update=NOW() WHERE name='" + mysqlEscapeString(getConfigItem("name_for_monitor", "Weston " + getExternalName())) + "'"; while (true) { _incoming.resetWaitHandle(); //std::cout<<"try"<callbackId) { case mtDeadManTimer: { //std::cout<<"mtDeadManTimer"< nextCheckTime) { //std::cout<<"update list"<rowIsValid(); result->nextRow()) { msg->quickstrikeList.insert(result->getStringField(0)); } for (MysqlResultRef result = slaveDatabase.tryQueryUntilSuccess ("SELECT s FROM weston_exclude_bollinger"); result->rowIsValid(); result->nextRow()) { msg->bollingerList.insert(result->getStringField(0)); } msg->send(_dataNodeManager); // ShaneBird DatabaseNotes *notes = new DatabaseNotes(getShaneBirdNotesChannel()); for (MysqlResultRef result = slaveDatabase.tryQueryUntilSuccess("SELECT field_name, value FROM tiq_notes WHERE user='Shane Bird'"); result->rowIsValid(); result->nextRow()) { notes->values[result->getStringField(0)] = result->getDoubleField(1, 0.0); } notes->send(_dataNodeManager); // JoeFavaloro notes = new DatabaseNotes(getJoeFavaloroNotesChannel()); for (MysqlResultRef result = slaveDatabase.tryQueryUntilSuccess("SELECT field_name, value FROM tiq_notes WHERE user='Joe Favaloro'"); result->rowIsValid(); result->nextRow()) { notes->values[result->getStringField(0)] = result->getDoubleField(1, 0.0); } notes->send(_dataNodeManager); // Check on the database at the somewhat arbitrary interval of once // every 10 minutes. This is probably good enough. Brad said // daily might be good enough for Shane Bird. The weston stuff // originally updated every 5 minutes, but that's changed a lot. // More of that stuff is done on the client now. We still look // at that list here, but in practice it's always empty. There's // a second list, but I doubt if it's updated more than once per // month. nextCheckTime = now + 600; } timeval pollInterval; pollInterval.tv_sec = nextCheckTime - now; // Add a little extra time for good measure. I've found in similar // situations that we would wake up a very short time before we needed // to. So we'd busy wait until it was really time to wake up. It's not // a big deal, but I'd rather wake up a little late rather than busy // waiting. pollInterval.tv_usec = 250000; //std::cout<<"wait"<callbackId = mtDeadManTimer; _incoming.newRequest(r); }