Skip to content

Commit

Permalink
Add before run method (#33)
Browse files Browse the repository at this point in the history
* Updated example

Ref #30

* Add before run method

* Add unit test to before function and remove job instance parameter
  • Loading branch information
Šarūnas Norkus authored and peppeocchi committed Oct 8, 2017
1 parent 2bec937 commit c0c5cef
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -285,14 +285,15 @@ For example you might want to add an entry to you logs, ping a url etc...
By default, the job will be forced to run in foreground (because the output is injected to the function), if you don't need the output, you can pass `true` as a second parameter to allow the execution in background (in this case `$output` will be empty).

```php
// $logger and $messenger here are your own implementation
$scheduler->php('script.php')->then(function ($output) use ($logger, $messenger) {
$logger->info($output);

$messenger->ping('myurl.com', $output);
});

$scheduler->php('script.php')->then(function ($output) {
log('Job executed!');
$scheduler->php('script.php')->then(function ($output) use ($logger) {
$logger->info('Job executed!');
}, true);
```

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 c0c5cef

Please sign in to comment.