job scheduler works with run once and run periodic schedules. Stop logic is not fully thought through.
Dependents: JobSchedulerDemo Borsch
Diff: scheduler.h
- Revision:
- 2:9bf5366ad5a2
- Parent:
- 1:ec6a1d054065
- Child:
- 3:f08f55827736
--- a/scheduler.h Tue Jul 11 18:07:11 2017 +0000 +++ b/scheduler.h Tue Jul 11 20:35:19 2017 +0000 @@ -8,6 +8,7 @@ typedef int JobID; typedef int JobTypeID; + typedef int ActionType; const ActionType JobAddAT(1); @@ -19,7 +20,27 @@ */ class ISchedule { public: - virtual ~ISchedule() = 0; + + virtual ~ISchedule() {}; + + /** + NextRunTime returns next run time or zero if never. + + @param from reflects current time. + Test cases may manipulate the value of from to test algorithms. + + If return time is less than from, then scheduler will run the job + ASAP. + + If return time is more than from, then scheduler will run the job + in the future calculated as (return_value - from). + */ + virtual time_t NextRunTime(time_t from) = 0; + }; + + struct IJobData { + public: + virtual ~IJobData() = 0; }; /** @@ -43,17 +64,22 @@ }; /** - Job describes the job and its parameters, but not the job's schedule. + Job describes the job, its parameters and schedule. */ class Job { public: - Job(JobID id, JobTypeID typeID): _id(id), _typeID(typeID) {}; + Job(JobTypeID typeID, ISchedule *schedule, IJobData *data) + : _id(0), _typeID(typeID), _schedule(schedule), _data(data) {}; JobID GetID() const { return _id; }; + void Init(JobID id) { + _id = id; + } + JobTypeID GetTypeID() const { return _typeID; }; @@ -61,9 +87,31 @@ private: JobID _id; JobTypeID _typeID; + ISchedule *_schedule; + IJobData *_data; }; - + class Appointment { + + public: + + Appointment(JobTypeID typeID, ISchedule *schedule, IJobData *data, time_t time) + : _job(typeID, schedule, data), _time(time) { } + + time_t GetTime() { + return _time; + } + + Job* GetJob() { + return &_job; + } + + private: + + Job _job; + time_t _time; + }; + /** Scheduler is responsible for maintaining job schedules and running jobs in a serial manner. For example, next job appointments can be: @@ -84,8 +132,8 @@ void WaitToStop(); /** JobAdd adds job of typeID and returns ID of added job. */ - Response<JobID> JobAdd(int typeID); - void JobRemove(int jobID); + Response<JobID> JobAdd(JobTypeID typeID, ISchedule *schedule, IJobData *data); + void JobRemove(JobID jobID); private: static void updateAdapter(void *target); void updateHandler(); @@ -98,6 +146,7 @@ JobID _nextJobID; /** _actions contains incoming action queue to handle. */ Queue<Action, 5> _actions; + LinkedList<Appointment> _timeline; }; } \ No newline at end of file