Bez popisu

Supervisor.php 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace MailPoet\Cron;
  3. if (!defined('ABSPATH')) exit;
  4. class Supervisor {
  5. public $daemon;
  6. public $token;
  7. /** @var CronHelper */
  8. private $cronHelper;
  9. public function __construct(
  10. CronHelper $cronHelper
  11. ) {
  12. $this->cronHelper = $cronHelper;
  13. }
  14. public function init() {
  15. $this->token = $this->cronHelper->createToken();
  16. $this->daemon = $this->getDaemon();
  17. }
  18. public function checkDaemon() {
  19. $daemon = $this->daemon;
  20. $updatedAt = $daemon ? (int)$daemon['updated_at'] : 0;
  21. $executionTimeoutExceeded =
  22. (time() - $updatedAt) >= $this->cronHelper->getDaemonExecutionTimeout();
  23. $daemonIsInactive =
  24. isset($daemon['status']) && $daemon['status'] === CronHelper::DAEMON_STATUS_INACTIVE;
  25. if ($executionTimeoutExceeded || $daemonIsInactive) {
  26. $this->cronHelper->restartDaemon($this->token);
  27. return $this->runDaemon();
  28. }
  29. return $daemon;
  30. }
  31. public function runDaemon() {
  32. $this->cronHelper->accessDaemon($this->token);
  33. $daemon = $this->cronHelper->getDaemon();
  34. return $daemon;
  35. }
  36. public function getDaemon() {
  37. $daemon = $this->cronHelper->getDaemon();
  38. if (!$daemon) {
  39. $this->cronHelper->createDaemon($this->token);
  40. return $this->runDaemon();
  41. }
  42. return $daemon;
  43. }
  44. }