Skip to content
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
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/php-cron-scheduler.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions .idea/php.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/phpunit.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
],
"minimum-stability": "dev",
"require": {
"php": "^7.1",
"php": "^7.2",
"dragonmantank/cron-expression": "^2.3"
},
"require-dev": {
Expand Down
53 changes: 39 additions & 14 deletions src/GO/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ class Job
*/
private $outputMode;

/**
* @var bool
*/
private $captureStdErr = false;

/**
* Create a new Job instance.
*
Expand Down Expand Up @@ -215,8 +220,8 @@ public function isDue(DateTime $date = null)
public function isOverlapping()
{
return $this->lockFile &&
file_exists($this->lockFile) &&
call_user_func($this->whenOverlapping, filemtime($this->lockFile)) === false;
file_exists($this->lockFile) &&
call_user_func($this->whenOverlapping, filemtime($this->lockFile)) === false;
}

/**
Expand Down Expand Up @@ -299,27 +304,34 @@ public function compile()
}
}

// Add the boilerplate to redirect the output to file/s
// Handle output redirection for different platforms
if (count($this->outputTo) > 0) {
$compiled .= ' | tee ';
$compiled .= $this->outputMode === 'a' ? '-a ' : '';
foreach ($this->outputTo as $file) {
$compiled .= $file . ' ';
if ($this->captureStdErr) {
$compiled .= ' 2>&1';
}

$compiled = trim($compiled);
foreach ($this->outputTo as $file) {
$compiled .= ' >> ' . escapeshellarg($file);
}
}

// Add boilerplate to remove lockfile after execution
// Handle lock file removal
if ($this->lockFile) {
$compiled .= '; rm ' . $this->lockFile;
$lockFile = escapeshellarg($this->lockFile);
if (PHP_OS_FAMILY === 'Windows') {
$compiled .= " && del $lockFile";
} else {
$compiled .= " && rm $lockFile";
}
}

// Add boilerplate to run in background
// Background execution
if ($this->canRunInBackground()) {
// Parentheses are need execute the chain of commands in a subshell
// that can then run in background
$compiled = '(' . $compiled . ') > /dev/null 2>&1 &';
if (PHP_OS_FAMILY === 'Windows') {
$compiled = 'start /B ' . $compiled;
} else {
$compiled .= ' &';
}
}

return trim($compiled);
Expand Down Expand Up @@ -477,6 +489,19 @@ public function output($filename, $append = false)
return $this;
}

/**
* Set the option for writing stderr to the output file.
*
* @param bool $capture
* @return self
*/
public function captureStandardError($capture = true)
{
$this->captureStdErr = $capture;

return $this;
}

/**
* Get the job output.
*
Expand Down
43 changes: 43 additions & 0 deletions tests/GO/JobOutputFilesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,47 @@ public function testShouldWriteFunctionOutputAndReturnToFile()

unlink($outputFile);
}

public function testShouldWriteStdErrToFile()
{
$command = PHP_BINARY . ' ' . __DIR__ . '/../error_job.php';
$job = new Job($command);
$outputFile = __DIR__ . '/../tmp/output.log';

@unlink($outputFile);

// Test fist that the file doesn't exist yet
$this->assertFalse(file_exists($outputFile));
$job->captureStandardError();
$job->output($outputFile)->run();

sleep(2);
$this->assertTrue(file_exists($outputFile));

// Content should be the error for calling an undefined function
$this->assertStringStartsWith('PHP Fatal error:', file_get_contents($outputFile));

unlink($outputFile);
}

public function testShouldNotWriteStdErrToFile()
{
$command = PHP_BINARY . ' ' . __DIR__ . '/../error_job.php';
$job = new Job($command);
$outputFile = __DIR__ . '/../tmp/output.log';

@unlink($outputFile);

// Test fist that the file doesn't exist yet
$this->assertFalse(file_exists($outputFile));
$job->output($outputFile)->run();

sleep(2);
$this->assertTrue(file_exists($outputFile));

// Content should be ''
$this->assertEquals('', file_get_contents($outputFile));

unlink($outputFile);
}
}
3 changes: 3 additions & 0 deletions tests/error_job.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

call_a_function_that_does_not_exist();