Skip to content

Commit

Permalink
Add jobby output to console log
Browse files Browse the repository at this point in the history
For some time I was in need to use jobby with console output. Since
we are working in cloud foundry environment and we capture application
log with a drainlog this was quite critical.

This commit introduces the config flag `output_console` which takes
a boolean to either activate the output to console. What it actually
does is remove the bash output to logs and puts an echo infront of
the `exec()`.

While at it: Add composer script for tests. Add 2 testcases for
closures and files.
  • Loading branch information
mgbolivierdahinden committed Nov 27, 2019
1 parent 042865d commit 6f53dc3
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ _**Logging**_ | | | _**Options fo
output | string | /dev/null | Redirect `stdout` and `stderr` to this file
output_stdout | string | value from `output` option | Redirect `stdout` to this file
output_stderr | string | value from `output` option | Redirect `stderr` to this file
output_console | boolean | false | Echo all output to console
dateFormat | string | Y-m-d H:i:s | Format for dates on `jobby` log messages
_**Mailing**_ | | | _**Options for emailing errors**_
recipients | string | null | Comma-separated string of email addresses
Expand Down
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,10 @@
"psr-4": {
"Jobby\\Tests\\": "tests"
}
},
"scripts": {
"tests": [
"php vendor/bin/phpunit --configuration phpunit.xml"
]
}
}
14 changes: 12 additions & 2 deletions src/BackgroundJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public function __construct($job, array $config, Helper $helper = null)
'output' => null,
'output_stdout' => null,
'output_stderr' => null,
'output_console' => false,
'dateFormat' => null,
'enabled' => null,
'haltDir' => null,
Expand Down Expand Up @@ -245,11 +246,15 @@ protected function runFunction()
$retval = $e->getMessage();
}
$content = ob_get_contents();
if ($logfile = $this->getLogfile()) {
if ($logfile = $this->getLogfile() && !$this->config['output_console']) {
file_put_contents($this->getLogfile(), $content, FILE_APPEND);
}
ob_end_clean();

if ($this->config['output_console']) {
echo $content;
}

if ($retval !== true) {
throw new Exception("Closure did not return true! Returned:\n" . print_r($retval, true));
}
Expand All @@ -272,6 +277,7 @@ protected function runFile()

// Start execution. Run in foreground (will block).
$command = $this->config['command'];
$command_original = $command;
$stdoutLogfile = $this->getLogfile() ?: $this->helper->getSystemNullDevice();
$stderrLogfile = $this->getLogfile('stderr') ?: $this->helper->getSystemNullDevice();
$command = "$useSudo $command 1>> \"$stdoutLogfile\" 2>> \"$stderrLogfile\"";
Expand All @@ -280,7 +286,11 @@ protected function runFile()
$command = "$useSudo $command >> \"$stdoutLogfile\" 2>&1";
}

exec($command, $dummy, $retval);
if ($this->config['output_console']) {
echo exec($command_original, $dummy, $retval);
} else {
exec($command, $dummy, $retval);
}

if ($retval !== 0) {
throw new Exception("Job exited with status '$retval'.");
Expand Down
10 changes: 8 additions & 2 deletions src/Jobby.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public function getDefaultConfig()
'output' => null,
'output_stdout' => null,
'output_stderr' => null,
'console_output' => false,
'dateFormat' => 'Y-m-d H:i:s',
'enabled' => true,
'haltDir' => null,
Expand Down Expand Up @@ -178,8 +179,13 @@ protected function runUnix($job, array $config)
$command = $this->getExecutableCommand($job, $config);
$binary = $this->getPhpBinary();

$output = $config['debug'] ? 'debug.log' : '/dev/null';
exec("$binary $command 1> $output 2>&1 &");
if (!$config['output_console']) {
$output = $config['debug'] ? 'debug.log' : '/dev/null';
exec("$binary $command 1> $output 2>&1 &");
return;
}

echo exec("$binary $command");
}

// @codeCoverageIgnoreStart
Expand Down
48 changes: 48 additions & 0 deletions tests/JobbyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,54 @@ public function testClosure()
$this->assertEquals('A function!', $this->getLogContent());
}

/**
* @covers ::add
* @covers ::run
*/
public function testClosureStdout()
{
$jobby = new Jobby();
$jobby->add(
'HelloWorldClosureStdout',
[
'command' => static function () {
echo 'console output';
return true;
},
'schedule' => '* * * * *',
'output_console' => true,
]
);
ob_start();
$jobby->run();
$content = ob_get_contents();
ob_end_clean();

$this->assertEquals('console output', $content);
}

/**
* @covers ::add
* @covers ::run
*/
public function testFileStdout()
{
$jobby = new Jobby();
$jobby->add(
'HelloWorldFileStdout',
[
'command' => 'php ' . __DIR__ . '/_files/helloworld.php',
'schedule' => '* * * * *',
'output_console' => true,
]
);
ob_start();
$jobby->run();
$content = ob_get_contents();
ob_end_clean();
$this->assertEquals('Hello World!', $content);
}

/**
* @covers ::add
* @covers ::run
Expand Down

0 comments on commit 6f53dc3

Please sign in to comment.