Skip to content

Commit

Permalink
Moved commands that are closures to "closure" option, so we don't hav…
Browse files Browse the repository at this point in the history
…e to hackishly figure out if string is closure data or not. Deprecated passing SerializableClosures in directly, not sure why this is needed and it breaks stuff.
  • Loading branch information
Carson Full committed Dec 8, 2015
1 parent af29a03 commit d75fe89
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 41 deletions.
22 changes: 4 additions & 18 deletions src/BackgroundJob.php
Expand Up @@ -3,10 +3,11 @@
namespace Jobby;

use Cron\CronExpression;
use SuperClosure\SerializableClosure;

class BackgroundJob
{
use SerializerTrait;

/**
* @var Helper
*/
Expand Down Expand Up @@ -83,7 +84,7 @@ public function run()
$this->helper->acquireLock($lockFile);
$lockAcquired = true;

if ($this->isFunction()) {
if (isset($this->config['closure'])) {
$this->runFunction();
} else {
$this->runFile();
Expand Down Expand Up @@ -227,24 +228,9 @@ protected function log($message)
}
}

/**
* @return bool
*/
protected function isFunction()
{
$cmd = @unserialize($this->config['command']);

if ($cmd === false) {
return false;
}

return is_object($cmd) && $cmd instanceof SerializableClosure;
}

protected function runFunction()
{
/** @var SerializableClosure $command */
$command = unserialize($this->config['command']);
$command = $this->getSerializer()->unserialize($this->config['closure']);

ob_start();
$retval = $command();
Expand Down
37 changes: 24 additions & 13 deletions src/Jobby.php
Expand Up @@ -2,11 +2,14 @@

namespace Jobby;

use Closure;
use SuperClosure\SerializableClosure;
use Symfony\Component\Process\PhpExecutableFinder;

class Jobby
{
use SerializerTrait;

/**
* @var array
*/
Expand Down Expand Up @@ -104,9 +107,25 @@ public function getConfig()
*/
public function add($job, array $config)
{
foreach (['command', 'schedule'] as $field) {
if (empty($config[$field])) {
throw new Exception("'$field' is required for '$job' job");
if (empty($config['schedule'])) {
throw new Exception("'schedule' is required for '$job' job");
}

if (!(isset($config['command']) xor isset($config['closure']))) {
throw new Exception("Either 'command' or 'closure' is required for '$job' job");
}

if (isset($config['command']) &&
(
$config['command'] instanceof Closure ||
$config['command'] instanceof SerializableClosure
)
) {
$config['closure'] = $config['command'];
unset($config['command']);

if ($config['closure'] instanceof SerializableClosure) {
$config['closure'] = $config['closure']->getClosure();
}
}

Expand Down Expand Up @@ -170,17 +189,9 @@ protected function runWindows($job, array $config)
*/
protected function getExecutableCommand($job, array $config)
{
if ($config['command'] instanceof SerializableClosure) {
$config['command'] = serialize($config['command']);

} else if ($config['command'] instanceof \Closure) {
// Convert closures to its source code as a string so that we
// can send it on the command line.
$config['command'] = $this->getHelper()
->closureToString($config['command'])
;
if (isset($config['closure'])) {
$config['closure'] = $this->getSerializer()->serialize($config['closure']);
}

return sprintf('"%s" "%s" "%s"', $this->script, $job, http_build_query($config));
}

Expand Down
23 changes: 13 additions & 10 deletions tests/BackgroundJobTest.php
Expand Up @@ -4,13 +4,16 @@

use Jobby\BackgroundJob;
use Jobby\Helper;
use Jobby\SerializerTrait;
use Symfony\Component\Filesystem\Filesystem;

/**
* @coversDefaultClass Jobby\BackgroundJob
*/
class BackgroundJobTest extends \PHPUnit_Framework_TestCase
{
use SerializerTrait;

const JOB_NAME = 'name';

/**
Expand Down Expand Up @@ -51,15 +54,15 @@ public function runProvider()

return true;
};
$job = ['command' => $echo];
$job = ['closure' => $echo];

return [
'diabled, not run' => [$job + ['enabled' => false], ''],
'cron schedule, not run' => [$job + ['schedule' => '0 0 1 1 *'], ''],
'date time, not run' => [$job + ['schedule' => date('Y-m-d H:i:s', strtotime('tomorrow'))], ''],
'date time, run' => [$job + ['schedule' => date('Y-m-d H:i:s')], 'test'],
'wrong host, not run' => [$job + ['runOnHost' => 'something that does not match'], ''],
'current user, run,' => [['command' => $uid], getmyuid()],
'current user, run,' => [['closure' => $uid], getmyuid()],
];
}

Expand Down Expand Up @@ -100,7 +103,7 @@ public function testClosureNotReturnTrue()
{
$this->runJob(
[
'command' => function () {
'closure' => function () {
return false;
},
]
Expand All @@ -120,7 +123,7 @@ public function testHideStdOutByDefault()
ob_start();
$this->runJob(
[
'command' => function () {
'closure' => function () {
echo 'foo bar';
},
'output' => null,
Expand All @@ -140,7 +143,7 @@ public function testShouldCreateLogFolder()
$logfile = dirname($this->logFile) . '/foo/bar.log';
$this->runJob(
[
'command' => function () {
'closure' => function () {
echo 'foo bar';
},
'output' => $logfile,
Expand Down Expand Up @@ -169,7 +172,7 @@ public function testNotSendMailOnMissingRecipients()

$this->runJob(
[
'command' => function () {
'closure' => function () {
return false;
},
'recipients' => '',
Expand All @@ -190,7 +193,7 @@ public function testMailShouldTriggerHelper()

$this->runJob(
[
'command' => function () {
'closure' => function () {
return false;
},
'recipients' => 'test@example.com',
Expand Down Expand Up @@ -267,7 +270,7 @@ public function testHaltDir($createFile, $jobRuns)
$this->runJob(
[
'haltDir' => $dir,
'command' => function () {
'closure' => function () {
echo 'test';

return true;
Expand Down Expand Up @@ -312,8 +315,8 @@ private function getJobConfig(array $config)
{
$helper = new Helper();

if ($config['command'] instanceof \Closure) {
$config['command'] = $helper->closureToString($config['command']);
if (isset($config['closure'])) {
$config['closure'] = $this->getSerializer()->serialize($config['closure']);
}

return array_merge(
Expand Down

0 comments on commit d75fe89

Please sign in to comment.