Skip to content

Commit

Permalink
[CronJob] New CronJob package (#246)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Martin Poirier Théorêt <[email protected]>
  • Loading branch information
DumitracheAdrian and mpoiriert authored Apr 21, 2024
1 parent d260f93 commit 8e7236c
Show file tree
Hide file tree
Showing 47 changed files with 2,456 additions and 54 deletions.
6 changes: 5 additions & 1 deletion .docker/php/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ RUN if [ $xdebug = "1" ] ; then \
RUN docker-php-ext-configure opcache --enable-opcache \
&& docker-php-ext-install opcache

#AMQP
RUN apt-get -y install --no-install-recommends gcc make autoconf libc-dev pkg-config librabbitmq-dev --fix-missing
RUN pecl install amqp-1.11.0 && docker-php-ext-enable amqp

#INTL
RUN apt-get update \
&& apt-get install -y --no-install-recommends libicu-dev
Expand Down Expand Up @@ -76,4 +80,4 @@ USER dev

WORKDIR /home/wwwroot/

EXPOSE 9000
EXPOSE 9000
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
60 changes: 60 additions & 0 deletions app/migrations/Version20240416163152.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20240416163152 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}

public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE TABLE cron_job__cron_job (
id INT AUTO_INCREMENT NOT NULL,
name VARCHAR(255) NOT NULL,
active TINYINT(1) DEFAULT 0 NOT NULL,
command LONGTEXT NOT NULL,
schedule VARCHAR(255) DEFAULT NULL,
time_to_live INT DEFAULT 0 NOT NULL,
priority INT DEFAULT NULL,
UNIQUE INDEX UNIQ_5D454BF65E237E06 (name),
PRIMARY KEY(id)
) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('CREATE TABLE cron_job__cron_job_execution (
id INT AUTO_INCREMENT NOT NULL,
cron_job_id INT NOT NULL,
requested_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\',
`force` TINYINT(1) DEFAULT 0 NOT NULL,
execution_started_at DATETIME DEFAULT NULL COMMENT \'(DC2Type:datetime_immutable)\',
execution_ended_at DATETIME DEFAULT NULL COMMENT \'(DC2Type:datetime_immutable)\',
execution_delay INT DEFAULT NULL,
exit_code INT DEFAULT NULL,
error JSON DEFAULT NULL,
INDEX IDX_2DD653DD79099ED8 (cron_job_id),
PRIMARY KEY(id)
) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('ALTER TABLE
cron_job__cron_job_execution
ADD
CONSTRAINT FK_2DD653DD79099ED8 FOREIGN KEY (cron_job_id) REFERENCES cron_job__cron_job (id) ON DELETE CASCADE');
}

public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE cron_job__cron_job_execution DROP FOREIGN KEY FK_2DD653DD79099ED8');
$this->addSql('DROP TABLE cron_job__cron_job');
$this->addSql('DROP TABLE cron_job__cron_job_execution');
}
}
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"doctrine/doctrine-bundle": "^2.5",
"doctrine/orm": "^2.11",
"doctrine/persistence": "^3.2",
"dragonmantank/cron-expression": "^3.3",
"guzzlehttp/psr7": "^1.8 || ^2.0",
"jms/serializer": "^3.29.1",
"jms/serializer-bundle": "^5.4.0",
Expand All @@ -29,6 +30,7 @@
"sonata-project/admin-bundle": "^4.8",
"sonata-project/doctrine-orm-admin-bundle": "^4.2",
"sonata-project/form-extensions": "^1.13",
"symfony/amqp-messenger": "^6.4.0",
"symfony/browser-kit": "^6.4.0",
"symfony/config": "^6.4.0",
"symfony/console": "^6.4.0",
Expand Down Expand Up @@ -126,6 +128,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 @@ -174,6 +177,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 Expand Up @@ -288,4 +292,4 @@
"vendor/bin/phpstan analyse --generate-baseline"
]
}
}
}
132 changes: 131 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions config/packages/draw_framework_extra.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ draw_framework_extra:
filter: 'in'
command_names: [ 'draw:console:generate-documentation' ]

cron_job: ~

doctrine_extra: ~

entity_migrator: ~
Expand Down
2 changes: 2 additions & 0 deletions config/packages/draw_sonata_integration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ draw_sonata_integration:
commandName: 'app:null'
label: 'Null'

cron_job: ~

entity_migrator: ~

messenger:
Expand Down
1 change: 1 addition & 0 deletions config/packages/messenger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ framework:
sync: 'sync://'

routing:
Draw\Component\CronJob\Message\ExecuteCronJobMessage: 'async_high_priority'
App\Message\NewTestDocumentMessage: ['sync', 'async']
Draw\Component\Messenger\ManualTrigger\Message\ManuallyTriggeredInterface: 'async'
Draw\Bundle\UserBundle\Message\NewUserLockMessage: 'sync'
Expand Down
8 changes: 8 additions & 0 deletions packages/contracts/Process/ProcessFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,12 @@
interface ProcessFactoryInterface
{
public function create(array $command, ?string $cwd = null, ?array $env = null, $input = null, ?float $timeout = 60): Process;

public function createFromShellCommandLine(
string $command,
?string $cwd = null,
?array $env = null,
$input = null,
?float $timeout = 60
): Process;
}
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 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\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;
}
}
Loading

0 comments on commit 8e7236c

Please sign in to comment.