helper = $helper; $this->scheduleFactory = $scheduleFactory; $this->scheduleCollectionFactory = $scheduleCollectionFactory; $this->logger = $helper->getLogger(); } /** * get job code */ abstract public function getJobCode(); /** * get queue manager */ abstract protected function getQueueManager(); /** * Execute the cron. * * @return void */ public function fetchAndEnqueue() { // $this->logger->info("[$this->JOB_CODE] fetchAndEnqueue"); if (!$this->helper->getNetSuiteIsEnabled() || $this->isRunningCurrently(self::FETCH)) { $this->processed = 0; return; } $this->getQueueManager()->fetchAndEnqueue(); } /** * Execute the cron. * * @return void */ public function fetchAllAndEnqueue() { // $this->logger->info("[$this->JOB_CODE] fetchAllAndEnqueue"); if (!$this->helper->getNetSuiteIsEnabled() || $this->isRunningCurrently(self::FETCH)) { $this->processed = 0; return; } $this->getQueueManager()->fetchAndEnqueue(true); } /** * Schedule the cron. * * @param string $time * @return bool */ public function scheduleCronJob($type = self::PROCESS, $time = '+1 minute') { $currentDateTime = (new \DateTime())->format('Y-m-d H:i:s'); $scheduledDateTime = (new \DateTime())->modify($time)->format('Y-m-d H:i:00'); return $this->scheduleFactory->create() ->setJobCode($this->getJobCode() . '_' . $type) ->setStatus(Schedule::STATUS_PENDING) ->setCreatedAt($currentDateTime) ->setScheduledAt($scheduledDateTime) ->save(); } // Optionally override in child protected function getCustomBatchSize(): ?int { return null; // base class doesn't define custom size } /** * Process the queue items. * * @return int */ /* public function processQueueItems() { if (!$this->helper->getNetSuiteIsEnabled() || $this->isRunningCurrently(self::PROCESS)) { $this->processed = 0; return 0; } $batchSize = $this->helper->getCronBatchSize() ?: 50; return $this->getQueueManager()->processBatch($batchSize); } */ public function processQueueItems() { if (!$this->helper->getNetSuiteIsEnabled() || $this->isRunningCurrently(self::PROCESS)) { $this->processed = 0; return 0; } // ?? Let child class override the batch size if needed $customBatchSize = $this->getCustomBatchSize(); $batchSize = $customBatchSize !== null ? $customBatchSize : ($this->helper->getCronBatchSize() ?: 50); $this->logger->info("[$this->JOB_CODE] processQueueItems: batch size -> " . $batchSize); return $this->getQueueManager()->processBatch($batchSize); } /** * Check if the cron is running currently. * * @return bool */ public function isRunningCurrently($type = self::PROCESS) { $schedule = $this->scheduleCollectionFactory->create() ->addFieldToFilter('job_code', $this->getJobCode() . '_' . $type) ->addFieldToFilter('status', Schedule::STATUS_RUNNING); return $schedule->count() > 1; // 1 meaning the current job } /** * Get the number of processed items. * * @return int */ public function getProcessed() { return $this->processed; } }