Sergei G / JobScheduler

Dependencies:   LinkedList

Dependents:   JobSchedulerDemo Borsch

Revision:
5:d8f69ac330f2
Parent:
4:78bcd5a675e1
Child:
6:5baa0e4ec500
--- a/scheduler.cpp	Tue Jul 11 22:05:12 2017 +0000
+++ b/scheduler.cpp	Tue Jul 11 22:35:33 2017 +0000
@@ -29,7 +29,7 @@
     struct JobRunReq: Action {
         JobRunReq() : Action(JobRunAT) {}
     };
-    
+       
     Scheduler::Scheduler(JobService *jobService) 
     : _jobService(jobService), _nextJobID(1) {   }
 
@@ -64,24 +64,30 @@
             printf("[Scheduler::JobAdd] failed to allocate appointment\n");
             return Response<JobID>(1, 0);
         }
+        return this->reschedule(apt);
+    }
+    
+    Response<JobID> Scheduler::reschedule(Appointment *apt) {
         JobAddReq req(apt);
+        printf("[Scheduler::reschedule] put\n");
         _updates.put(&req);
+        printf("[Scheduler::reschedule] get\n");
         // default is wait forever
         osEvent evt = req.resQueue.get();
         if (evt.status == osEventMessage) {
             if (evt.value.p != NULL) {
-                printf("[Scheduler::JobAdd] completed ok\n");
+                printf("[Scheduler::reschedule] completed ok\n");
             } else {
-                printf("[Scheduler::JobAdd] NOT added (C1)\n");
+                printf("[Scheduler::reschedule] NOT added (C1)\n");
             }
         } else {
             // not sure what condition is
-            printf("[Scheduler::JobAdd] NOT added (C2)\n");
+            printf("[Scheduler::reschedule] NOT added (C2)\n");
             delete apt;
             apt = NULL;
         }
         // yes, return a copy of the structure
-        return req.response;
+        return req.response;        
     }
     
     void Scheduler::JobRemove(JobID jobID) {
@@ -104,7 +110,6 @@
             } else {
                 printf("[Scheduler::updateHandler] NOT osEventMessage\n");
             }
-            wait(2);
         }
     }
    
@@ -113,6 +118,7 @@
         time_t now = time(NULL); // now in seconds
         switch(action->type) {
             case JobAddAT: {
+                printf("[Scheduler::process] JobAddAT\n");
                 JobAddReq *req = static_cast<JobAddReq*>(action);
                 Job *job = req->apt->GetJob();
                 if (job->GetID() == 0) {
@@ -135,6 +141,7 @@
                 break;
             }
             case JobRunAT: {
+                printf("[Scheduler::process] JobRunAT\n");
                 // execute job run logic after switch
                 break;
             }
@@ -145,14 +152,14 @@
         node<Appointment> *wakeNode = _timeline.pop(1);
         Appointment *wakeApt = wakeNode->data;
         Job* wakeJob = wakeApt->GetJob();
-        time_t sleepTime = wakeApt->GetTime() - now;
-        if (sleepTime > 0) {
+        if (now < wakeApt->GetTime()) {
             // request wake up
+            time_t sleepTime = wakeApt->GetTime() - now;
             printf("[Scheduler::process] job %d wake up in %d seconds\n", wakeJob->GetID(), sleepTime);
             WakeOnce.attach(callback(this, &Scheduler::onWakeOnce), sleepTime);
         } else {
             // process job
-            printf("[Scheduler::process] running job ID %d\n", wakeJob->GetID());
+            printf("[Scheduler::process] job ID %d ready to run\n", wakeJob->GetID());
             _timeline.remove(1);
             _runs.put(wakeApt);
         }
@@ -166,7 +173,18 @@
             if (evt.status == osEventMessage) {
                 printf("[Scheduler::runHandler] run action\n");
                 Appointment *apt = (Appointment*)evt.value.p;
-                printf("[Scheduler::runHandler] TBD: RUN, RESCHEDULE\n");
+                Job *job = apt->GetJob();
+                jobFunc *f = _jobService->GetJob(job->GetTypeID());
+                if (f == NULL) {
+                    printf("[Scheduler::runHandler] NO FUNC for job type id %d\n", job->GetTypeID());
+                    // NO reschedule
+                    delete apt;
+                    continue;
+                }
+                printf("Job Started\n");
+                f();
+                printf("Job Finished\n");
+                this->reschedule(apt);
             } else {
                 printf("[Scheduler::runHandler] NOT osEventMessage\n");
             }