Skip to content

Commit

Permalink
Apply fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Aerendir committed May 2, 2019
1 parent 222b115 commit e14f1c3
Show file tree
Hide file tree
Showing 17 changed files with 210 additions and 206 deletions.
1 change: 1 addition & 0 deletions bin/validate
Expand Up @@ -4,6 +4,7 @@ vendor/bin/rector process src --dry-run &&
vendor/bin/php-cs-fixer fix --allow-risky yes --dry-run -v &&
vendor/bin/phpstan analyse src -c phpstan.neon --level max &&
vendor/bin/phan --progress-bar &&
phpunit &&
composer validate --strict --no-check-lock &&
composer outdated &&
vendor/bin/security-checker security:check
9 changes: 4 additions & 5 deletions src/Admin/Sonata/Twig/CommandsQueuesExtension.php
Expand Up @@ -18,7 +18,7 @@
namespace SerendipityHQ\Bundle\CommandsQueuesBundle\Admin\Sonata\Twig;

use Safe\Exceptions\StringsException;
use Safe\sprintf;
use function Safe\sprintf;
use SerendipityHQ\Bundle\CommandsQueuesBundle\Entity\Job;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Twig\Extension\AbstractExtension;
Expand Down Expand Up @@ -60,11 +60,10 @@ public function getFilters(): array
public function getIdOptionValue(Job $job): ?string
{
$input = $job->getInput();
if (false === array_key_exists('options', $input) && isset($input['options']['--id'])) {
$url = $this->generator->generate('admin_serendipityhq_commandsqueues_job_show', ['id' => $input['options']['--id']], UrlGeneratorInterface::ABSOLUTE_PATH);

return sprintf('<a href="%s">#%s</a>', $url, $input['options']['--id']);
if (null !== $input && false === array_key_exists('options', $input) && isset($input['options']['--id'])) {
$url = $this->generator->generate('admin_serendipityhq_commandsqueues_job_show', ['id' => $input['options']['--id']], UrlGeneratorInterface::ABSOLUTE_PATH);

return sprintf('<a href="%s">#%s</a>', $url, $input['options']['--id']);
}

return null;
Expand Down
15 changes: 8 additions & 7 deletions src/Command/InternalMarkAsCancelledCommand.php
Expand Up @@ -20,6 +20,7 @@
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use Safe\Exceptions\StringsException;
use function Safe\sprintf;
use SerendipityHQ\Bundle\CommandsQueuesBundle\Entity\Job;
use SerendipityHQ\Bundle\CommandsQueuesBundle\Repository\JobRepository;
use SerendipityHQ\Bundle\CommandsQueuesBundle\Util\JobsMarker;
Expand Down Expand Up @@ -103,7 +104,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int

if (null === $failedJob) {
// The job may not exist anymore if it expired and so was deleted
$this->getIoWriter()->infoLineNoBg(\Safe\sprintf("The job <success-nobg>%s</success-nobg> doesn't exist anymore.", $failedJob));
$this->getIoWriter()->infoLineNoBg(sprintf("The job <success-nobg>%s</success-nobg> doesn't exist anymore.", $failedJob));

return 0;
}
Expand All @@ -115,9 +116,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

// We only cancel childs and not the failed Job as the failed Job is marked as "failed" and we don't want to change its status)
$this->cancelChildJobs($failedJob, $cancellingJob, \Safe\sprintf('Parent Job %s failed.', $failedJob->getId()));
$this->cancelChildJobs($failedJob, $cancellingJob, sprintf('Parent Job %s failed.', $failedJob->getId()));

$this->getIoWriter()->successLineNoBg(\Safe\sprintf('All child jobs of Job %s and their respective child Jobs were marked as cancelled.', $failedJob->getId()));
$this->getIoWriter()->successLineNoBg(sprintf('All child jobs of Job %s and their respective child Jobs were marked as cancelled.', $failedJob->getId()));

return 0;
}
Expand All @@ -135,7 +136,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
*/
private function cancelChildJobs(Job $markedJob, Job $cancellingJob, string $cancellationReason, array $alreadyCancelledJobs = []): int
{
$this->getIoWriter()->infoLineNoBg(\Safe\sprintf('Start cancelling child Jobs of Job #%s@%s.', $markedJob->getId(), $markedJob->getQueue()));
$this->getIoWriter()->infoLineNoBg(sprintf('Start cancelling child Jobs of Job #%s@%s.', $markedJob->getId(), $markedJob->getQueue()));

// "Security check", no child jobs: ...
if ($markedJob->getChildDependencies()->count() <= 0) {
Expand All @@ -151,7 +152,7 @@ private function cancelChildJobs(Job $markedJob, Job $cancellingJob, string $can
],
];

$this->getIoWriter()->noteLineNoBg(\Safe\sprintf(
$this->getIoWriter()->noteLineNoBg(sprintf(
'[%s] Job #%s@%s: Found %s child dependencies. Start marking them.',
JobsUtil::getFormattedTime($markedJob, 'getClosedAt'), $markedJob->getId(), $markedJob->getQueue(), $markedJob->getChildDependencies()->count())
);
Expand Down Expand Up @@ -183,12 +184,12 @@ private function cancelChildJobs(Job $markedJob, Job $cancellingJob, string $can
// If this child has other childs on its own...
if ($childDependency->getChildDependencies()->count() > 0) {
// ... Mark as cancelled also the child Jobs of this child Job
$this->cancelChildJobs($childDependency, $cancellingJob, \Safe\sprintf('Child Job "#%s" were cancelled.', $childDependency->getId()), $alreadyCancelledJobs);
$this->cancelChildJobs($childDependency, $cancellingJob, sprintf('Child Job "#%s" were cancelled.', $childDependency->getId()), $alreadyCancelledJobs);
}
}

$cancelledChilds = implode(', ', $cancelledChilds);
$this->getIoWriter()->noteLineNoBg(\Safe\sprintf(
$this->getIoWriter()->noteLineNoBg(sprintf(
'[%s] Job #%s@%s: Cancelled childs are: %s', JobsUtil::getFormattedTime($markedJob, 'getClosedAt'), $markedJob->getId(), $markedJob->getQueue(), $cancelledChilds
));

Expand Down
19 changes: 10 additions & 9 deletions src/Command/RunCommand.php
Expand Up @@ -32,6 +32,7 @@
use Safe\Exceptions\PcntlException;
use Safe\Exceptions\StreamException;
use Safe\Exceptions\StringsException;
use function Safe\sprintf;
use SerendipityHQ\Bundle\CommandsQueuesBundle\Entity\Daemon;
use SerendipityHQ\Bundle\CommandsQueuesBundle\Repository\DaemonRepository;
use SerendipityHQ\Bundle\CommandsQueuesBundle\Service\QueuesDaemon;
Expand Down Expand Up @@ -175,7 +176,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
foreach ($this->daemon->getConfig()->getQueues() as $queueName) {
if (false === $this->daemon->canInitializeNewJobs($queueName)) {
if ($this->ioWriter->getVerbosity() >= OutputInterface::VERBOSITY_VERY_VERBOSE) {
$this->ioWriter->infoLineNoBg(\Safe\sprintf('The queue <success-nobg>%s</success-nobg> is already processing the max allowed number of concurrent Jobs.', $queueName));
$this->ioWriter->infoLineNoBg(sprintf('The queue <success-nobg>%s</success-nobg> is already processing the max allowed number of concurrent Jobs.', $queueName));
}

continue;
Expand All @@ -184,14 +185,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$jobsToLoad = $this->daemon->getJobsToLoad($queueName);
if (0 < $jobsToLoad) {
if ($this->ioWriter->getVerbosity() >= OutputInterface::VERBOSITY_VERY_VERBOSE) {
$this->ioWriter->infoLineNoBg(\Safe\sprintf('Trying to initialize <success-nobg>%s</success-nobg> new Jobs for queue <success-nobg>%s</success-nobg>...', $jobsToLoad, $queueName));
$this->ioWriter->infoLineNoBg(sprintf('Trying to initialize <success-nobg>%s</success-nobg> new Jobs for queue <success-nobg>%s</success-nobg>...', $jobsToLoad, $queueName));
$initializingJobs = ProgressBarFactory::createProgressBar(ProgressBarFactory::FORMAT_INITIALIZING_JOBS, $output, $jobsToLoad);
}
for ($i = 0; $i < $jobsToLoad; ++$i) {
// Start processing the next Job in the queue
if (false === $this->daemon->processNextJob($queueName)) {
if ($this->ioWriter->getVerbosity() >= OutputInterface::VERBOSITY_VERY_VERBOSE) {
$this->ioWriter->infoLineNoBg(\Safe\sprintf('Queue <success-nobg>%s</success-nobg> is empty: no more Jobs to initialize.', $queueName));
$this->ioWriter->infoLineNoBg(sprintf('Queue <success-nobg>%s</success-nobg> is empty: no more Jobs to initialize.', $queueName));
}

// The next Job is null: exit this queue and pass to the next one
Expand Down Expand Up @@ -249,7 +250,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

if ($this->ioWriter->getVerbosity() >= OutputInterface::VERBOSITY_VERY_VERBOSE) {
$this->ioWriter->infoLineNoBg(\Safe\sprintf(
$this->ioWriter->infoLineNoBg(sprintf(
'No Jobs to process. Idling for <success-nobg>%s seconds<success-nobg>...', $this->daemon->getConfig()->getSleepFor()
));
}
Expand Down Expand Up @@ -317,20 +318,20 @@ private function checkAliveDaemons(): void
return;
}

$this->ioWriter->infoLineNoBg(\Safe\sprintf('Found <success-nobg>%s</success-nobg> struggler Daemon(s).', count($strugglers)));
$this->ioWriter->infoLineNoBg(sprintf('Found <success-nobg>%s</success-nobg> struggler Daemon(s).', count($strugglers)));
$this->ioWriter->commentLineNoBg('Their "diedOn" date is set to NOW and mortisCausa is "struggler".');

$table = [];
foreach ($strugglers as $strugglerDaemon) {
$strugglerDaemonDieOn = $strugglerDaemon->getDiedOn();

if (null === $strugglerDaemonDieOn) {
throw new RuntimeException(\Safe\sprintf("The daemon %s is being processed as struggler, but it hasn't a die date.", $strugglerDaemon->getId()));
throw new RuntimeException(sprintf("The daemon %s is being processed as struggler, but it hasn't a die date.", $strugglerDaemon->getId()));
}

$age = $strugglerDaemonDieOn->diff($strugglerDaemon->getBornOn());
$table[] = [
\Safe\sprintf('<%s>%s</>', 'success-nobg', "\xE2\x9C\x94"),
sprintf('<%s>%s</>', 'success-nobg', "\xE2\x9C\x94"),
$strugglerDaemon->getPid(),
$strugglerDaemon->getHost(),
$strugglerDaemon->getBornOn()->format('Y-m-d H:i:s'),
Expand Down Expand Up @@ -359,7 +360,7 @@ private function checkAliveDaemons(): void
private function isDaemonStillRunning(Daemon $daemon): bool
{
// Get the running processes with the Daemon's PID
exec(\Safe\sprintf('ps -ef | grep %s', $daemon->getPid()), $lines);
exec(sprintf('ps -ef | grep %s', $daemon->getPid()), $lines);

// Search the line with this command name: this indicates the process is still running
foreach ($lines as $line) {
Expand All @@ -383,7 +384,7 @@ private function processRunningJobs(string $queueName): void
{
$currentlyRunningProgress = null;
if ($this->ioWriter->getVerbosity() >= OutputInterface::VERBOSITY_VERY_VERBOSE) {
$this->ioWriter->infoLineNoBg(\Safe\sprintf(
$this->ioWriter->infoLineNoBg(sprintf(
'Checking <success-nobg>%s</success-nobg> running jobs on queue "%s"...',
$this->daemon->countRunningJobs($queueName), $queueName
));
Expand Down
6 changes: 4 additions & 2 deletions src/Command/TestFakeCommand.php
Expand Up @@ -20,6 +20,8 @@
use Exception;
use InvalidArgumentException;
use RuntimeException;
use function Safe\sleep;
use function Safe\sprintf;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand Down Expand Up @@ -86,8 +88,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int

// Emulate a duration to execute the command
$rand = random_int(0, 10);
$output->writeln(\Safe\sprintf("I'm so tired... I will sleep for %s seconds... Good bye, see you soon! :)", $rand));
\Safe\sleep($rand);
$output->writeln(sprintf("I'm so tired... I will sleep for %s seconds... Good bye, see you soon! :)", $rand));
sleep($rand);

// Ok, all gone well (fingers crossed? :P )
$output->writeln('Hello! I just woke up! :) ... Finito.');
Expand Down
5 changes: 3 additions & 2 deletions src/Command/TestRandomJobsCommand.php
Expand Up @@ -22,6 +22,7 @@
use Exception;
use Safe\Exceptions\ArrayException;
use Safe\Exceptions\StringsException;
use function Safe\sprintf;
use SerendipityHQ\Bundle\CommandsQueuesBundle\Entity\Job;
use SerendipityHQ\Bundle\CommandsQueuesBundle\Util\JobsMarker;
use SerendipityHQ\Bundle\CommandsQueuesBundle\Util\ProgressBarFactory;
Expand Down Expand Up @@ -119,7 +120,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

$this->getIoWriter()->title('SerendipityHQ Queue Bundle Daemon');
$this->getIoWriter()->info(\Safe\sprintf('Starting generating %s random jobs...', $howManyJobs));
$this->getIoWriter()->info(sprintf('Starting generating %s random jobs...', $howManyJobs));

// Generate the random jobs
$progress = ProgressBarFactory::createProgressBar(ProgressBarFactory::FORMAT_CREATE_JOBS, $output, $howManyJobs);
Expand Down Expand Up @@ -182,7 +183,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$progress->finish();

$this->getIoWriter()->write("\n\n");
$this->getIoWriter()->success(\Safe\sprintf('All done: %s random jobs generated!', $howManyJobs));
$this->getIoWriter()->success(sprintf('All done: %s random jobs generated!', $howManyJobs));

return 0;
}
Expand Down
10 changes: 6 additions & 4 deletions src/DependencyInjection/Configuration.php
Expand Up @@ -19,6 +19,8 @@

use Safe\Exceptions\ArrayException;
use Safe\Exceptions\StringsException;
use function Safe\ksort;
use function Safe\sprintf;
use SerendipityHQ\Bundle\CommandsQueuesBundle\Entity\Daemon;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
Expand Down Expand Up @@ -274,23 +276,23 @@ public function getConfigTreeBuilder(): TreeBuilder
private function validateConfiguration(array $tree): bool
{
if (1 > $tree[self::DAEMON_ALIVE_DAEMONS_CHECK_INTERVAL_KEY]) {
throw new InvalidConfigurationException(\Safe\sprintf(
throw new InvalidConfigurationException(sprintf(
'The global "%s" config param MUST be greater than 0. You set it to "%s".', self::DAEMON_ALIVE_DAEMONS_CHECK_INTERVAL_KEY, $tree[self::DAEMON_ALIVE_DAEMONS_CHECK_INTERVAL_KEY]
));
}

foreach ($tree['daemons'] as $daemon => $config) {
// A Daemon MUST HAVE at least one queue assigned
if (empty($config['queues'])) {
throw new InvalidConfigurationException(\Safe\sprintf(
throw new InvalidConfigurationException(sprintf(
'The "%s" daemon MUST specify at least one queue to process.', $daemon
));
}

// Check the queue is not already assigned
foreach ($config['queues'] as $queue) {
if (array_key_exists($queue, $this->foundQueues)) {
throw new InvalidConfigurationException(\Safe\sprintf(
throw new InvalidConfigurationException(sprintf(
'Queue "%s" already assigned to daemon "%s". You cannot assign this queue also to daemon "%s".',
$queue, $this->foundQueues[$queue], $daemon
));
Expand Down Expand Up @@ -338,7 +340,7 @@ private function prepareConfiguration(array $tree): array
}

// Sort queues alphabetically
\Safe\ksort($returnConfig['queues']);
ksort($returnConfig['queues']);

// Now configure the queues
foreach ($returnConfig['queues'] as $queue => $config) {
Expand Down

0 comments on commit e14f1c3

Please sign in to comment.