diff options
-rw-r--r-- | src/minicron.h | 52 | ||||
-rw-r--r-- | src/optparser.cpp | 13 | ||||
-rw-r--r-- | src/optparser.h | 3 | ||||
-rw-r--r-- | src/tests/optparser.cpp | 5 |
4 files changed, 67 insertions, 6 deletions
diff --git a/src/minicron.h b/src/minicron.h index b100ad0..3e6beea 100644 --- a/src/minicron.h +++ b/src/minicron.h | |||
@@ -30,6 +30,12 @@ namespace Bu | |||
30 | * By default a job will continually reschedule itself after being run | 30 | * By default a job will continually reschedule itself after being run |
31 | * unless it calls stop() on it's job object, it is removed using | 31 | * unless it calls stop() on it's job object, it is removed using |
32 | * removeJob() on the cron object, or it is added with addJobOnce. | 32 | * removeJob() on the cron object, or it is added with addJobOnce. |
33 | * | ||
34 | *@todo A minor change to the job execution system could allow a Timer to | ||
35 | * defer or reschedule execution instead of the job executing. This would, | ||
36 | * in effect, allow us to do every type of interesting scheduling that | ||
37 | * systems like fcron offer, including time constrained load-balanced | ||
38 | * execution. | ||
33 | */ | 39 | */ |
34 | class MiniCron | 40 | class MiniCron |
35 | { | 41 | { |
@@ -89,16 +95,35 @@ namespace Bu | |||
89 | */ | 95 | */ |
90 | virtual void removeJob( JobId jid ); | 96 | virtual void removeJob( JobId jid ); |
91 | 97 | ||
98 | /** | ||
99 | * The baseclass for timer/schedulers for MiniCron jobs. Classes that | ||
100 | * inherit from this are used to determine when jobs will run and at | ||
101 | * what interval. | ||
102 | */ | ||
92 | class Timer | 103 | class Timer |
93 | { | 104 | { |
94 | public: | 105 | public: |
95 | Timer(); | 106 | Timer(); |
96 | virtual ~Timer(); | 107 | virtual ~Timer(); |
97 | 108 | ||
109 | /** | ||
110 | * Called by MiniCron when each job is run to determine the next | ||
111 | * time that a job should be run. When a job is run, this function | ||
112 | * is actually called before the job is executed again so that the | ||
113 | * job can tell when the next time it will be run will be. | ||
114 | */ | ||
98 | virtual time_t nextTime()=0; | 115 | virtual time_t nextTime()=0; |
116 | |||
117 | /** | ||
118 | * This function should return a copy of the child class. | ||
119 | */ | ||
99 | virtual Timer *clone() const = 0; | 120 | virtual Timer *clone() const = 0; |
100 | }; | 121 | }; |
101 | 122 | ||
123 | /** | ||
124 | * Execute the job every tInterval seconds, also you can delay the | ||
125 | * first run by a different amount of time from the job's creation. | ||
126 | */ | ||
102 | class TimerInterval : public Timer | 127 | class TimerInterval : public Timer |
103 | { | 128 | { |
104 | public: | 129 | public: |
@@ -113,6 +138,23 @@ namespace Bu | |||
113 | time_t tInterval; | 138 | time_t tInterval; |
114 | }; | 139 | }; |
115 | 140 | ||
141 | /** | ||
142 | * A much more general timer class that can be used for much more | ||
143 | * "cron-like" functionality. The constructor takes a string that | ||
144 | * describes the times that the job should be run. At the moment the | ||
145 | * following schemes are understood: | ||
146 | * | ||
147 | * "daily [hour] [minute]" | ||
148 | * "hourly [minute]" | ||
149 | * "weekly [day] [hour] [minute]" | ||
150 | * | ||
151 | * In these examples each word in [brackets] represents a number that | ||
152 | * matches the data type in the brackets. [day] is the number of days | ||
153 | * since sunday, 0-6. You can also use lowercase three character | ||
154 | * abbreviations for the day names. | ||
155 | * | ||
156 | * Many more forms follow. | ||
157 | */ | ||
116 | class TimerBasic : public Timer | 158 | class TimerBasic : public Timer |
117 | { | 159 | { |
118 | public: | 160 | public: |
@@ -142,13 +184,21 @@ namespace Bu | |||
142 | Bu::FString sSpec; | 184 | Bu::FString sSpec; |
143 | }; | 185 | }; |
144 | 186 | ||
187 | /** | ||
188 | * Represents a MiniCron Job. This class is used for both internal | ||
189 | * job management as well as job slot interaction and control. Objects | ||
190 | * of this class are passed into the slots that are signaled when a job | ||
191 | * is executed. | ||
192 | */ | ||
145 | class Job | 193 | class Job |
146 | { | 194 | { |
147 | friend class Bu::MiniCron; | 195 | friend class Bu::MiniCron; |
148 | public: | 196 | private: |
149 | Job( JobId jid, bool bRepeat=true ); | 197 | Job( JobId jid, bool bRepeat=true ); |
150 | virtual ~Job(); | 198 | virtual ~Job(); |
151 | 199 | ||
200 | public: | ||
201 | |||
152 | /** | 202 | /** |
153 | * Execute this job once, increment the runcount and schedule the | 203 | * Execute this job once, increment the runcount and schedule the |
154 | * next occurance of it. | 204 | * next occurance of it. |
diff --git a/src/optparser.cpp b/src/optparser.cpp index 14e7418..081b26c 100644 --- a/src/optparser.cpp +++ b/src/optparser.cpp | |||
@@ -179,6 +179,11 @@ void Bu::OptParser::setOverride( const Bu::FString &sOpt, const Bu::FString &sOv | |||
179 | hlOption.get( sOpt )->sOverride = sOverride; | 179 | hlOption.get( sOpt )->sOverride = sOverride; |
180 | } | 180 | } |
181 | 181 | ||
182 | void Bu::OptParser::setHelpDefault( const Bu::FString &sOpt, const Bu::FString &sTxt ) | ||
183 | { | ||
184 | hlOption.get( sOpt )->sHelpDefault = sTxt; | ||
185 | } | ||
186 | |||
182 | void Bu::OptParser::addHelpOption( char c, const Bu::FString &s, const Bu::FString &sHelp ) | 187 | void Bu::OptParser::addHelpOption( char c, const Bu::FString &s, const Bu::FString &sHelp ) |
183 | { | 188 | { |
184 | Option o; | 189 | Option o; |
@@ -213,8 +218,9 @@ int Bu::OptParser::optHelp( StrArray /*aParams*/ ) | |||
213 | { | 218 | { |
214 | if( (*i).cOpt != '\0' ) | 219 | if( (*i).cOpt != '\0' ) |
215 | bHasShort = true; | 220 | bHasShort = true; |
216 | if( (*i).sOpt && iMaxWidth < (*i).sOpt.getSize() ) | 221 | int lOptSize = (*i).sOpt.getSize() + (*i).sHelpDefault.getSize(); |
217 | iMaxWidth = (*i).sOpt.getSize(); | 222 | if( (*i).sOpt && iMaxWidth < lOptSize ) |
223 | iMaxWidth = lOptSize; | ||
218 | } | 224 | } |
219 | int iIndent = 4; | 225 | int iIndent = 4; |
220 | if( bHasShort ) | 226 | if( bHasShort ) |
@@ -249,7 +255,8 @@ int Bu::OptParser::optHelp( StrArray /*aParams*/ ) | |||
249 | { | 255 | { |
250 | if( (*i).sOpt ) | 256 | if( (*i).sOpt ) |
251 | { | 257 | { |
252 | sio << "--" << Fmt(iMaxWidth, Fmt::Left) << (*i).sOpt; | 258 | sio << "--" << Fmt(iMaxWidth, Fmt::Left) |
259 | << (*i).sOpt + (*i).sHelpDefault; | ||
253 | } | 260 | } |
254 | else | 261 | else |
255 | { | 262 | { |
diff --git a/src/optparser.h b/src/optparser.h index 89ef14d..6b0ae58 100644 --- a/src/optparser.h +++ b/src/optparser.h | |||
@@ -81,6 +81,7 @@ namespace Bu | |||
81 | OptionSignal sUsed; | 81 | OptionSignal sUsed; |
82 | _ValueProxy *pProxy; | 82 | _ValueProxy *pProxy; |
83 | Bu::FString sOverride; | 83 | Bu::FString sOverride; |
84 | Bu::FString sHelpDefault; | ||
84 | }; | 85 | }; |
85 | 86 | ||
86 | private: | 87 | private: |
@@ -157,6 +158,8 @@ namespace Bu | |||
157 | void setOverride( char cOpt, const Bu::FString &sOverride ); | 158 | void setOverride( char cOpt, const Bu::FString &sOverride ); |
158 | void setOverride( const Bu::FString &sOpt, | 159 | void setOverride( const Bu::FString &sOpt, |
159 | const Bu::FString &sOverride ); | 160 | const Bu::FString &sOverride ); |
161 | |||
162 | void setHelpDefault( const Bu::FString &sOpt, const Bu::FString &sTxt ); | ||
160 | 163 | ||
161 | void addHelpOption( char c='h', const Bu::FString &s="help", | 164 | void addHelpOption( char c='h', const Bu::FString &s="help", |
162 | const Bu::FString &sHelp="This help." ); | 165 | const Bu::FString &sHelp="This help." ); |
diff --git a/src/tests/optparser.cpp b/src/tests/optparser.cpp index 5c91351..449677d 100644 --- a/src/tests/optparser.cpp +++ b/src/tests/optparser.cpp | |||
@@ -34,10 +34,11 @@ public: | |||
34 | true | 34 | true |
35 | ); | 35 | ); |
36 | addOption( sVar, 's', "str", "Set a variable, see what it does."); | 36 | addOption( sVar, 's', "str", "Set a variable, see what it does."); |
37 | addOption( iBob, "bob", "Change iBob to wahtever you want."); | 37 | addOption( iBob, "bob", "Change iBob to whatever you want."); |
38 | addOption( dBob, 'd', "Change dBob to wahtever you want."); | 38 | addOption( dBob, 'd', "Change dBob to whatever you want."); |
39 | 39 | ||
40 | setOverride("str", "Bob!"); | 40 | setOverride("str", "Bob!"); |
41 | setHelpDefault("bob", "=542"); | ||
41 | 42 | ||
42 | addHelpOption(); | 43 | addHelpOption(); |
43 | 44 | ||