mmucklo /
DtcQueueBundle
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace Dtc\QueueBundle\Model; |
||||
| 4 | |||||
| 5 | use Dtc\QueueBundle\Manager\JobManagerInterface; |
||||
| 6 | use Dtc\QueueBundle\Util\Util; |
||||
| 7 | |||||
| 8 | abstract class Worker |
||||
| 9 | { |
||||
| 10 | public const RESULT_SUCCESS = 0; |
||||
| 11 | public const RESULT_FAILURE = 1; |
||||
| 12 | |||||
| 13 | /** @var JobManagerInterface */ |
||||
| 14 | private $jobManager; |
||||
| 15 | private $currentJob; |
||||
| 16 | |||||
| 17 | 6 | public function setCurrentJob(BaseJob $job) |
|||
| 18 | { |
||||
| 19 | 6 | $this->currentJob = $job; |
|||
| 20 | 6 | } |
|||
| 21 | |||||
| 22 | public function getCurrentJob() |
||||
| 23 | { |
||||
| 24 | return $this->currentJob; |
||||
| 25 | } |
||||
| 26 | |||||
| 27 | 31 | public function setJobManager(JobManagerInterface $jobManager) |
|||
| 28 | { |
||||
| 29 | 31 | $this->jobManager = $jobManager; |
|||
| 30 | 31 | } |
|||
| 31 | |||||
| 32 | /** |
||||
| 33 | * @return |
||||
| 34 | */ |
||||
| 35 | 111 | public function getJobManager() |
|||
| 36 | { |
||||
| 37 | 111 | return $this->jobManager; |
|||
| 38 | } |
||||
| 39 | |||||
| 40 | /** |
||||
| 41 | * @param int|null $time |
||||
| 42 | * @param bool $batch |
||||
| 43 | * @param int|null $priority |
||||
| 44 | * |
||||
| 45 | * @throws \Exception |
||||
| 46 | * @throws \InvalidArgumentException |
||||
| 47 | */ |
||||
| 48 | 25 | public function at($time = null, $batch = false, $priority = null) |
|||
| 49 | { |
||||
| 50 | 25 | $timeU = $time; |
|||
| 51 | 25 | if (null === $time) { |
|||
| 52 | 1 | $timeU = Util::getMicrotimeStr(); |
|||
| 53 | 1 | $dateTime = \DateTime::createFromFormat( |
|||
| 54 | 1 | 'U.u', |
|||
| 55 | 1 | $timeU |
|||
| 56 | ); |
||||
| 57 | 1 | if (!$dateTime) { |
|||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||||
| 58 | throw new \Exception("Could not create DateTime object from $timeU"); |
||||
| 59 | } |
||||
| 60 | 1 | $dateTime->setTimezone(new \DateTimeZone(date_default_timezone_get())); |
|||
| 61 | } else { |
||||
| 62 | 25 | $localeInfo = localeconv(); |
|||
| 63 | 25 | $decimalPoint = isset($localeInfo['decimal_point']) ? $localeInfo['decimal_point'] : '.'; |
|||
| 64 | 25 | $hasDecimalPoint = false !== strpos(strval($time), $decimalPoint); |
|||
| 65 | 25 | $hasEnDecimalPoint = '.' === $decimalPoint ? $hasDecimalPoint : strpos(strval($time), '.'); |
|||
| 66 | 25 | if (!$hasEnDecimalPoint) { |
|||
| 67 | 2 | if ($hasDecimalPoint) { |
|||
| 68 | $dateTime = \DateTime::createFromFormat( |
||||
| 69 | 'U'.$decimalPoint.'u', |
||||
| 70 | strval($timeU) |
||||
| 71 | ); |
||||
| 72 | if (!$dateTime) { |
||||
|
0 ignored issues
–
show
|
|||||
| 73 | throw new \InvalidArgumentException("Could not create DateTime object from $timeU"); |
||||
| 74 | } |
||||
| 75 | $dateTime->setTimezone(new \DateTimeZone(date_default_timezone_get())); |
||||
| 76 | } else { |
||||
| 77 | 2 | $dateTime = \DateTime::createFromFormat( |
|||
| 78 | 2 | 'U', |
|||
| 79 | 2 | strval($timeU) |
|||
| 80 | ); |
||||
| 81 | 2 | if (!$dateTime) { |
|||
|
0 ignored issues
–
show
|
|||||
| 82 | 1 | throw new \InvalidArgumentException("Could not create DateTime object from $timeU"); |
|||
| 83 | } |
||||
| 84 | 2 | $dateTime->setTimezone(new \DateTimeZone(date_default_timezone_get())); |
|||
| 85 | } |
||||
| 86 | } else { |
||||
| 87 | 23 | $dateTime = \DateTime::createFromFormat( |
|||
| 88 | 23 | 'U.u', |
|||
| 89 | 23 | strval($timeU) |
|||
| 90 | ); |
||||
| 91 | 23 | if (!$dateTime) { |
|||
|
0 ignored issues
–
show
|
|||||
| 92 | throw new \InvalidArgumentException("Could not create DateTime object from $timeU"); |
||||
| 93 | } |
||||
| 94 | 23 | $dateTime->setTimezone(new \DateTimeZone(date_default_timezone_get())); |
|||
| 95 | } |
||||
| 96 | } |
||||
| 97 | |||||
| 98 | 25 | if (!($dateTime instanceof \DateTime)) { |
|||
|
0 ignored issues
–
show
|
|||||
| 99 | throw new \InvalidArgumentException("Invalid time: $time".($timeU != $time ? " - (micro: $timeU)" : '')); |
||||
| 100 | } |
||||
| 101 | 25 | $jobClass = $this->jobManager->getJobClass(); |
|||
| 102 | |||||
| 103 | 25 | return new $jobClass($this, $batch, $priority, $dateTime); |
|||
| 104 | } |
||||
| 105 | |||||
| 106 | /** |
||||
| 107 | * @param int $delay Amount of time to delay |
||||
| 108 | * @param int|null $priority |
||||
| 109 | */ |
||||
| 110 | 22 | public function later($delay = 0, $priority = null) |
|||
| 111 | { |
||||
| 112 | 22 | return $this->batchOrLaterDelay($delay, false, $priority); |
|||
| 113 | } |
||||
| 114 | |||||
| 115 | 23 | public function batchOrLaterDelay($delay = 0, $batch = false, $priority = null) |
|||
| 116 | { |
||||
| 117 | 23 | $timing = Util::getMicrotimeStr(); |
|||
| 118 | 23 | $parts = explode('.', $timing); |
|||
| 119 | 23 | $parts[0] = strval(intval($parts[0]) + intval($delay)); |
|||
| 120 | 23 | $localeInfo = localeconv(); |
|||
| 121 | 23 | $decimalPoint = isset($localeInfo['decimal_point']) ? $localeInfo['decimal_point'] : '.'; |
|||
| 122 | |||||
| 123 | 23 | $job = $this->at(implode($decimalPoint, $parts), $batch, $priority); |
|||
|
0 ignored issues
–
show
implode($decimalPoint, $parts) of type string is incompatible with the type integer|null expected by parameter $time of Dtc\QueueBundle\Model\Worker::at().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 124 | 23 | $job->setDelay($delay); |
|||
| 125 | |||||
| 126 | 23 | return $job; |
|||
| 127 | } |
||||
| 128 | |||||
| 129 | /** |
||||
| 130 | * @param int $delay Amount of time to delay |
||||
| 131 | * @param int|null $priority |
||||
| 132 | */ |
||||
| 133 | 6 | public function batchLater($delay = 0, $priority = null) |
|||
| 134 | { |
||||
| 135 | 6 | return $this->batchOrLaterDelay($delay, true, $priority); |
|||
| 136 | } |
||||
| 137 | |||||
| 138 | /** |
||||
| 139 | * @param int|null $time |
||||
| 140 | * @param int|null $priority |
||||
| 141 | */ |
||||
| 142 | 1 | public function batchAt($time = null, $priority = null) |
|||
| 143 | { |
||||
| 144 | 1 | return $this->at($time, true, $priority); |
|||
| 145 | } |
||||
| 146 | |||||
| 147 | abstract public function getName(); |
||||
| 148 | } |
||||
| 149 |