From f4e34e9c115d2e54165d2d8a2fd0181ee5abc8a0 Mon Sep 17 00:00:00 2001 From: Olivier Dahinden Date: Wed, 27 Nov 2019 15:18:03 +0100 Subject: [PATCH] Add jobby output to console log 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. --- README.md | 1 + composer.json | 5 +++++ src/BackgroundJob.php | 14 +++++++++++-- src/Jobby.php | 10 +++++++-- tests/JobbyTest.php | 48 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 74 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 101c979..db7df37 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/composer.json b/composer.json index 5327c2d..8a0efc0 100644 --- a/composer.json +++ b/composer.json @@ -34,5 +34,10 @@ "psr-4": { "Jobby\\Tests\\": "tests" } + }, + "scripts": { + "tests": [ + "php vendor/bin/phpunit --configuration phpunit.xml" + ] } } diff --git a/src/BackgroundJob.php b/src/BackgroundJob.php index ea754f6..035c3cf 100644 --- a/src/BackgroundJob.php +++ b/src/BackgroundJob.php @@ -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, @@ -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)); } @@ -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\""; @@ -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'."); diff --git a/src/Jobby.php b/src/Jobby.php index 53858af..81d7c8c 100644 --- a/src/Jobby.php +++ b/src/Jobby.php @@ -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, @@ -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 diff --git a/tests/JobbyTest.php b/tests/JobbyTest.php index 9218ee7..8fe21c3 100644 --- a/tests/JobbyTest.php +++ b/tests/JobbyTest.php @@ -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