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

Dependencies:   LinkedList

Dependents:   JobSchedulerDemo Borsch

Committer:
sgnezdov
Date:
Tue Jul 11 23:12:54 2017 +0000
Revision:
8:4ead1f4ab741
Parent:
2:9bf5366ad5a2
Child:
16:f61b62b119dd
added periodic job and it works

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sgnezdov 0:806403f3d0d1 1 #pragma once
sgnezdov 0:806403f3d0d1 2
sgnezdov 2:9bf5366ad5a2 3 #include "scheduler.h"
sgnezdov 2:9bf5366ad5a2 4
sgnezdov 0:806403f3d0d1 5 namespace JobScheduler {
sgnezdov 0:806403f3d0d1 6
sgnezdov 2:9bf5366ad5a2 7 class RunOnceSchedule: public ISchedule {
sgnezdov 2:9bf5366ad5a2 8 public:
sgnezdov 2:9bf5366ad5a2 9 RunOnceSchedule(time_t time): _time(time) {};
sgnezdov 2:9bf5366ad5a2 10
sgnezdov 2:9bf5366ad5a2 11 virtual ~RunOnceSchedule() {};
sgnezdov 2:9bf5366ad5a2 12
sgnezdov 2:9bf5366ad5a2 13 virtual time_t NextRunTime(time_t from) {
sgnezdov 2:9bf5366ad5a2 14 time_t current = _time;
sgnezdov 2:9bf5366ad5a2 15 _time = 0;
sgnezdov 2:9bf5366ad5a2 16 return current;
sgnezdov 2:9bf5366ad5a2 17 };
sgnezdov 2:9bf5366ad5a2 18
sgnezdov 2:9bf5366ad5a2 19 private:
sgnezdov 2:9bf5366ad5a2 20 time_t _time;
sgnezdov 2:9bf5366ad5a2 21 };
sgnezdov 2:9bf5366ad5a2 22
sgnezdov 8:4ead1f4ab741 23 class RunPeriodicSchedule: public ISchedule {
sgnezdov 8:4ead1f4ab741 24 public:
sgnezdov 8:4ead1f4ab741 25 RunPeriodicSchedule(time_t period, int limit = 0)
sgnezdov 8:4ead1f4ab741 26 : _period(period), _limit(limit), _counter(0) {};
sgnezdov 8:4ead1f4ab741 27
sgnezdov 8:4ead1f4ab741 28 virtual ~RunPeriodicSchedule() {};
sgnezdov 8:4ead1f4ab741 29
sgnezdov 8:4ead1f4ab741 30 virtual time_t NextRunTime(time_t from) {
sgnezdov 8:4ead1f4ab741 31 if (_limit > 0 && _counter == _limit) {
sgnezdov 8:4ead1f4ab741 32 // no next run
sgnezdov 8:4ead1f4ab741 33 return 0;
sgnezdov 8:4ead1f4ab741 34 }
sgnezdov 8:4ead1f4ab741 35 _counter++;
sgnezdov 8:4ead1f4ab741 36 time_t next = from + _period;
sgnezdov 8:4ead1f4ab741 37 return next;
sgnezdov 8:4ead1f4ab741 38 };
sgnezdov 8:4ead1f4ab741 39
sgnezdov 8:4ead1f4ab741 40 private:
sgnezdov 8:4ead1f4ab741 41 time_t _period;
sgnezdov 8:4ead1f4ab741 42 /** limit limits number of times to execute NextRunTime.
sgnezdov 8:4ead1f4ab741 43 If set to zero, then it is unlimitted.
sgnezdov 8:4ead1f4ab741 44 */
sgnezdov 8:4ead1f4ab741 45 int _limit;
sgnezdov 8:4ead1f4ab741 46 int _counter;
sgnezdov 8:4ead1f4ab741 47 };
sgnezdov 8:4ead1f4ab741 48
sgnezdov 8:4ead1f4ab741 49 } // end of namespace