vendor/dukecity/command-scheduler-bundle/Entity/ScheduledCommand.php line 22

Open in your IDE?
  1. <?php
  2. namespace Dukecity\CommandSchedulerBundle\Entity;
  3. use Carbon\Carbon;
  4. use Cron\CronExpression as CronExpressionLib;
  5. use DateTime;
  6. use Doctrine\DBAL\Types\Types;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Dukecity\CommandSchedulerBundle\Repository\ScheduledCommandRepository;
  9. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  10. use Symfony\Component\Validator\Constraints as Assert;
  11. use Dukecity\CommandSchedulerBundle\Validator\Constraints as AssertDukecity;
  12. /**
  13. * https://www.doctrine-project.org/2021/05/24/orm2.9.html
  14. * @author Julien Guyon <julienguyon@hotmail.com>
  15. */
  16. #[ORM\Entity(repositoryClass: ScheduledCommandRepository::class)]
  17. #[ORM\Table(name: "scheduled_command")]
  18. #[UniqueEntity(fields: ["name"], groups: ['new'])]
  19. class ScheduledCommand
  20. {
  21. #[ORM\Id, ORM\Column(type: Types::INTEGER), ORM\GeneratedValue(strategy: 'AUTO')]
  22. private $id; # temporary, otherwise EasyAdminBundle could not create new entries
  23. #private ?int $id = null;
  24. // see https://www.doctrine-project.org/projects/doctrine-orm/en/2.9/reference/transactions-and-concurrency.html
  25. #[ORM\Version]
  26. #[ORM\Column(type: Types::INTEGER)]
  27. private int $version = 0;
  28. #[ORM\Column(name: "created_at", type: Types::DATETIME_MUTABLE, nullable: true)]
  29. private ?DateTime $createdAt = null;
  30. #[Assert\NotBlank]
  31. #[ORM\Column(type: Types::STRING, length: 150, unique: true, nullable: false)]
  32. private string $name;
  33. #[Assert\NotBlank]
  34. #[ORM\Column(type: Types::STRING, length: 200, nullable: false)]
  35. private string $command;
  36. #[ORM\Column(type: Types::TEXT, nullable: true)]
  37. private ?string $arguments = null;
  38. /**
  39. * @see http://www.abunchofutils.com/utils/developer/cron-expression-helper/
  40. */
  41. #[Assert\NotBlank]
  42. #[AssertDukecity\CronExpression]
  43. #[ORM\Column(type: Types::STRING, length: 200, nullable: true)]
  44. private string $cronExpression;
  45. #[Assert\Type(DateTime::class)]
  46. #[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
  47. private ?DateTime $lastExecution = null;
  48. #[ORM\Column(type: Types::INTEGER, nullable: true)]
  49. private ?int $lastReturnCode = null;
  50. /** Log's file name (without path). */
  51. #[ORM\Column(type: Types::STRING, length: 150, nullable: true)]
  52. private ?string $logFile = null;
  53. ##[Assert\Type(Integer::class)]
  54. #[ORM\Column(type: Types::INTEGER, nullable: false)]
  55. private int $priority;
  56. /** If true, command will be execute next time regardless cron expression. */
  57. #[ORM\Column(type: Types::BOOLEAN, nullable: false)]
  58. private bool $executeImmediately = false;
  59. #[ORM\Column(type: Types::BOOLEAN, nullable: false)]
  60. private bool $disabled = false;
  61. #[ORM\Column(type: Types::BOOLEAN, nullable: false)]
  62. private bool $locked = false;
  63. /**
  64. * Init new ScheduledCommand.
  65. */
  66. public function __construct()
  67. {
  68. $this->createdAt = new DateTime();
  69. #$this->setLastExecution(new DateTime());
  70. $this->lastExecution = null;
  71. $this->setLocked(false);
  72. $this->priority = 0;
  73. $this->version = 1;
  74. }
  75. public function __toString(): string
  76. {
  77. return $this->getName();
  78. }
  79. public function getId(): ?int
  80. {
  81. return $this->id;
  82. }
  83. public function setId(int $id): ScheduledCommand
  84. {
  85. $this->id = $id;
  86. return $this;
  87. }
  88. public function getName(): ?string
  89. {
  90. return $this->name;
  91. }
  92. public function setName(string $name): ScheduledCommand
  93. {
  94. $this->name = $name;
  95. return $this;
  96. }
  97. public function getCommand(): ?string
  98. {
  99. return $this->command;
  100. }
  101. public function setCommand(string $command): ScheduledCommand
  102. {
  103. $this->command = $command;
  104. return $this;
  105. }
  106. public function getArguments(): ?string
  107. {
  108. return $this->arguments;
  109. }
  110. public function setArguments(?string $arguments): ScheduledCommand
  111. {
  112. $this->arguments = $arguments;
  113. return $this;
  114. }
  115. public function getCronExpression(): ?string
  116. {
  117. return $this->cronExpression;
  118. }
  119. public function setCronExpression(string $cronExpression): ScheduledCommand
  120. {
  121. $this->cronExpression = $cronExpression;
  122. return $this;
  123. }
  124. public function getLastExecution(): ?DateTime
  125. {
  126. return $this->lastExecution;
  127. }
  128. public function setLastExecution(DateTime $lastExecution): ScheduledCommand
  129. {
  130. $this->lastExecution = $lastExecution;
  131. return $this;
  132. }
  133. public function getLogFile(): ?string
  134. {
  135. return $this->logFile;
  136. }
  137. public function setLogFile(?string $logFile): ScheduledCommand
  138. {
  139. $this->logFile = $logFile;
  140. return $this;
  141. }
  142. public function getLastReturnCode(): ?int
  143. {
  144. return $this->lastReturnCode;
  145. }
  146. public function setLastReturnCode(?int $lastReturnCode): ScheduledCommand
  147. {
  148. $this->lastReturnCode = $lastReturnCode;
  149. return $this;
  150. }
  151. public function getPriority(): int
  152. {
  153. return $this->priority;
  154. }
  155. public function setPriority(int $priority): ScheduledCommand
  156. {
  157. $this->priority = $priority;
  158. return $this;
  159. }
  160. public function isExecuteImmediately(): bool
  161. {
  162. return $this->executeImmediately;
  163. }
  164. public function getExecuteImmediately(): bool
  165. {
  166. return $this->executeImmediately;
  167. }
  168. public function setExecuteImmediately(bool $executeImmediately): ScheduledCommand
  169. {
  170. $this->executeImmediately = $executeImmediately;
  171. return $this;
  172. }
  173. public function isDisabled(): ?bool
  174. {
  175. return $this->disabled;
  176. }
  177. public function getDisabled(): ?bool
  178. {
  179. return $this->disabled;
  180. }
  181. public function setDisabled(bool $disabled): ScheduledCommand
  182. {
  183. $this->disabled = $disabled;
  184. return $this;
  185. }
  186. public function isLocked(): ?bool
  187. {
  188. return $this->locked;
  189. }
  190. public function getLocked(): ?bool
  191. {
  192. return $this->locked;
  193. }
  194. public function setLocked(bool $locked): ScheduledCommand
  195. {
  196. $this->locked = $locked;
  197. return $this;
  198. }
  199. public function getCreatedAt(): ?DateTime
  200. {
  201. return $this->createdAt;
  202. }
  203. public function setCreatedAt(DateTime $createdAt): ScheduledCommand
  204. {
  205. $this->createdAt = $createdAt;
  206. return $this;
  207. }
  208. /**
  209. * @throws \Exception
  210. */
  211. public function getNextRunDate(bool $checkExecuteImmediately = true): ?DateTime
  212. {
  213. if($this->getDisabled() || $this->getLocked())
  214. {return null;}
  215. if ($checkExecuteImmediately && $this->getExecuteImmediately()) {
  216. return new DateTime();
  217. }
  218. return (new CronExpressionLib($this->getCronExpression()))->getNextRunDate();
  219. }
  220. /**
  221. * Get a human readable format of the next run of the scheduled command
  222. */
  223. public function getNextRunDateForHumans(): ?string
  224. {
  225. try{
  226. if(!$this->getNextRunDate())
  227. {return null;}
  228. return Carbon::instance($this->getNextRunDate())->diffForHumans();
  229. }
  230. catch (\Exception)
  231. {}
  232. return null;
  233. }
  234. }