Skip to content

Commit

Permalink
Added "before" method (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
peppeocchi authored Oct 8, 2017
1 parent 3fdef40 commit 0c8926e
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,19 @@ $scheduler->php('script.php')->onlyOne(null, function ($lastExecutionTime) {
});
```

### Before job execution

In some cases you might want to run some code, if the job is due to run, before it's being executed.
For example you might want to add a log entry, ping a url or anything else.
To do so, you can call the `before` like the example below.

```php
// $logger here is your own implementation
$scheduler->php('script.php')->before(function () use ($logger) {
$logger->info("script.php started at " . time());
});
```

### After job execution

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.
Expand All @@ -297,6 +310,21 @@ $scheduler->php('script.php')->then(function ($output) use ($logger) {
}, true);
```

#### Using "before" and "then" together

```php
// $logger here is your own implementation
$scheduler->php('script.php')
->before(function () use ($logger) {
$logger->info("script.php started at " . time());
})
->then(function ($output) use ($logger) {
$logger->info("script.php completed at " . time(), [
'output' => $output,
]);
});
```

### Multiple scheduler runs
In some cases you might need to run the scheduler multiple times in the same script.
Although this is not a common case, the following methods will allow you to re-use the same instance of the scheduler.
Expand Down
25 changes: 25 additions & 0 deletions src/GO/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ class Job
*/
private $emailConfig = [];

/**
* A function to execute before the job is executed.
*
* @var callable
*/
private $before;

/**
* A function to execute after the job is executed.
*
Expand Down Expand Up @@ -365,6 +372,10 @@ public function run()
// Write lock file if necessary
$this->createLockFile();

if (is_callable($this->before)) {
call_user_func($this->before);
}

if (is_callable($compiled)) {
$this->output = $this->exec($compiled);
} else {
Expand Down Expand Up @@ -519,6 +530,20 @@ private function emailOutput()
return true;
}

/**
* Set function to be called before job execution
* Job object is injected as a parameter to callable function.
*
* @param callable $fn
* @return self
*/
public function before(callable $fn)
{
$this->before = $fn;

return $this;
}

/**
* Set a function to be called after job execution.
* By default this will force the job to run in foreground
Expand Down
18 changes: 18 additions & 0 deletions tests/GO/JobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,24 @@ public function testShouldReturnOutputOfJobExecution()
$this->assertEquals(['hi'], $job3->getOutput());
}

public function testShouldRunCallbackBeforeJobExecution()
{
$job = new Job(function () {
return 'Job for testing before function';
});

$callbackWasExecuted = false;
$outputWasSet = false;

$job->before(function () use ($job, &$callbackWasExecuted, &$outputWasSet) {
$callbackWasExecuted = true;
$outputWasSet = ! is_null($job->getOutput());
})->run();

$this->assertTrue($callbackWasExecuted);
$this->assertFalse($outputWasSet);
}

public function testShouldRunCallbackAfterJobExecution()
{
$job = new Job(function () {
Expand Down

0 comments on commit 0c8926e

Please sign in to comment.