Skip to content

Commit

Permalink
Merge pull request #105 from usemarkup/fix/add-native-argument-suppor…
Browse files Browse the repository at this point in the history
…t-for-recurring-jobs

fix: add native argument support for recurring jobs
  • Loading branch information
calumbrodie committed Sep 14, 2020
2 parents 40eb3ca + 4560ae7 commit a9a0977
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 11 deletions.
40 changes: 35 additions & 5 deletions Command/AddRecurringConsoleJobToQueueCommand.php
Expand Up @@ -108,17 +108,28 @@ private function addRecurringJobs(OutputInterface $output)
}

$command = $configuration->getCommand();
$arguments = [];

// i.e. does the command already have options or arguments within the string
if (stripos($configuration->getCommand(), ' ') !== false) {
$command = trim(strstr($configuration->getCommand(), ' ', true));
$arguments = explode(' ', trim(strstr($configuration->getCommand(), ' ', false)));
throw new \LogicException(sprintf('%s Command cannot contain spaces', $configuration->getCommand()));
}

foreach ($configuration->getArguments() as $argument) {
if (!is_string($argument)) {
throw new \Exception('Argument was expected to be a string');
}

$this->validateNoQuotes($argument, $configuration);

if (substr($argument, 0, 2) === '--') {
$optionValue = ltrim(strstr($argument, '='), '=');

$this->validateNoQuotes($optionValue, $configuration);
}
}

$this->jobManager->addConsoleCommandJob(
$command,
$arguments,
$configuration->getArguments(),
$configuration->getTopic(),
$configuration->getTimeout(),
$configuration->getTimeout()
Expand All @@ -141,4 +152,23 @@ private function maintainJobLogs()
{
$this->jobLogRepository->removeExpiredJobs();
}

/**
* @param string $argument
* @param RecurringConsoleCommandConfiguration $configuration
* @throws \Exception
*/
private function validateNoQuotes(string $argument, RecurringConsoleCommandConfiguration $configuration): void
{
$firstCharacter = substr($argument, 0, 1);
$lastCharacter = substr($argument, strlen($argument)-1, 1);

if ($firstCharacter === '"' && $lastCharacter === '"') {
throw new \Exception(sprintf('remove quotes as they will be included as literal values on %s', $configuration->getCommand()));
}

if ($firstCharacter === "'" && $lastCharacter === "'") {
throw new \Exception(sprintf('remove quotes as they will be included as literal values on %s', $configuration->getCommand()));
}
}
}
25 changes: 20 additions & 5 deletions Model/RecurringConsoleCommandConfiguration.php
Expand Up @@ -41,16 +41,23 @@ class RecurringConsoleCommandConfiguration
private $envs;

/**
* @param string $command
* @param string $topic
* @param string $schedule
* @param string|null $description
* @var array
*/
private $arguments;

/**
* @param string $command
* @param array $arguments
* @param string $topic
* @param string $schedule
* @param string|null $description
* @param integer|null $timeout
* @param array|null $envs
*/
public function __construct($command, $topic, $schedule, $description = null, $timeout = 60, $envs = null)
public function __construct($command, array $arguments, $topic, $schedule, $description = null, $timeout = 60, $envs = null)
{
$this->command = $command;
$this->arguments = $arguments;
$this->schedule = $schedule;
$this->topic = str_replace('-', '_', $topic);
$this->timeout = $timeout;
Expand All @@ -76,6 +83,14 @@ public function getCommand()
return $this->command;
}

/**
* @return array
*/
public function getArguments(): array
{
return $this->arguments;
}

/**
* @return string
*/
Expand Down
5 changes: 5 additions & 0 deletions Service/RecurringConsoleCommandReader.php
Expand Up @@ -129,8 +129,13 @@ private function parseConfiguration(array $config)
throw new InvalidConfigurationException('`envs` config key must be an array or null');
}

if (isset($group['arguments']) && !is_array($group['arguments'])) {
throw new InvalidConfigurationException(sprintf('`arguments` config key must be an array for %s', $group['command']));
}

$recurringConsoleCommandConfiguration = new RecurringConsoleCommandConfiguration(
$group['command'],
$group['arguments'] ?? [],
$group['topic'],
$group['schedule'],
isset($group['description']) ? $group['description'] : null,
Expand Down
2 changes: 1 addition & 1 deletion Tests/Model/RecurringConsoleCommandConfigurationTest.php
Expand Up @@ -9,7 +9,7 @@ class RecurringConsoleCommandConfigurationTest extends TestCase
{
public function testCanBeConstructed()
{
$config = new RecurringConsoleCommandConfiguration('foo:bar', 'test', '30 1 * * *', 'a short description');
$config = new RecurringConsoleCommandConfiguration('foo:bar', [], 'test', '30 1 * * *', 'a short description');
$this->assertEquals($config->getCommand(), 'foo:bar');
$this->assertEquals($config->getTopic(), 'test');
$this->assertEquals($config->getSchedule(), '30 1 * * *');
Expand Down

0 comments on commit a9a0977

Please sign in to comment.