Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[ARMT] added scheduler and run loop

  • Loading branch information...
commit 7edbc37a3d4ef4482e9e2aeaa2e143dd092f1a42 1 parent 620c734
@gnif authored
View
1  Makefile
@@ -21,6 +21,7 @@ OBJECTS += common/CProcInfo.o
OBJECTS += common/CPCIInfo.o
OBJECTS += common/CHTTP.o
OBJECTS += common/CMessageBuilder.o
+OBJECTS += common/CScheduler.o
OBJECTS += block/CBlockEnumerator.o
OBJECTS += block/CSMARTBlockDevice.o
View
59 armt.cc
@@ -22,6 +22,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <sys/utsname.h>
+#include <time.h>
#include <iostream>
#include <fstream>
@@ -33,6 +34,7 @@
#include "common/CHTTP.h"
#include "common/CCompress.h"
#include "common/CMessageBuilder.h"
+#include "common/CScheduler.h"
#include "fs/CFSVerifier.h"
#include "block/CBlockEnumerator.h"
@@ -104,6 +106,39 @@ bool FSCHECK(std::iostream &ss)
return fs.Save(ss);
}
+class CMSGJob: public ISchedulerJob
+{
+ public:
+ CMSGJob(
+ const std::time_t next ,
+ const unsigned int interval,
+ CMessageBuilder *msg ,
+ const std::string &name ,
+ CMessageBuilder::SegmentFn fn
+ ) :
+ m_next (next ),
+ m_interval(interval),
+ m_msg (msg ),
+ m_name (name ),
+ m_fn (fn )
+ {}
+
+ virtual std::time_t GetRunTime( ) { return m_next; }
+ virtual void SetRunTime(std::time_t time) { m_next = time; }
+ virtual unsigned int GetDelayInterval( ) { return m_interval; }
+ virtual void Execute()
+ {
+ m_msg->AppendSegment(m_name, m_fn);
+ }
+
+ private:
+ std::time_t m_next;
+ unsigned int m_interval;
+ CMessageBuilder *m_msg;
+ std::string m_name;
+ CMessageBuilder::SegmentFn m_fn;
+};
+
int main(int argc, char *argv[])
{
/* must be called first */
@@ -134,10 +169,28 @@ int main(int argc, char *argv[])
return -1;
}
+
CMessageBuilder msg(armthost, armtport);
- msg.AppendSegment("DISKCHECK", &DISKCHECK);
- msg.AppendSegment("FSCHECK" , &FSCHECK );
- msg.Send();
+ CScheduler s;
+
+ /* calculate the GMT time for midnight tonight in the server's timezone */
+ tzset();
+ std::time_t midnight = time(NULL);
+ midnight -= midnight % 86400;
+ midnight += 86400;
+ midnight += timezone - (daylight * 3600);
+
+ /* add the jobs to the scheduler */
+ s.AddJob(new CMSGJob(midnight , 86400, &msg, "FSCHECK" , &FSCHECK ));
+ s.AddJob(new CMSGJob(time(NULL), 60 , &msg, "DISKCHECK", &DISKCHECK));
+
+ while(true)
+ {
+ msg.Reset();
+ if (s.Run())
+ msg.Send();
+ sleep(1);
+ }
if (_hostent)
DNS.Free(_hostent);
View
10 common/CMessageBuilder.cc
@@ -174,6 +174,11 @@ void CMessageBuilder::AppendSegment(const std::string &name, SegmentFn fn)
m_segments[name] = fn;
}
+void CMessageBuilder::Reset()
+{
+ m_segments.clear();
+}
+
void CMessageBuilder::PackString(std::ostream &ss, const std::string &value)
{
uint16_t len = value.length();
@@ -190,6 +195,7 @@ bool CMessageBuilder::Send()
{
std::string body;
{
+ bool send = false;
std::stringstream total;
for(SegmentList::iterator segment = m_segments.begin(); segment != m_segments.end(); ++segment)
{
@@ -207,8 +213,12 @@ bool CMessageBuilder::Send()
total.write((const char *)&namelen, sizeof(namelen));
total.write((const char *)&datalen, sizeof(datalen));
total << segment->first << ss.str();
+ send = true;
}
+ /* if there is nothing to send, do not do anything */
+ if (!send)
+ return true;
std::stringstream compressed;
CCompress::Deflate(total, compressed);
View
1  common/CMessageBuilder.h
@@ -44,6 +44,7 @@ class CMessageBuilder
void InitAuth();
void AppendSegment(const std::string &name, SegmentFn fn);
+ void Reset();
bool Send();
static void PackString(std::ostream &ss, const std::string &value);
View
58 common/CScheduler.cc
@@ -0,0 +1,58 @@
+/*
+ * ARMT (Another Remote Monitoring Tool)
+ * Copyright (C) Geoffrey McRae 2012 <geoff@spacevs.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ */
+
+#include "CScheduler.h"
+
+CScheduler::CScheduler()
+{
+}
+
+CScheduler::~CScheduler()
+{
+ for(JobList::iterator it = m_jobs.begin(); it != m_jobs.end(); ++it)
+ {
+ ISchedulerJob *job = *it;
+ delete job;
+ }
+}
+
+bool CScheduler::Run()
+{
+ std::time_t currentTime = std::time(NULL);
+ bool ran = false;
+
+ for(JobList::iterator it = m_jobs.begin(); it != m_jobs.end(); ++it)
+ {
+ ISchedulerJob *job = *it;
+ if (job->GetRunTime() > currentTime)
+ continue;
+
+ ran = true;
+ job->Execute();
+ job->SetRunTime(job->GetRunTime() + job->GetDelayInterval());
+ }
+
+ return ran;
+}
+
+void CScheduler::AddJob(ISchedulerJob *job)
+{
+ m_jobs.push_back(job);
+}
View
63 common/CScheduler.h
@@ -0,0 +1,63 @@
+/*
+ * ARMT (Another Remote Monitoring Tool)
+ * Copyright (C) Geoffrey McRae 2012 <geoff@spacevs.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ */
+
+#ifndef _CSCHEDULER_H_
+#define _CSCHEDULER_H_
+
+#include <ctime>
+#include <vector>
+
+class ISchedulerJob
+{
+ public:
+ ISchedulerJob() {}
+ virtual ~ISchedulerJob() {}
+
+ virtual std::time_t GetRunTime() = 0;
+ virtual void SetRunTime(std::time_t time) = 0;
+ virtual unsigned int GetDelayInterval() = 0;
+ virtual void Execute() = 0;
+};
+
+class CScheduler
+{
+ public:
+ CScheduler();
+ ~CScheduler();
+
+ /**
+ * Run's any jobs that need running
+ * @return True if any jobs ran
+ */
+ bool Run();
+
+ /**
+ * Add a recurring job to execute at the specified time
+ * @param job An instance of ISchedulerJob
+ */
+ void AddJob(ISchedulerJob *job);
+
+ private:
+ typedef std::vector<ISchedulerJob *> JobList;
+
+ JobList m_jobs;
+};
+
+#endif // _CSCHEDULER_H_

0 comments on commit 7edbc37

Please sign in to comment.
Something went wrong with that request. Please try again.