PeriodicJobRunner::PeriodicJobRunner(JobManager* mgr, const std::string& owner, JobInfo* info) :
- JobRunner(mgr, owner, info)
+ JobRunner(mgr, owner, info),
+ __lastScheduledTime(0)
{
++__periodicJobRunnerCnt;
}
SchedTimer::reset();
}
+time_t PeriodicJobRunner::getLastTime()
+{
+ return __lastScheduledTime;
+}
+
+void PeriodicJobRunner::setLastTime(time_t t)
+{
+ __lastScheduledTime = t;
+}
+
OnDemandJobRunner::OnDemandJobRunner(JobManager* mgr, const std::string& owner, JobInfo* info) :
JobRunner(mgr, owner, info)
* limitations under the License.
*/
+#include <Conf.h>
#include "JobRunner.h"
#include "JobState.h"
#define ENTER _I("ENTER: Job-%d of '%s'", getJobId(), getOwner().c_str())
#define EXIT _I("EXIT : Job-%d of '%s'", getJobId(), getOwner().c_str())
+#define SEC_PER_MIN 60
+
using namespace ctx;
unsigned int TimerStandbyState::__timerStandbyStateCnt = 0;
bool TimerStandbyState::execute()
{
- //TODO: Set a timer.
- return true;
+ return __setTimer();
}
void TimerStandbyState::onSchedTimerExpired()
{
- //TODO: Transit to ReqVerificationState.
+ transit(new ReqVerificationState(this));
}
unsigned int TimerStandbyState::getCount()
return __timerStandbyStateCnt;
}
+bool TimerStandbyState::__setTimer()
+{
+ PeriodicJobRunner* runner = static_cast<PeriodicJobRunner*>(getJobRunner());
+ time_t interval = static_cast<time_t>(static_cast<PeriodicJobInfo*>(getJobInfo())->getInterval()) * SEC_PER_MIN;
+
+ time_t lastTime = [&]()->time_t {
+ if (runner->getLastTime() != 0)
+ return runner->getLastTime();
+
+ time_t now = time(NULL);
+ time_t anchor = static_cast<PeriodicJobInfo*>(getJobInfo())->getAnchor();
+
+ anchor = anchor % interval;
+ return static_cast<time_t>((now - anchor) / interval) * interval + anchor;
+ }();
+
+ time_t nextTime = lastTime + interval;
+ unsigned int margin = static_cast<double>(interval) * CTX_PERIOD_MARGIN;
+
+ runner->setLastTime(nextTime);
+
+ _I("Set a timer at %ld with %us margin", static_cast<long>(nextTime), margin);
+ return SchedTimer::set(this, nextTime, margin);
+}
+
TriggerStandbyState::TriggerStandbyState(JobRunner* runner) :
JobState(runner)