job scheduler works with run once and run periodic schedules. Stop logic is not fully thought through.

Dependencies:   LinkedList

Dependents:   JobSchedulerDemo Borsch

Revision:
12:684ddfc57199
Parent:
10:8cff30b5b90d
Child:
13:6be67ee77861
--- a/scheduler.cpp	Fri Jul 14 00:00:20 2017 +0000
+++ b/scheduler.cpp	Fri Jul 14 17:02:29 2017 +0000
@@ -5,9 +5,6 @@
 
 Timeout WakeOnce;
 
-void update(void *target) {
-};
-
 namespace JobScheduler {
 
     const ActionType JobAddAT(1);
@@ -16,8 +13,10 @@
 
     bool descendingTimeline(Appointment *a1, Appointment *a2)
     {
-        bool rv = a1->GetTime() <= a2->GetTime();
-        //tr_debug("(%d %d:%d)", *d1, *d2, rv);
+        time_t t1 = a1->GetTime();
+        time_t t2 = a2->GetTime();
+        bool rv = t1 <= t2;
+        tr_debug("apt %d:%d <= %d:%d is %d)", a1->GetJob()->GetID(), t1, a2->GetJob()->GetID(), t2, rv);
         return rv;
     };
     
@@ -139,10 +138,13 @@
                 if (job->GetID() == 0) {
                     // assign job its ID
                     job->Init(_nextJobID++);
+                    tr_debug("assigned new job its id %d", job->GetID());
+                } else {
+                    tr_debug("job already has id %d", job->GetID());
                 }
                 node<Appointment> *tmp = _timeline.insertOrdered(req->apt, descendingTimeline);
                 if (NULL == tmp) {
-                    tr_error("[process] timeline insert failed");
+                    tr_error("[process] timeline insert failed for job ID %d", job->GetID());
                     action->resQueue.put(NULL);
                     // internal state has not changed
                     return;
@@ -169,6 +171,10 @@
                 action->resQueue.put(NULL);
         }
         node<Appointment> *wakeNode = _timeline.pop(1);
+        if (wakeNode == NULL) {
+            tr_debug("[process] found no nodes to run");
+            return;
+        }
         Appointment *wakeApt = wakeNode->data;
         Job* wakeJob = wakeApt->GetJob();
         if (now < wakeApt->GetTime()) {
@@ -181,6 +187,12 @@
             tr_debug("[process] job ID %d ready to run", wakeJob->GetID());
             _timeline.remove(1);
             _runs.put(wakeApt);
+            // make sure we run this function one more time
+            // in case there are jobs left to run
+            //
+            // don't use WakeOnce, because it appears to work only for 
+            // one wake up request at a time.
+            _updates.put(&jobRunReq);
         }
     }