Skip to content

Commit f4e34e9

Browse files
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.
1 parent 042865d commit f4e34e9

5 files changed

Lines changed: 74 additions & 4 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ _**Logging**_ | | | _**Options fo
204204
output | string | /dev/null | Redirect `stdout` and `stderr` to this file
205205
output_stdout | string | value from `output` option | Redirect `stdout` to this file
206206
output_stderr | string | value from `output` option | Redirect `stderr` to this file
207+
output_console | boolean | false | Echo all output to console
207208
dateFormat | string | Y-m-d H:i:s | Format for dates on `jobby` log messages
208209
_**Mailing**_ | | | _**Options for emailing errors**_
209210
recipients | string | null | Comma-separated string of email addresses

composer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,10 @@
3434
"psr-4": {
3535
"Jobby\\Tests\\": "tests"
3636
}
37+
},
38+
"scripts": {
39+
"tests": [
40+
"php vendor/bin/phpunit --configuration phpunit.xml"
41+
]
3742
}
3843
}

src/BackgroundJob.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public function __construct($job, array $config, Helper $helper = null)
5151
'output' => null,
5252
'output_stdout' => null,
5353
'output_stderr' => null,
54+
'output_console' => false,
5455
'dateFormat' => null,
5556
'enabled' => null,
5657
'haltDir' => null,
@@ -245,11 +246,15 @@ protected function runFunction()
245246
$retval = $e->getMessage();
246247
}
247248
$content = ob_get_contents();
248-
if ($logfile = $this->getLogfile()) {
249+
if ($logfile = $this->getLogfile() && !$this->config['output_console']) {
249250
file_put_contents($this->getLogfile(), $content, FILE_APPEND);
250251
}
251252
ob_end_clean();
252253

254+
if ($this->config['output_console']) {
255+
echo $content;
256+
}
257+
253258
if ($retval !== true) {
254259
throw new Exception("Closure did not return true! Returned:\n" . print_r($retval, true));
255260
}
@@ -272,6 +277,7 @@ protected function runFile()
272277

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

283-
exec($command, $dummy, $retval);
289+
if ($this->config['output_console']) {
290+
echo exec($command_original, $dummy, $retval);
291+
} else {
292+
exec($command, $dummy, $retval);
293+
}
284294

285295
if ($retval !== 0) {
286296
throw new Exception("Job exited with status '$retval'.");

src/Jobby.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public function getDefaultConfig()
7777
'output' => null,
7878
'output_stdout' => null,
7979
'output_stderr' => null,
80+
'output_console' => false,
8081
'dateFormat' => 'Y-m-d H:i:s',
8182
'enabled' => true,
8283
'haltDir' => null,
@@ -178,8 +179,13 @@ protected function runUnix($job, array $config)
178179
$command = $this->getExecutableCommand($job, $config);
179180
$binary = $this->getPhpBinary();
180181

181-
$output = $config['debug'] ? 'debug.log' : '/dev/null';
182-
exec("$binary $command 1> $output 2>&1 &");
182+
if (!$config['output_console']) {
183+
$output = $config['debug'] ? 'debug.log' : '/dev/null';
184+
exec("$binary $command 1> $output 2>&1 &");
185+
return;
186+
}
187+
188+
echo exec("$binary $command");
183189
}
184190

185191
// @codeCoverageIgnoreStart

tests/JobbyTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,54 @@ public function testClosure()
149149
$this->assertEquals('A function!', $this->getLogContent());
150150
}
151151

152+
/**
153+
* @covers ::add
154+
* @covers ::run
155+
*/
156+
public function testClosureStdout()
157+
{
158+
$jobby = new Jobby();
159+
$jobby->add(
160+
'HelloWorldClosureStdout',
161+
[
162+
'command' => static function () {
163+
echo 'console output';
164+
return true;
165+
},
166+
'schedule' => '* * * * *',
167+
'output_console' => true,
168+
]
169+
);
170+
ob_start();
171+
$jobby->run();
172+
$content = ob_get_contents();
173+
ob_end_clean();
174+
175+
$this->assertEquals('console output', $content);
176+
}
177+
178+
/**
179+
* @covers ::add
180+
* @covers ::run
181+
*/
182+
public function testFileStdout()
183+
{
184+
$jobby = new Jobby();
185+
$jobby->add(
186+
'HelloWorldFileStdout',
187+
[
188+
'command' => 'php ' . __DIR__ . '/_files/helloworld.php',
189+
'schedule' => '* * * * *',
190+
'output_console' => true,
191+
]
192+
);
193+
ob_start();
194+
$jobby->run();
195+
$content = ob_get_contents();
196+
ob_end_clean();
197+
$this->assertEquals('Hello World!', $content);
198+
}
199+
152200
/**
153201
* @covers ::add
154202
* @covers ::run

0 commit comments

Comments
 (0)