job scheduler works with run once and run periodic schedules. Stop logic is not fully thought through.
Dependents: JobSchedulerDemo Borsch
scheduler.cpp
- Committer:
- sgnezdov
- Date:
- 2017-07-13
- Revision:
- 10:8cff30b5b90d
- Parent:
- 9:ee21cd055a97
- Child:
- 12:684ddfc57199
File content as of revision 10:8cff30b5b90d:
#include "scheduler.h" #include "mbed-trace/mbed_trace.h" #define TRACE_GROUP "schd" Timeout WakeOnce; void update(void *target) { }; namespace JobScheduler { const ActionType JobAddAT(1); const ActionType JobRunAT(3); const ActionType JobQuitAT(4); bool descendingTimeline(Appointment *a1, Appointment *a2) { bool rv = a1->GetTime() <= a2->GetTime(); //tr_debug("(%d %d:%d)", *d1, *d2, rv); return rv; }; /** JobAddReq adds new job to the scheduler. */ struct JobAddReq: Action { Appointment *apt; Response<JobID> response; JobAddReq(Appointment *a) : Action(JobAddAT), apt(a), response(NoError, 0) {} }; Scheduler::Scheduler(JobService *jobService) : _quitUpdater(false), _quitRunner(false), _jobService(jobService), _nextJobID(1) { } void Scheduler::updateAdapter(void *thisPointer) { Scheduler *self = static_cast<Scheduler*>(thisPointer); self->updateHandler(); } void Scheduler::runAdapter(void *thisPointer) { Scheduler *self = static_cast<Scheduler*>(thisPointer); self->runHandler(); } void Scheduler::Start() { _updater.start(callback(Scheduler::updateAdapter, this)); _runner.start(callback(Scheduler::runAdapter, this)); } void Scheduler::Stop() { _runs.put(NULL); Action uReq = Action(JobQuitAT); _updates.put(&uReq); uReq.resQueue.get(); } void Scheduler::WaitToStop() { _updater.join(); _runner.join(); } Response<JobID> Scheduler::JobAdd(JobTypeID jobTID, ISchedule *schedule, IJobData *data) { Appointment *apt = new Appointment(jobTID, schedule, data, time_t(0)); if (NULL == apt) { tr_error("[JobAdd] failed to allocate appointment"); return Response<JobID>(1, 0); } return this->reschedule(apt); } Response<JobID> Scheduler::reschedule(Appointment *apt) { JobAddReq req(apt); // set next appointment time time_t now = time(NULL); // now in seconds apt->SetTime(apt->GetJob()->GetSchedule()->NextRunTime(now)); if (apt->GetTime() == 0) { // there is no next run time; delete appointment tr_debug("[reschedule] NO next appointment"); delete apt; req.response.error = 2; return req.response; } tr_debug("[reschedule] put"); _updates.put(&req); tr_debug("[reschedule] get"); // default is wait forever osEvent evt = req.resQueue.get(); if (evt.status == osEventMessage) { if (evt.value.p != NULL) { tr_debug("[reschedule] completed ok"); } else { tr_error("[reschedule] NOT added (C1)"); } } else { // not sure what condition is tr_error("[reschedule] NOT added (C2)"); delete apt; apt = NULL; } // yes, return a copy of the structure return req.response; } void Scheduler::JobRemove(JobID jobID) { } static Action jobRunReq(JobRunAT); void Scheduler::onWakeOnce() { _updates.put(&jobRunReq); } void Scheduler::updateHandler() { while (!_quitUpdater) { tr_debug("[updateHandler] waiting for action"); // wait forever ... osEvent evt = _updates.get(); if (evt.status == osEventMessage) { tr_debug("[updateHandler] process action"); this->process((Action*)evt.value.p); } else { tr_error("[updateHandler] NOT osEventMessage"); } } } void Scheduler::process(Action *action) { time_t now = time(NULL); // now in seconds switch(action->type) { case JobAddAT: { tr_debug("[process] 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) { tr_error("[process] timeline insert failed"); action->resQueue.put(NULL); // internal state has not changed return; } req->response.data = job->GetID(); //tr_debug("[process] simulate error"); //action->resQueue.put(NULL); action->resQueue.put(&req->response); break; } case JobRunAT: { tr_debug("[process] JobRunAT"); // execute job run logic after switch break; } case JobQuitAT: { tr_debug("[process] JobQuitAT"); action->resQueue.put(NULL); _quitUpdater = true; return; } default: tr_warn("[process] unknown action type"); action->resQueue.put(NULL); } node<Appointment> *wakeNode = _timeline.pop(1); Appointment *wakeApt = wakeNode->data; Job* wakeJob = wakeApt->GetJob(); if (now < wakeApt->GetTime()) { // request wake up time_t sleepTime = wakeApt->GetTime() - now; tr_debug("[process] job %d wake up in %d seconds", wakeJob->GetID(), sleepTime); WakeOnce.attach(callback(this, &Scheduler::onWakeOnce), sleepTime); } else { // process job tr_debug("[process] job ID %d ready to run", wakeJob->GetID()); _timeline.remove(1); _runs.put(wakeApt); } } void Scheduler::runHandler() { while (!_quitRunner) { tr_debug("[runHandler] waiting for action"); // wait forever ... osEvent evt = _runs.get(); if (evt.status == osEventMessage) { tr_debug("[runHandler] run action"); Appointment *apt = (Appointment*)evt.value.p; if (NULL == apt) { tr_debug("[runHandler] quit requested"); _quitRunner = true; break; } Job *job = apt->GetJob(); JobType *jt = _jobService->GetJob(job->GetTypeID()); if (jt == NULL) { tr_error("[runHandler] NO FUNC for job type id %d", job->GetTypeID()); // NO reschedule delete apt; continue; } tr_debug("Job Started"); jt->RunJob(); tr_debug("Job Finished"); this->reschedule(apt); } else { tr_error("[runHandler] NOT osEventMessage"); } } } }