@@ -52,6 +52,15 @@ class Task {
52
52
*/
53
53
protected $ work = null ;
54
54
55
+ /**
56
+ * task run time
57
+ * @var array
58
+ */
59
+ protected $ time = [
60
+ 'started_at ' => 0 ,
61
+ 'finished_at ' => 0
62
+ ];
63
+
55
64
/**
56
65
* drivers` results
57
66
* @var array
@@ -92,22 +101,61 @@ public function runWork()
92
101
}
93
102
}
94
103
104
+ /**
105
+ * run task
106
+ * @param string $driverName
107
+ *
108
+ * @return bool
109
+ * @throws \Exception
110
+ */
95
111
public function run ($ driverName = '' )
96
112
{
97
113
if ($ this ->isRunning ()) {
98
114
//stop run because current task is running
99
115
return false ;
100
116
}
101
- $ this ->status = static ::RUNNING ;
117
+ if (!$ this ->beforeRun ()) {
118
+ return false ;
119
+ }
102
120
if (!$ driverName ) {
103
121
$ driverName = $ this ->getDriverNameByWeight ();
104
122
}
105
123
$ this ->resortBackupDrivers ($ driverName );
106
124
$ success = $ this ->runDriver ($ driverName );
125
+ return $ this ->afterRun ($ success );
126
+ }
127
+
128
+ /**
129
+ * before run task
130
+ * @return bool
131
+ */
132
+ private function beforeRun ()
133
+ {
134
+ $ this ->time ['started_at ' ] = microtime ();
135
+ $ this ->status = static ::RUNNING ;
136
+ return true ;
137
+ }
138
+
139
+ /**
140
+ * after run task
141
+ * @param $result
142
+ *
143
+ * @return mixed
144
+ */
145
+ private function afterRun ($ result )
146
+ {
107
147
$ this ->status = static ::FINISHED ;
108
- return $ success ;
148
+ $ this ->time ['finished_at ' ] = microtime ();
149
+ return $ result ;
109
150
}
110
151
152
+ /**
153
+ * run driver by name
154
+ * @param $name
155
+ *
156
+ * @return bool
157
+ * @throws \Exception
158
+ */
111
159
public function runDriver ($ name )
112
160
{
113
161
$ driver = $ this ->getDriver ($ name );
@@ -133,6 +181,11 @@ public function runDriver($name)
133
181
return $ success ;
134
182
}
135
183
184
+ /**
185
+ * get a backup driver and run it
186
+ * @return bool
187
+ * @throws \Exception
188
+ */
136
189
public function runBackupDriver ()
137
190
{
138
191
$ name = $ this ->getNextBackupDriverName ();
@@ -142,6 +195,10 @@ public function runBackupDriver()
142
195
return true ;
143
196
}
144
197
198
+ /**
199
+ * generator a back up driver`s name
200
+ * @return null
201
+ */
145
202
public function getNextBackupDriverName ()
146
203
{
147
204
$ drivers = $ this ->backupDrivers ;
@@ -162,6 +219,11 @@ public function getNextBackupDriverName()
162
219
return null ;
163
220
}
164
221
222
+ /**
223
+ * get a driver`s name by drivers` weight
224
+ * @return mixed
225
+ * @throws \Exception
226
+ */
165
227
public function getDriverNameByWeight ()
166
228
{
167
229
$ count = $ base = 0 ;
@@ -190,11 +252,24 @@ public function getDriverNameByWeight()
190
252
throw new \Exception ('get driver name by weight failed, something wrong ' );
191
253
}
192
254
255
+ /**
256
+ * get a driver name
257
+ * @return mixed
258
+ */
193
259
public function driverNameRand ()
194
260
{
195
261
return array_rand (array_keys ($ this ->drivers ));
196
262
}
197
263
264
+ /**
265
+ * create a new driver instance for current task
266
+ * @param $name
267
+ * @param int $weight
268
+ * @param bool|false $isBackup
269
+ * @param \Closure|null $work
270
+ *
271
+ * @return mixed
272
+ */
198
273
public function driver ($ name , $ weight = 1 , $ isBackup = false , \Closure $ work = null )
199
274
{
200
275
$ driver = $ this ->getDriver ($ name );
@@ -205,9 +280,15 @@ public function driver($name, $weight = 1, $isBackup = false, \Closure $work = n
205
280
$ this ->backupDrivers [] = $ name ;
206
281
}
207
282
}
208
- return $ this -> drivers [ $ name ] ;
283
+ return $ driver ;
209
284
}
210
285
286
+ /**
287
+ * current task has driver?
288
+ * @param $name
289
+ *
290
+ * @return bool
291
+ */
211
292
public function hasDriver ($ name )
212
293
{
213
294
if (!$ this ->drivers ) {
@@ -216,6 +297,12 @@ public function hasDriver($name)
216
297
return isset ($ this ->drivers [$ name ]);
217
298
}
218
299
300
+ /**
301
+ * get a driver from current task drives pool
302
+ * @param $name
303
+ *
304
+ * @return null
305
+ */
219
306
public function getDriver ($ name )
220
307
{
221
308
if ($ this ->hasDriver ($ name )) {
@@ -224,6 +311,10 @@ public function getDriver($name)
224
311
return null ;
225
312
}
226
313
314
+ /**
315
+ * init back up drivers
316
+ * @param $name
317
+ */
227
318
public function resortBackupDrivers ($ name )
228
319
{
229
320
if (count ($ this ->backupDrivers ) < 2 ) {
@@ -237,17 +328,29 @@ public function resortBackupDrivers($name)
237
328
}
238
329
}
239
330
331
+ /**
332
+ * task is running ?
333
+ * @return bool
334
+ */
240
335
public function isRunning ()
241
336
{
242
337
return $ this ->status == static ::RUNNING ;
243
338
}
244
339
340
+ /**
341
+ * reset status
342
+ * @return $this
343
+ */
245
344
public function reset ()
246
345
{
247
346
$ this ->status = '' ;
248
347
return $ this ;
249
348
}
250
349
350
+ /**
351
+ * add a driver to backup drivers
352
+ * @param $driverName
353
+ */
251
354
public function addToBackupDrivers ($ driverName )
252
355
{
253
356
if ($ driverName instanceof Driver) {
@@ -258,6 +361,10 @@ public function addToBackupDrivers($driverName)
258
361
}
259
362
}
260
363
364
+ /**
365
+ * remove character driver from backup drivers
366
+ * @param $driverName
367
+ */
261
368
public function removeFromBackupDrivers ($ driverName )
262
369
{
263
370
if ($ driverName instanceof Driver) {
0 commit comments