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:
- 9:ee21cd055a97
- Parent:
- 7:98c8b2eabea3
- Child:
- 10:8cff30b5b90d
File content as of revision 9:ee21cd055a97:
#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_debug("[JobAdd] failed to allocate appointment\n"); 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\n"); delete apt; req.response.error = 2; return req.response; } tr_debug("[reschedule] put\n"); _updates.put(&req); tr_debug("[reschedule] get\n"); // default is wait forever osEvent evt = req.resQueue.get(); if (evt.status == osEventMessage) { if (evt.value.p != NULL) { tr_debug("[reschedule] completed ok\n"); } else { tr_debug("[reschedule] NOT added (C1)\n"); } } else { // not sure what condition is tr_debug("[reschedule] NOT added (C2)\n"); 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\n"); // wait forever ... osEvent evt = _updates.get(); if (evt.status == osEventMessage) { tr_debug("[updateHandler] process action\n"); this->process((Action*)evt.value.p); } else { tr_debug("[updateHandler] NOT osEventMessage\n"); } } } void Scheduler::process(Action *action) { time_t now = time(NULL); // now in seconds switch(action->type) { case JobAddAT: { tr_debug("[process] JobAddAT\n"); 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_debug("[process] timeline insert failed\n"); action->resQueue.put(NULL); // internal state has not changed return; } req->response.data = job->GetID(); //tr_debug("[process] simulate error\n"); //action->resQueue.put(NULL); action->resQueue.put(&req->response); break; } case JobRunAT: { tr_debug("[process] JobRunAT\n"); // execute job run logic after switch break; } case JobQuitAT: { tr_debug("[process] JobQuitAT\n"); action->resQueue.put(NULL); _quitUpdater = true; return; } default: tr_debug("[process] unknown action type\n"); 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\n", wakeJob->GetID(), sleepTime); WakeOnce.attach(callback(this, &Scheduler::onWakeOnce), sleepTime); } else { // process job tr_debug("[process] job ID %d ready to run\n", wakeJob->GetID()); _timeline.remove(1); _runs.put(wakeApt); } } void Scheduler::runHandler() { while (!_quitRunner) { tr_debug("[runHandler] waiting for action\n"); // wait forever ... osEvent evt = _runs.get(); if (evt.status == osEventMessage) { tr_debug("[runHandler] run action\n"); Appointment *apt = (Appointment*)evt.value.p; if (NULL == apt) { tr_debug("[runHandler] quit requested\n"); _quitRunner = true; break; } Job *job = apt->GetJob(); JobType *f = _jobService->GetJob(job->GetTypeID()); if (f == NULL) { tr_debug("[runHandler] NO FUNC for job type id %d\n", job->GetTypeID()); // NO reschedule delete apt; continue; } tr_debug("Job Started\n"); f->RunJob(); tr_debug("Job Finished\n"); this->reschedule(apt); } else { tr_debug("[runHandler] NOT osEventMessage\n"); } } } }