Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add jobby output to console log #120

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
'output_console' => 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