Skip to content

Commit

Permalink
Merge pull request #33 from Indatus/develop
Browse files Browse the repository at this point in the history
Commands now run asynchronously in both windows and linux correctly
  • Loading branch information
bkuhl committed Apr 22, 2014
2 parents 487b619 + b68ab09 commit 850e919
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 14 deletions.
3 changes: 3 additions & 0 deletions src/Indatus/Dispatcher/Platform.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
* file that was distributed with this source code.
*/

/**
* @codeCoverageIgnore
*/
class Platform {

/**
Expand Down
21 changes: 16 additions & 5 deletions src/Indatus/Dispatcher/Services/CommandService.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ public function getRunCommand(
array $arguments = array(),
array $options = array())
{
/** @var \Indatus\Dispatcher\Platform $platform */
$platform = App::make('Indatus\Dispatcher\Platform');

$commandPieces = array(
'php',
base_path().'/artisan',
Expand All @@ -144,12 +147,20 @@ public function getRunCommand(
$commandPieces[] = $this->prepareOptions($options);
}

$commandPieces[] = '&'; //run in background
$commandPieces[] = '> /dev/null 2>&1'; //don't show output, errors can be viewed in the Laravel log
if ($platform->isUnix()) {
$commandPieces[] = '> /dev/null'; //don't show output, errors can be viewed in the Laravel log
$commandPieces[] = '&'; //run in background

//run the command as a different user
if (is_string($scheduledCommand->user())) {
array_unshift($commandPieces, 'sudo -u '.$scheduledCommand->user());
}
} elseif($platform->isWindows()) {
$commandPieces[] = '> NUL'; //don't show output, errors can be viewed in the Laravel log

//run the command as a different user
if (is_string($scheduledCommand->user())) {
array_unshift($commandPieces, 'sudo -u '.$scheduledCommand->user());
//run in background on windows
array_unshift($commandPieces, '/B');
array_unshift($commandPieces, 'START');
}

return implode(' ', $commandPieces);
Expand Down
2 changes: 1 addition & 1 deletion tests/Drivers/Cron/TestCronScheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public function testOpts()
$this->assertEquals($args, $scheduler->getArguments());
$this->assertEquals($opts, $scheduler->getOptions());

//be sure schedule reset, if not then we didn't get a fresh instance
//be sure schedule reset, if not then we didn't get a fresh SchedulerClass
$this->assertEquals($scheduler->getSchedule(), $this->defaultSchedule);
}

Expand Down
37 changes: 29 additions & 8 deletions tests/Services/TestCommandService.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,29 @@ public function testGetRunCommand()
'php',
base_path().'/artisan',
$commandName,
'&',
'> /dev/null 2>&1'
'> /dev/null',
'&'
)));
}

public function testGetRunCommandWindows()
{
$this->app->instance('Indatus\Dispatcher\Platform', m::mock('Indatus\Dispatcher\Platform', function ($m) {
$m->shouldReceive('isUnix')->andReturn(false);
$m->shouldReceive('isWindows')->andReturn(true);
}));

$commandName = 'test:command';
$scheduledCommand = $this->mockCommand();
$scheduledCommand->shouldReceive('getName')->andReturn($commandName);
$scheduledCommand->shouldReceive('user')->andReturn(false);
$this->assertEquals($this->commandService->getRunCommand($scheduledCommand), implode(' ', array(
'START',
'/B',
'php',
base_path().'/artisan',
$commandName,
'> NUL'
)));
}

Expand All @@ -181,8 +202,8 @@ public function testGetRunCommandWithArguments()
base_path().'/artisan',
$commandName,
'option',
'&',
'> /dev/null 2>&1'
'> /dev/null',
'&'
)));
}

Expand All @@ -207,8 +228,8 @@ public function testGetRunCommandWithOptions()
$commandName,
'--option="value"',
'--anotherOption',
'&',
'> /dev/null 2>&1'
'> /dev/null',
'&'
)));
}

Expand All @@ -224,8 +245,8 @@ public function testGetRunCommandAsUser()
'php',
base_path().'/artisan',
$commandName,
'&',
'> /dev/null 2>&1'
'> /dev/null',
'&'
)));
}

Expand Down

0 comments on commit 850e919

Please sign in to comment.