job scheduler works with run once and run periodic schedules. Stop logic is not fully thought through.
Dependents: JobSchedulerDemo Borsch
Diff: scheduler.cpp
- Revision:
- 2:9bf5366ad5a2
- Parent:
- 1:ec6a1d054065
- Child:
- 3:f08f55827736
--- a/scheduler.cpp Tue Jul 11 18:07:11 2017 +0000 +++ b/scheduler.cpp Tue Jul 11 20:35:19 2017 +0000 @@ -5,14 +5,24 @@ namespace JobScheduler { + bool descendingTimeline(Appointment *a1, Appointment *a2) + { + bool rv = a1->GetTime() <= a2->GetTime(); + //printf("(%d %d:%d)", *d1, *d2, rv); + return rv; + }; + + /** + JobAddReq adds new job to the scheduler. + */ struct JobAddReq: Action { - JobTypeID jobTID; + Appointment *apt; Response<JobID> response; - JobAddReq(JobTypeID typeID) : Action(JobAddAT), jobTID(typeID), response(NoError, 0) {} + JobAddReq(Appointment *a) : Action(JobAddAT), apt(a), response(NoError, 0) {} }; Scheduler::Scheduler(JobService *jobService) - : _jobService(jobService) { } + : _jobService(jobService), _nextJobID(1) { } void Scheduler::updateAdapter(void *thisPointer) { Scheduler *self = static_cast<Scheduler*>(thisPointer); @@ -32,19 +42,33 @@ _updater.join(); } - Response<JobID> Scheduler::JobAdd(JobTypeID jobTID) { - JobAddReq req(jobTID); + Response<JobID> Scheduler::JobAdd(JobTypeID jobTID, ISchedule *schedule, IJobData *data) { + Appointment *apt = new Appointment(jobTID, schedule, data, time_t(0)); + if (NULL == apt) { + printf("[Scheduler::JobAdd] failed to allocate appointment\n"); + return Response<JobID>(1, 0); + } + JobAddReq req(apt); _actions.put(&req); // default is wait forever osEvent evt = req.resQueue.get(); if (evt.status == osEventMessage) { - printf("[Scheduler::JobAdd] completed ok\n"); - //return (JobRes*)evt.value.p; + if (evt.value.p != NULL) { + printf("[Scheduler::JobAdd] completed ok\n"); + } else { + printf("[Scheduler::JobAdd] NOT added (C1)\n"); + } + } else { + // not sure what condition is + printf("[Scheduler::JobAdd] NOT added (C2)\n"); + delete apt; + apt = NULL; } + // yes, return a copy of the structure return req.response; } - void Scheduler::JobRemove(int jobID) { + void Scheduler::JobRemove(JobID jobID) { } void Scheduler::updateHandler() { @@ -67,6 +91,19 @@ switch(action->type) { case JobAddAT: { JobAddReq *req = static_cast<JobAddReq*>(action); + Job *job = req->apt->GetJob(); + if (job->GetID() == 0) { + // assign job its ID + job->Init(_nextJobID++); + } + node<Appointment> *tmp = _timeline.insertOrdered(req->apt, descendingTimeline); + if (NULL == tmp) { + printf("[Scheduler::process] timeline insert failed\n"); + action->resQueue.put(NULL); + return; + } + //printf("[Scheduler::process] simulate error\n"); + //action->resQueue.put(NULL); action->resQueue.put(&req->response); break; }