Files
grzanieplus.pl/plugins/stTaskSchedulerImportPlugin/lib/stTaskSchedulerImportPreloadedDataTask.class.php
2025-03-12 17:06:23 +01:00

143 lines
4.2 KiB
PHP

<?php
class stTaskSchedulerImportPreloadedDataTask extends stTask
{
const LIMIT = 20;
/**
* Undocumented variable
*
* @var stTaskSchedulerImportPreloadedDataAbstract
*/
protected $preloadedDataImport = [];
public function initialize()
{
if ($this->getParameter('execute_verification', true))
{
$this->setParameter('execute_verification', false);
foreach (stTaskSchedulerImportConfiguration::getAll() as $configuration)
{
$parentConfig = $configuration->getParent() ? $configuration->getParent() : $configuration;
if (null !== $parentConfig->getOption('preloaded_data_import') && stTaskScheluder::getTask($configuration->getId())->getStatus() == stTask::STATUS_RUNNING)
{
$task = stTaskScheluder::getTask($configuration->getId());
$this->getLogger()->info('Import został pominięty (powód: Oczekuje na zakończenie zadania **%task%**)', [
'%task%' => $task->getName(),
]);
return false;
}
}
$this->doCount(true);
}
}
public function count(): int
{
$count = stTaskSchedulerImportPreloadedDataPeer::doCount($this->getCriteria());
return $count;
}
public function finished()
{
if (null !== $this->getParameter('import_hash_id'))
{
stTaskSchedulerImportPreloadedDataPeer::doDelete($this->getCriteria());
}
else
{
Propel::getConnection()->executeQuery(sprintf('TRUNCATE TABLE %s', stTaskSchedulerImportPreloadedDataPeer::TABLE_NAME));
}
}
public function execute(int $offset): int
{
$c = $this->getCriteria();
$c->setOffset($offset);
$c->setLimit(self::LIMIT);
$c->addAscendingOrderByColumn(stTaskSchedulerImportPreloadedDataPeer::IMPORT_HASH_ID);
$results = stTaskSchedulerImportPreloadedDataPeer::doSelect($c);
if (empty($results))
{
return $this->doCount();
}
$limit = null;
foreach ($results as $preloadedData)
{
$preloadedDataImport = $this->getPreloadedDataImport($preloadedData);
if (null === $limit)
{
$limit = $preloadedDataImport->getLimit();
}
try
{
$data = $preloadedData->getData();
$data['data_id'] = $preloadedData->getDataId();
$preloadedDataImport->process($data);
$preloadedDataImport->clearStaticPools();
usleep($preloadedDataImport->getDelay());
}
catch (stTaskSchedulerImportLogException $e)
{
$this->getLogger()->log($e->getCode(), $e->getMessage(), $e->getMessageParameters());
}
$offset++;
$limit--;
if ($limit === 0)
{
break;
}
}
return $offset;
}
protected function getPreloadedDataImport(stTaskSchedulerImportPreloadedData $preloadedData): stTaskSchedulerImportPreloadedDataAbstract
{
if (!isset($this->preloadedDataImport[$preloadedData->getImportHashId()]))
{
$configuration = stTaskSchedulerImportConfiguration::getByHashId($preloadedData->getImportHashId());
if ($configuration->getParent())
{
$configuration = $configuration->getParent();
}
$preloadedDataImportClass = $configuration->getOption('preloaded_data_import');
$this->preloadedDataImport[$preloadedData->getImportHashId()] = new $preloadedDataImportClass($this, $configuration);
}
return $this->preloadedDataImport[$preloadedData->getImportHashId()];
}
protected function getCriteria(): Criteria
{
$c = new Criteria();
if (null !== $this->getParameter('import_hash_id'))
{
$c->add(stTaskSchedulerImportPreloadedDataPeer::IMPORT_HASH_ID, $this->getParameter('import_hash_id'));
}
return $c;
}
}