ltsecurityinc安防产品在线商城代码
This commit is contained in:
+174
@@ -0,0 +1,174 @@
|
||||
<?php
|
||||
|
||||
namespace JR\NetSuite\Cron;
|
||||
|
||||
use Magento\Cron\Model\Schedule;
|
||||
|
||||
abstract class AbstractSyncCron {
|
||||
|
||||
public const FETCH = 'fetch';
|
||||
public const PROCESS = 'process';
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $processed = 0;
|
||||
|
||||
/**
|
||||
* @var \JR\NetSuite\Helper\Data
|
||||
*/
|
||||
protected $helper;
|
||||
|
||||
/**
|
||||
* @var \JR\NetSuite\Logger\Logger
|
||||
*/
|
||||
protected $logger;
|
||||
|
||||
/**
|
||||
* @var \Magento\Cron\Model\ScheduleFactory
|
||||
*/
|
||||
protected $scheduleFactory;
|
||||
|
||||
/**
|
||||
* @var \Magento\Cron\Model\ResourceModel\Schedule\CollectionFactory
|
||||
*/
|
||||
protected $scheduleCollectionFactory;
|
||||
|
||||
/**
|
||||
* AbstractSyncCron constructor.
|
||||
*
|
||||
* @param \Magento\Cron\Model\ScheduleFactory $scheduleFactory
|
||||
* @param \Magento\Cron\Model\ResourceModel\Schedule\CollectionFactory $scheduleCollectionFactory
|
||||
* @param \JR\NetSuite\Helper\Data $helper
|
||||
*/
|
||||
public function __construct(
|
||||
\Magento\Cron\Model\ScheduleFactory $scheduleFactory,
|
||||
\Magento\Cron\Model\ResourceModel\Schedule\CollectionFactory $scheduleCollectionFactory,
|
||||
\JR\NetSuite\Helper\Data $helper,
|
||||
) {
|
||||
$this->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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user