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 1 commit
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 ###
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', nullable: false, options: ['default' => false])]
private bool $active = false;

#[ORM\Column(name: 'command', type: 'string', length: 255, nullable: false)]
mpoiriert marked this conversation as resolved.
Show resolved Hide resolved
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;
mpoiriert marked this conversation as resolved.
Show resolved Hide resolved

#[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,
mpoiriert marked this conversation as resolved.
Show resolved Hide resolved
)
]
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;
mpoiriert marked this conversation as resolved.
Show resolved Hide resolved
}
}
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', nullable: false, 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;
}
}
21 changes: 21 additions & 0 deletions packages/cron-job/Event/PostCronJobExecutionEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Draw\Component\CronJob\Event;

use Draw\Component\CronJob\Entity\CronJobExecution;
use Symfony\Contracts\EventDispatcher\Event;

class PostCronJobExecutionEvent extends Event
{
public function __construct(
private CronJobExecution $execution,
) {
}

public function getExecution(): CronJobExecution
{
return $this->execution;
}
}
Loading
Loading