Skip to content

Commit

Permalink
cron-job package
Browse files Browse the repository at this point in the history
  • Loading branch information
DumitracheAdrian committed Apr 12, 2024
1 parent 4b178ad commit c99360c
Show file tree
Hide file tree
Showing 12 changed files with 522 additions and 0 deletions.
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 ###
169 changes: 169 additions & 0 deletions packages/cron-job/Entity/CronJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
<?php

declare(strict_types=1);

namespace Draw\Component\CronJob\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

#[
ORM\Entity,
ORM\Table(name: 'cron_job__cron_job'),
]
class CronJob implements \Stringable
{
#[
ORM\Id,
ORM\GeneratedValue,
ORM\Column(name: 'id', type: 'int'),
]
private ?int $id = null;

#[ORM\Column(name: 'name', type: 'string', length: 255, unique: true, nullable: false)]
private ?string $name = null;

#[ORM\Column(name: 'active', type: 'boolean', options: ['default' => false])]
private bool $active = false;

#[ORM\Column(name: 'command', type: 'string', length: 255, nullable: false)]
private ?string $command = null;

#[ORM\Column(name: 'schedule', type: 'string', length: 255, nullable: true)]
private ?string $schedule = null;

#[ORM\Column(name: 'ttl', type: 'int', nullable: false, options: ['default' => 0])]
private int $ttl = 0;

#[ORM\Column(name: 'priority', type: 'int', nullable: true)]
private ?int $priority = null;

/**
* @var Collection<CronJobExecution>
*/
#[
ORM\OneToMany(
mappedBy: 'cronJob',
targetEntity: CronJobExecution::class,
cascade: ['persist'],
orphanRemoval: true,
)
]
private Collection $executions;

public function __construct()
{
$this->executions = new ArrayCollection();
}

public function getId(): ?int
{
return $this->id;
}

public function getName(): ?string
{
return $this->name;
}

public function setName(?string $name): self
{
$this->name = $name;

return $this;
}

public function isActive(): bool
{
return $this->active;
}

public function setActive(bool $active): self
{
$this->active = $active;

return $this;
}

public function getCommand(): ?string
{
return $this->command;
}

public function setCommand(?string $command): self
{
$this->command = $command;

return $this;
}

public function getSchedule(): ?string
{
return $this->schedule;
}

public function setSchedule(?string $schedule): self
{
$this->schedule = $schedule;

return $this;
}

public function getTtl(): int
{
return $this->ttl;
}

public function setTtl(int $ttl): self
{
$this->ttl = $ttl;

return $this;
}

public function getPriority(): ?int
{
return $this->priority;
}

public function setPriority(?int $priority): self
{
$this->priority = $priority;

return $this;
}

/**
* @return Collection<CronJobExecution>
*/
public function getExecutions(): Collection
{
return $this->executions;
}

public function addExecution(CronJobExecution $execution): self
{
if (!$this->executions->contains($execution)) {
$this->executions->add($execution);
$execution->setCronJob($this);
}

return $this;
}

public function removeExecution(CronJobExecution $execution): self
{
if ($this->executions->contains($execution)) {
$this->executions->removeElement($execution);
$execution->setCronJob(null);
}

return $this;
}

public function __toString(): string
{
return $this->name;
}
}
157 changes: 157 additions & 0 deletions packages/cron-job/Entity/CronJobExecution.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<?php

declare(strict_types=1);

namespace Draw\Component\CronJob\Entity;

use Doctrine\ORM\Mapping as ORM;

#[
ORM\Entity,
ORM\Table(name: 'cron_job__cron_job_execution'),
]
class CronJobExecution
{
#[
ORM\Id,
ORM\GeneratedValue,
ORM\Column(name: 'id', type: 'integer'),
]
private ?int $id = null;

#[ORM\Column(name: 'requested_at', type: 'datetime_immutable', nullable: false)]
private ?\DateTimeImmutable $requestedAt = null;

#[ORM\Column(name: 'force', type: 'boolean', options: ['default' => false])]
private bool $force = false;

#[ORM\Column(name: 'execution_started_at', type: 'datetime_immutable', nullable: true)]
private ?\DateTimeImmutable $executionStartedAt = null;

#[ORM\Column(name: 'execution_ended_at', type: 'datetime_immutable', nullable: true)]
private ?\DateTimeImmutable $executionEndedAt = null;

#[ORM\Column(name: 'execution_delay', type: 'int', nullable: true)]
private ?int $executionDelay = null;

#[ORM\Column(name: 'exit_code', type: 'int', nullable: true)]
private ?int $exitCode = null;

#[ORM\Column(name: 'error', type: 'json', nullable: true)]
private ?array $error = null;

#[
ORM\ManyToOne(
targetEntity: CronJob::class,
inversedBy: 'executions',
),
ORM\JoinColumn(
name: 'cron_job_id',
referencedColumnName: 'id',
nullable: false,
onDelete: 'CASCADE',
)
]
private ?CronJob $cronJob = null;

public function getId(): ?int
{
return $this->id;
}

public function getRequestedAt(): ?\DateTimeImmutable
{
return $this->requestedAt;
}

public function setRequestedAt(?\DateTimeImmutable $requestedAt): self
{
$this->requestedAt = $requestedAt;

return $this;
}

public function isForce(): bool
{
return $this->force;
}

public function setForce(bool $force): self
{
$this->force = $force;

return $this;
}

public function getExecutionStartedAt(): ?\DateTimeImmutable
{
return $this->executionStartedAt;
}

public function setExecutionStartedAt(?\DateTimeImmutable $executionStartedAt): self
{
$this->executionStartedAt = $executionStartedAt;

return $this;
}

public function getExecutionEndedAt(): ?\DateTimeImmutable
{
return $this->executionEndedAt;
}

public function setExecutionEndedAt(?\DateTimeImmutable $executionEndedAt): self
{
$this->executionEndedAt = $executionEndedAt;

return $this;
}

public function getExecutionDelay(): ?int
{
return $this->executionDelay;
}

public function setExecutionDelay(?int $executionDelay): self
{
$this->executionDelay = $executionDelay;

return $this;
}

public function getExitCode(): ?int
{
return $this->exitCode;
}

public function setExitCode(?int $exitCode): self
{
$this->exitCode = $exitCode;

return $this;
}

public function getError(): ?array
{
return $this->error;
}

public function setError(?array $error): self
{
$this->error = $error;

return $this;
}

public function getCronJob(): ?CronJob
{
return $this->cronJob;
}

public function setCronJob(?CronJob $cronJob): self
{
$this->cronJob = $cronJob;

return $this;
}
}
33 changes: 33 additions & 0 deletions packages/cron-job/Message/ExecuteCronJobMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Draw\Component\CronJob\Message;

use Draw\Component\CronJob\Entity\CronJob;
use Draw\Component\Messenger\DoctrineEnvelopeEntityReference\Message\DoctrineReferenceAwareInterface;
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;

class ExecuteCronJobMessage implements DoctrineReferenceAwareInterface
{
private ?CronJob $cronJob;

public function __construct(CronJob $cronJob)
{
$this->cronJob = $cronJob;
}

public function getCronJob(): ?CronJob
{
if (null === $this->cronJob) {
throw new UnrecoverableMessageHandlingException('CronJob is not set.');
}

return $this->cronJob;
}

public function getPropertiesWithDoctrineObject(): array
{
return ['cronJob'];
}
}
Loading

0 comments on commit c99360c

Please sign in to comment.