forked from j-guyon/CommandSchedulerBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
230 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
namespace JMose\CommandSchedulerBundle\Fixtures\ORM; | ||
|
||
use Doctrine\Common\DataFixtures\FixtureInterface; | ||
use Doctrine\Persistence\ObjectManager; | ||
use JMose\CommandSchedulerBundle\Entity\ScheduledCommand; | ||
|
||
/** | ||
* Class LoadScheduledCommandData. | ||
* | ||
* @author Julien Guyon <[email protected]> | ||
*/ | ||
abstract class AbstractScheduledCommandData implements FixtureInterface | ||
{ | ||
/** | ||
* Create a new ScheduledCommand in database. | ||
* | ||
* @param $name | ||
* @param $command | ||
* @param $arguments | ||
* @param $cronExpression | ||
* @param $logFile | ||
* @param $priority | ||
* @param $lastExecution | ||
* @param bool $locked | ||
* @param bool $disabled | ||
* @param bool $executeNow | ||
* @param int $lastReturnCode | ||
*/ | ||
protected function createScheduledCommand( | ||
$name, $command, $arguments, $cronExpression, $logFile, $priority, $lastExecution, | ||
$locked = false, $disabled = false, $executeNow = false, $lastReturnCode = null) | ||
{ | ||
$scheduledCommand = new ScheduledCommand(); | ||
$scheduledCommand | ||
->setName($name) | ||
->setCommand($command) | ||
->setArguments($arguments) | ||
->setCronExpression($cronExpression) | ||
->setLogFile($logFile) | ||
->setPriority($priority) | ||
->setLastExecution($lastExecution) | ||
->setLocked($locked) | ||
->setDisabled($disabled) | ||
->setLastReturnCode($lastReturnCode) | ||
->setExecuteImmediately($executeNow); | ||
|
||
$this->getManager()->persist($scheduledCommand); | ||
$this->getManager()->flush(); | ||
} | ||
|
||
/** | ||
* @return ObjectManager | ||
*/ | ||
abstract protected function getManager(): ObjectManager; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,16 +2,14 @@ | |
|
||
namespace JMose\CommandSchedulerBundle\Fixtures\ORM; | ||
|
||
use Doctrine\Common\DataFixtures\FixtureInterface; | ||
use Doctrine\Persistence\ObjectManager; | ||
use JMose\CommandSchedulerBundle\Entity\ScheduledCommand; | ||
|
||
/** | ||
* Class LoadScheduledCommandData. | ||
* | ||
* @author Julien Guyon <[email protected]> | ||
*/ | ||
class LoadScheduledCommandData implements FixtureInterface | ||
class LoadScheduledCommandData extends AbstractScheduledCommandData | ||
{ | ||
/** | ||
* @var ObjectManager | ||
|
@@ -36,39 +34,10 @@ public function load(ObjectManager $manager) | |
} | ||
|
||
/** | ||
* Create a new ScheduledCommand in database. | ||
* | ||
* @param $name | ||
* @param $command | ||
* @param $arguments | ||
* @param $cronExpression | ||
* @param $logFile | ||
* @param $priority | ||
* @param $lastExecution | ||
* @param bool $locked | ||
* @param bool $disabled | ||
* @param bool $executeNow | ||
* @param int $lastReturnCode | ||
* @inheritDoc | ||
*/ | ||
protected function createScheduledCommand( | ||
$name, $command, $arguments, $cronExpression, $logFile, $priority, $lastExecution, | ||
$locked = false, $disabled = false, $executeNow = false, $lastReturnCode = null) | ||
protected function getManager(): ObjectManager | ||
{ | ||
$scheduledCommand = new ScheduledCommand(); | ||
$scheduledCommand | ||
->setName($name) | ||
->setCommand($command) | ||
->setArguments($arguments) | ||
->setCronExpression($cronExpression) | ||
->setLogFile($logFile) | ||
->setPriority($priority) | ||
->setLastExecution($lastExecution) | ||
->setLocked($locked) | ||
->setDisabled($disabled) | ||
->setLastReturnCode($lastReturnCode) | ||
->setExecuteImmediately($executeNow); | ||
|
||
$this->manager->persist($scheduledCommand); | ||
$this->manager->flush(); | ||
return $this->manager; | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
Fixtures/ORM/LoadScheduledCommandWithDynamicValuesData.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
namespace JMose\CommandSchedulerBundle\Fixtures\ORM; | ||
|
||
use DateTime; | ||
use Doctrine\Persistence\ObjectManager; | ||
use JMose\CommandSchedulerBundle\Tests\App\Command\TestCommand; | ||
|
||
/** | ||
* Class LoadScheduledCommandData. | ||
* | ||
* @author Julien Guyon <[email protected]> | ||
*/ | ||
class LoadScheduledCommandWithDynamicValuesData extends AbstractScheduledCommandData | ||
{ | ||
const LAST_EXECUTION_DATE = '2021-01-02 08:01:02'; | ||
const LAST_RETURN_CODE_0 = 0; | ||
const LAST_RETURN_CODE_NEGATIVE_1 = -1; | ||
const LOG_FILE = 'LoadScheduledCommandWithDynamicValuesData_log_file.log'; | ||
|
||
/** | ||
* @var ObjectManager | ||
*/ | ||
protected $manager; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function load(ObjectManager $manager) | ||
{ | ||
$this->manager = $manager; | ||
|
||
$this->createScheduledCommand( | ||
__CLASS__ . '_one', | ||
'scheduler:execute:test', | ||
'%last_execution% %log_file% --' . TestCommand::LAST_RETURN_CODE . '=%last_return_code%', | ||
'@daily', | ||
self::LOG_FILE, | ||
40, | ||
new DateTime(self::LAST_EXECUTION_DATE), | ||
false, | ||
false, | ||
true, | ||
self::LAST_RETURN_CODE_0 | ||
); | ||
$this->createScheduledCommand( | ||
__CLASS__ . '_one', | ||
'scheduler:execute:test', | ||
'%last_execution% %log_file% --' . TestCommand::LAST_RETURN_CODE . '=%last_return_code%', | ||
'@daily', | ||
self::LOG_FILE, | ||
40, | ||
new DateTime(self::LAST_EXECUTION_DATE), | ||
false, | ||
false, | ||
true, | ||
self::LAST_RETURN_CODE_NEGATIVE_1 | ||
); | ||
|
||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
protected function getManager(): ObjectManager | ||
{ | ||
return $this->manager; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
|
||
namespace JMose\CommandSchedulerBundle\Tests\App\Command; | ||
|
||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
class TestCommand extends Command | ||
{ | ||
const OUTPUT_LOG_FILE = __DIR__ . '/../logs/TEST_COMMAND_OUTPUT_LOG_FILE.txt'; | ||
const LAST_EXECUTION_TIME = 'last_execution_time'; | ||
const LOG_FILE = 'log_file'; | ||
const LAST_RETURN_CODE = 'last_return_code'; | ||
|
||
private static $index = 0; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function configure() | ||
{ | ||
$this | ||
->setName('scheduler:execute:test') | ||
->setDescription('Fake command') | ||
->addArgument(self::LAST_EXECUTION_TIME, null, InputArgument::REQUIRED, 'Last execution time params') | ||
->addArgument(self::LOG_FILE, null, InputArgument::OPTIONAL, 'Log file') | ||
->addOption(self::LAST_RETURN_CODE, null, InputOption::VALUE_REQUIRED, 'Last return Code'); | ||
} | ||
|
||
/** | ||
* @param InputInterface $input | ||
* @param OutputInterface $output | ||
* | ||
* @return int | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$content = array(); | ||
if (file_exists(self::OUTPUT_LOG_FILE)) { | ||
$content = json_decode(file_get_contents(self::OUTPUT_LOG_FILE), true); | ||
} | ||
$content[self::$index] = array( | ||
self::LAST_EXECUTION_TIME => $input->getArgument(self::LAST_EXECUTION_TIME), | ||
self::LOG_FILE => $input->getArgument(self::LOG_FILE), | ||
self::LAST_RETURN_CODE => (int) $input->getOption(self::LAST_RETURN_CODE) | ||
); | ||
file_put_contents(self::OUTPUT_LOG_FILE, json_encode($content)); | ||
self::$index++; | ||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters