Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Featue/1.0/qa #12

Open
wants to merge 2 commits into
base: 1.0-dev
Choose a base branch
from
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
21 changes: 21 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# This file is for unifying the coding style for different editors and IDEs
# editorconfig.org

root = true

[*]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false

[*.markdown]
trim_trailing_whitespace = false

[Makefile]
indent_style = tab
2 changes: 2 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@ jobs:
run: composer install --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
- name: Check Code Styles
run: vendor/bin/ecs
- name: PhpStan
run: vendor/bin/phpstan analyze
- name: Execute tests (Unit and Feature tests) via PHPUnit
run: vendor/bin/simple-phpunit
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,18 @@ help:
install:
composer update

test:
make ecs
make phpstan
make phpunit

## Fix PHP Styles
ecs:
vendor/bin/ecs --fix

phpstan:
vendor/bin/phpstan

## PHP Unit
phpunit:
vendor/bin/simple-phpunit
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"doctrine/orm": "^2.11",
"doctrine/annotations": "^1.13.2",
"nyholm/symfony-bundle-test": "^1.8",
"phpstan/phpstan": "^1.8",
"symfony/phpunit-bridge": "~6.0.0",
"symfony/http-kernel": "~6.0.0",
"symfony/config": "~6.0.0",
Expand Down
8 changes: 8 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
parameters:
level: 6
paths:
- src
- tests
parallel:
maximumNumberOfProcesses: 2
checkGenericClassInNonGenericObjectType: false
9 changes: 2 additions & 7 deletions src/Command/CleanupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ protected function configure(): void
$this
->addOption('max-retention', null, InputOption::VALUE_REQUIRED, 'The maximum retention time (will be parsed by DateTime).', '1 month')
->addOption('max-retention-successful', null, InputOption::VALUE_REQUIRED, 'The maximum retention time for succeeded jobs (will be parsed by DateTime).', '7 days')
->addOption('per-call', null, InputOption::VALUE_REQUIRED, 'The maximum number of jobs to clean-up per call.', 1000)
;
}

Expand All @@ -59,11 +58,8 @@ protected function configure(): void
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$perCall = (int) $input->getOption('per-call');

$deletedSuccessful = $this->executionRepository->deleteSuccessfulJobs(
new \DateTimeImmutable('-' . $input->getOption('max-retention-successful')),
$perCall
new \DateTimeImmutable('-' . $input->getOption('max-retention-successful'))
);

$output->writeln(sprintf(
Expand All @@ -72,8 +68,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
));

$deletedNotSuccessful = $this->executionRepository->deleteNotSuccessfulJobs(
new \DateTimeImmutable('-' . $input->getOption('max-retention')),
$perCall
new \DateTimeImmutable('-' . $input->getOption('max-retention'))
);

$output->writeln(sprintf(
Expand Down
101 changes: 3 additions & 98 deletions src/Command/ExecuteCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,15 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Process\Process;
use whatwedo\CronBundle\CronJob\CronInterface;
use whatwedo\CronBundle\CronJob\CronJobInterface;
use whatwedo\CronBundle\Entity\Execution;
use whatwedo\CronBundle\Event\CronErrorEvent;
use whatwedo\CronBundle\Event\CronFinishEvent;
use whatwedo\CronBundle\Event\CronStartEvent;
use whatwedo\CronBundle\Exception\MaxRuntimeReachedException;
use whatwedo\CronBundle\Manager\CronJobManager;
use whatwedo\CronBundle\Manager\ExecutionManager;

#[AsCommand(name: 'whatwedo:cron:execute', description: 'Execute cron job')]
class ExecuteCommand extends Command
{
public function __construct(
protected CronJobManager $cronJobManager,
protected ExecutionManager $executionManager,
protected EntityManagerInterface $entityManager,
protected EventDispatcherInterface $eventDispatcher,
protected string $projectDir,
Expand All @@ -59,24 +53,6 @@ public function __construct(
parent::__construct();
}

public function checkMaxRuntime(Execution $execution, CronInterface $cronJob, Process $process): void
{
if (! $cronJob->getMaxRuntime()) {
return;
}
$now = new \DateTime();
$diff = $now->getTimestamp() - $execution->getStartedAt()->getTimestamp();
if ($diff > $cronJob->getMaxRuntime()) {
$execution
->setState(Execution::STATE_TERMINATED)
->setPid(null)
->setStdout($process->getOutput())
->setStderr($process->getErrorOutput());
$this->entityManager->flush($execution);
throw new MaxRuntimeReachedException($execution);
}
}

protected function configure(): void
{
$this
Expand All @@ -88,79 +64,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
// Get job definition
$cronJob = $this->cronJobManager->getCronJob($input->getArgument('cron_job'));

// Build command to execute
$command = array_merge(['bin/console', $this->getCronCommand($cronJob), '--env=' . $this->environment], $this->getCronArguments($cronJob));

// Create execution
$execution = new Execution();
$execution->setJob($cronJob::class)
->setCommand($command);
$this->entityManager->persist($execution);
$this->entityManager->flush($execution);

// Run command
$process = new Process($command, $this->projectDir);
$process->start();
$execution->setPid($process->getPid());
$this->entityManager->flush($execution);
$this->eventDispatcher->dispatch(new CronStartEvent($cronJob), CronStartEvent::NAME);

// Update command output every 5 seconds
while ($process->isRunning()) {
$this->checkMaxRuntime($execution, $cronJob, $process);
$output->writeln(sprintf('Process %s is running...', $process->getCommandLine()));
sleep(5);
$execution->setStdout($process->getOutput())
->setStderr($process->getErrorOutput());
$this->entityManager->flush($execution);
}

if (! $process->isSuccessful()) {
$this->eventDispatcher->dispatch(new CronErrorEvent($cronJob, $process->getErrorOutput()), CronErrorEvent::NAME);
}

// Finish execution
$output->writeln('Execution finished with exit code ' . $process->getExitCode());
$execution
->setState(Execution::STATE_FINISHED)
->setFinishedAt(new \DateTime())
->setPid(null)
->setStdout($process->getOutput())
->setStderr($process->getErrorOutput())
->setExitCode($process->getExitCode());

if ($execution->getExitCode() !== 0) {
$execution->setState(Execution::STATE_ERROR);
}

$this->entityManager->flush($execution);
$this->eventDispatcher->dispatch(new CronFinishEvent($cronJob), CronFinishEvent::NAME);
$this->executionManager->execute($cronJob, $output);

return Command::SUCCESS;
}

protected function getCronCommand(CronInterface $cron): string
{
if ($cron instanceof CronJobInterface) {
return $cron->getCommand();
}

if ($cron instanceof Command) {
return $cron->getDefaultName();
}

return '';
}

/**
* @return string[]
*/
protected function getCronArguments(CronInterface $cron): array
{
if ($cron instanceof CronJobInterface) {
return $cron->getArguments();
}

return [];
}
}
2 changes: 1 addition & 1 deletion src/Command/InfoCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ protected function getNextExecutionDateString(CronInterface $cronJob): ?string
return $this->getFormattedDate($nextExecutionDate);
}

protected function getFormattedDate(\DateTime $date): ?string
protected function getFormattedDate(?\DateTime $date): ?string
{
if (! $date) {
return null;
Expand Down
5 changes: 1 addition & 4 deletions src/Controller/CronJobController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use whatwedo\CronBundle\Entity\Execution;
use whatwedo\CronBundle\Manager\CronJobManager;
use whatwedo\CronBundle\Manager\ExecutionManager;
use whatwedo\CronBundle\Model\CronJobActivable;
use whatwedo\CronBundle\Repository\ExecutionRepository;

class CronJobController extends AbstractController
Expand Down Expand Up @@ -139,10 +140,6 @@ public function clean(string $class, string $state): Response
{
$cronJob = $this->cronJobManager->getCronJob($class);

if (! $cronJob) {
throw new \Exception('Job not found');
}

$this->executionRepository->deleteExecutions($cronJob, $state);

$this->addFlash('success', 'cronjob.cleaned');
Expand Down
9 changes: 9 additions & 0 deletions src/CronJob/CronJobActivable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace whatwedo\CronBundle\Model;

class CronJobActivable
{
}
5 changes: 4 additions & 1 deletion src/DependencyInjection/whatwedoCronExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@

class whatwedoCronExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
/**
* @param string[] $configs
*/
public function load(array $configs, ContainerBuilder $container): void
{
// Auto tag cron jobs
$container->registerForAutoconfiguration(CronInterface::class)->addTag('whatwedo.cron.job');
Expand Down
3 changes: 2 additions & 1 deletion src/Entity/Execution.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class Execution
protected string $job;

/**
* @var string[]
* @ORM\Column(type="json", nullable=false)
*/
protected ?array $command = [];
Expand Down Expand Up @@ -265,6 +266,6 @@ public function setCronJob(?CronInterface $cronJob): self

public function __toString(): string
{
return '#' . str_pad($this->getId(), 6, '0', STR_PAD_LEFT);
return '#' . str_pad((string) $this->getId(), 6, '0', STR_PAD_LEFT);
}
}
12 changes: 6 additions & 6 deletions src/Manager/CronJobManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\HttpKernel\KernelInterface;
use whatwedo\CronBundle\CronJob\CronInterface;
use whatwedo\CronBundle\CronJob\CronJobInterface;
use whatwedo\CronBundle\Exception\CronJobNotFoundException;

class CronJobManager
Expand All @@ -43,7 +43,7 @@ class CronJobManager
protected $consoleApplication;

/**
* @var CronInterface[]
* @var CronJobInterface[]
*/
protected $cronJobs = [];

Expand All @@ -52,22 +52,22 @@ public function __construct(KernelInterface $kernel)
$this->consoleApplication = new Application($kernel);
}

public function addCronJob(CronInterface $cronJob): self
public function addCronJob(CronJobInterface $cronJob): self
{
$this->cronJobs[$cronJob::class] = $cronJob;

return $this;
}

/**
* @return CronInterface[]
* @return CronJobInterface[]
*/
public function getCronJobs(): array
{
return $this->cronJobs;
}

public function getCronJob(string $class): CronInterface
public function getCronJob(string $class): CronJobInterface
{
foreach ($this->cronJobs as $cronJob) {
if ($cronJob instanceof $class) {
Expand All @@ -77,7 +77,7 @@ public function getCronJob(string $class): CronInterface
throw new CronJobNotFoundException($class);
}

public function getCommandByCronJob(CronInterface $cronJob): Command
public function getCommandByCronJob(CronJobInterface $cronJob): Command
{
return $this->consoleApplication->find($cronJob->getCommand());
}
Expand Down
Loading