Skip to content

Commit 0c8926e

Browse files
authored
Added "before" method (#34)
1 parent 3fdef40 commit 0c8926e

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

README.md

+28
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,19 @@ $scheduler->php('script.php')->onlyOne(null, function ($lastExecutionTime) {
278278
});
279279
```
280280

281+
### Before job execution
282+
283+
In some cases you might want to run some code, if the job is due to run, before it's being executed.
284+
For example you might want to add a log entry, ping a url or anything else.
285+
To do so, you can call the `before` like the example below.
286+
287+
```php
288+
// $logger here is your own implementation
289+
$scheduler->php('script.php')->before(function () use ($logger) {
290+
$logger->info("script.php started at " . time());
291+
});
292+
```
293+
281294
### After job execution
282295

283296
Sometime you might wish to do something after a job runs. The `then` methods provides you the flexibility to do anything you want after the job execution. The output of the job will be injected to this function.
@@ -297,6 +310,21 @@ $scheduler->php('script.php')->then(function ($output) use ($logger) {
297310
}, true);
298311
```
299312

313+
#### Using "before" and "then" together
314+
315+
```php
316+
// $logger here is your own implementation
317+
$scheduler->php('script.php')
318+
->before(function () use ($logger) {
319+
$logger->info("script.php started at " . time());
320+
})
321+
->then(function ($output) use ($logger) {
322+
$logger->info("script.php completed at " . time(), [
323+
'output' => $output,
324+
]);
325+
});
326+
```
327+
300328
### Multiple scheduler runs
301329
In some cases you might need to run the scheduler multiple times in the same script.
302330
Although this is not a common case, the following methods will allow you to re-use the same instance of the scheduler.

src/GO/Job.php

+25
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,13 @@ class Job
109109
*/
110110
private $emailConfig = [];
111111

112+
/**
113+
* A function to execute before the job is executed.
114+
*
115+
* @var callable
116+
*/
117+
private $before;
118+
112119
/**
113120
* A function to execute after the job is executed.
114121
*
@@ -365,6 +372,10 @@ public function run()
365372
// Write lock file if necessary
366373
$this->createLockFile();
367374

375+
if (is_callable($this->before)) {
376+
call_user_func($this->before);
377+
}
378+
368379
if (is_callable($compiled)) {
369380
$this->output = $this->exec($compiled);
370381
} else {
@@ -519,6 +530,20 @@ private function emailOutput()
519530
return true;
520531
}
521532

533+
/**
534+
* Set function to be called before job execution
535+
* Job object is injected as a parameter to callable function.
536+
*
537+
* @param callable $fn
538+
* @return self
539+
*/
540+
public function before(callable $fn)
541+
{
542+
$this->before = $fn;
543+
544+
return $this;
545+
}
546+
522547
/**
523548
* Set a function to be called after job execution.
524549
* By default this will force the job to run in foreground

tests/GO/JobTest.php

+18
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,24 @@ public function testShouldReturnOutputOfJobExecution()
353353
$this->assertEquals(['hi'], $job3->getOutput());
354354
}
355355

356+
public function testShouldRunCallbackBeforeJobExecution()
357+
{
358+
$job = new Job(function () {
359+
return 'Job for testing before function';
360+
});
361+
362+
$callbackWasExecuted = false;
363+
$outputWasSet = false;
364+
365+
$job->before(function () use ($job, &$callbackWasExecuted, &$outputWasSet) {
366+
$callbackWasExecuted = true;
367+
$outputWasSet = ! is_null($job->getOutput());
368+
})->run();
369+
370+
$this->assertTrue($callbackWasExecuted);
371+
$this->assertFalse($outputWasSet);
372+
}
373+
356374
public function testShouldRunCallbackAfterJobExecution()
357375
{
358376
$job = new Job(function () {

0 commit comments

Comments
 (0)