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

ISSUE-244 cron-job package #246

Merged
merged 21 commits into from
Apr 21, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
1 change: 1 addition & 0 deletions .github/workflows/after_splitting_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ jobs:
- aws-tool-kit
- console
- core
- cron-job
- doctrine-extra
- entity-migrator
- fixer
Expand Down
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
"draw/console": "self.version",
"draw/contracts": "self.version",
"draw/core": "self.version",
"draw/cron-job": "self.version",
"draw/doctrine-extra": "self.version",
"draw/entity-migrator": "self.version",
"draw/fixer": "self.version",
Expand Down Expand Up @@ -172,6 +173,7 @@
"Draw\\Component\\AwsToolKit\\": "packages/aws-tool-kit/",
"Draw\\Component\\Console\\": "packages/console/",
"Draw\\Component\\Core\\": "packages/core/",
"Draw\\Component\\CronJob\\": "packages/cron-job/",
"Draw\\Component\\EntityMigrator\\": "packages/entity-migrator/",
"Draw\\Component\\Log\\": "packages/log/",
"Draw\\Component\\Mailer\\": "packages/mailer/",
Expand Down
8 changes: 8 additions & 0 deletions packages/cron-job/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/vendor/
/.idea/
composer.lock

###> phpunit/phpunit ###
/phpunit.xml
.phpunit.result.cache
###< phpunit/phpunit ###
56 changes: 56 additions & 0 deletions packages/cron-job/Command/QueueCronJobByNameCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

namespace Draw\Component\CronJob\Command;

use Draw\Component\CronJob\CronJobProcessor;
use Draw\Component\CronJob\Entity\CronJob;
use Symfony\Bridge\Doctrine\ManagerRegistry;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

#[AsCommand(name: 'draw:cron-job:queue-by-name')]
class QueueCronJobByNameCommand extends Command
{
public function __construct(
private ManagerRegistry $managerRegistry,
private CronJobProcessor $cronJobProcessor,
) {
parent::__construct();
}

protected function configure(): void
{
$this
->setDescription('Queues cron job by name')
->addArgument('name', InputArgument::REQUIRED, 'Cron job name');
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);

$cronJob = $this->managerRegistry
->getRepository(CronJob::class)
->findOneBy(['name' => $input->getArgument('name')]);

if (null === $cronJob) {
$io->error('Cron job could not be found.');

return Command::FAILURE;
}

$io->section('Queueing cron job...');

$this->cronJobProcessor->queue($cronJob, true);

$io->section('Cron job successfully queued.');

return Command::SUCCESS;
}
}
61 changes: 61 additions & 0 deletions packages/cron-job/Command/QueueDueCronJobsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

namespace Draw\Component\CronJob\Command;

use Doctrine\Persistence\ManagerRegistry;
use Draw\Component\CronJob\CronJobProcessor;
use Draw\Component\CronJob\Entity\CronJob;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

#[AsCommand(name: 'draw:cron-job:queue-due')]
class QueueDueCronJobsCommand extends Command
{
public function __construct(
private ManagerRegistry $managerRegistry,
private CronJobProcessor $cronJobProcessor,
) {
parent::__construct();
}

protected function configure(): void
{
$this
->setDescription('Queues due cron jobs');
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$io->section('Queueing cron jobs...');

$cronJobs = array_map(
static fn(CronJob $cronJob): bool => $cronJob->isDue(),
mpoiriert marked this conversation as resolved.
Show resolved Hide resolved
$this->managerRegistry
->getRepository(CronJob::class)
->findAll()
);

$progress = $io->createProgressBar(\count($cronJobs));
$progress->setFormat(ProgressBar::FORMAT_DEBUG);

foreach ($cronJobs as $cronJob) {
$this->cronJobProcessor->queue($cronJob);

Check failure on line 49 in packages/cron-job/Command/QueueDueCronJobsCommand.php

View workflow job for this annotation

GitHub Actions / PHPStan

Parameter #1 $cronJob of method Draw\Component\CronJob\CronJobProcessor::queue() expects Draw\Component\CronJob\Entity\CronJob, bool given.

$progress->advance();
}

$progress->finish();

$io->newLine(2);
$io->success('Cron jobs successfully queued...');

return Command::SUCCESS;
}
}
19 changes: 19 additions & 0 deletions packages/cron-job/CronJobExecutionFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Draw\Component\CronJob;

use Draw\Component\CronJob\Entity\CronJob;
use Draw\Component\CronJob\Entity\CronJobExecution;

class CronJobExecutionFactory
{
public function create(CronJob $cronJob, bool $force = false): CronJobExecution
mpoiriert marked this conversation as resolved.
Show resolved Hide resolved
{
return (new CronJobExecution())
->setCronJob($cronJob)
->setRequestedAt(new \DateTimeImmutable())
->setForce($force);
}
}
55 changes: 55 additions & 0 deletions packages/cron-job/CronJobProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace Draw\Component\CronJob;

use Doctrine\Persistence\ManagerRegistry;
use Draw\Component\CronJob\Entity\CronJob;
use Draw\Component\CronJob\Entity\CronJobExecution;
use Draw\Component\CronJob\Message\ExecuteCronJobMessage;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Process\Process;

class CronJobProcessor
{
public function __construct(
private CronJobExecutionFactory $cronJobExecutionFactory,
private ManagerRegistry $managerRegistry,
private MessageBusInterface $messageBus,
) {
}

public function queue(CronJob $cronJob, bool $force = false): void
{
$manager = $this->managerRegistry->getManagerForClass(CronJobExecution::class);

$manager->persist($execution = $this->cronJobExecutionFactory->create($cronJob, $force));
$manager->flush();

$this->messageBus->dispatch(new ExecuteCronJobMessage($execution));
}

public function process(CronJobExecution $execution): void
{
$manager = $this->managerRegistry->getManagerForClass(CronJobExecution::class);

$execution->start();
$manager->flush();

$command = $execution->getCronJob()->getCommand();

$process = Process::fromShellCommandline($command)
mpoiriert marked this conversation as resolved.
Show resolved Hide resolved
->setTimeout(1800);

try {
$process->run();
mpoiriert marked this conversation as resolved.
Show resolved Hide resolved

$execution->end($process->getExitCode());
mpoiriert marked this conversation as resolved.
Show resolved Hide resolved
} catch (\Throwable $error) {
$execution->fail($process->getExitCode(), ['TODO']);
} finally {
$manager->flush();
}
}
}
Loading
Loading