| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- <?php
- namespace MGModule\DNSManager2\mgLibs\custom\task;
- use \Exception;
- use \MGModule\DNSManager2\mgLibs\custom\manager;
- use \MGModule\DNSManager2\mgLibs\custom\TaskManager;
- use \MGModule\DNSManager2\models\custom\globalsetting\GlobalSettingEnum;
- use \MGModule\DNSManager2\models\custom\server;
- use \MGModule\DNSManager2\models\custom\task;
- use \MGModule\DNSManager2\mgLibs\custom\helpers;
- use \MGModule\DNSManager2\mgLibs\custom\manager\LogHelper;
- use MGModule\DNSManager2\models\custom\zone\Repository as ZoneRepository;
- use MGModule\DNSManager2\models\custom\zone\Zone;
- use MGModule\DNSManager2\models\whmcs\domains\domain;
- class ImportToFile extends TaskAbstract
- {
- protected $abort_after_repeats = array('main' => 0);
- protected $taskTypeCode = TaskTypeCodesCodes::IMPORTTOFILE;
- public function mainDescription()
- {
- $server_from = new server\Server($this->getParams('from'));
- return 'Import from: ' . $server_from->name.' to: '.$this->getParams('toFile');
- }
- public function main($params)
- {
- $cron_import_run_each = manager\GlobalSettingHelper::getSetting(GlobalSettingEnum::CRON_IMPORT_RUN_EACH?:5);
- if(!$this->isReadyToRun( $cron_import_run_each ))
- {
- return true;
- }
-
- LogHelper::addSuccessLog('Cron Importer', 'Cron Importer Started');
- if($this->getStatus() == task\TaskStatusEnum::START)
- {
- $this->addChild('fetchZonesList')->run();
- $this->setStatus(task\TaskStatusEnum::IN_PROGRESS);
- }
- elseif($this->getStatus() == task\TaskStatusEnum::IN_PROGRESS)
- {
- $zones_per_run = manager\GlobalSettingHelper::getSetting(GlobalSettingEnum::CRON_IMPORT_ZONES_PER_RUN);
- $childs = $this->getXChilds($zones_per_run, 'import', task\TaskStatusEnum::START);
- if(count($childs) < $zones_per_run)
- {
- $childs += $this->getXChilds($zones_per_run - count($childs), 'import');
- }
- foreach($childs as $child)
- {
- $child->run();
- }
- $this->setFinishedStatusOnCronRun('import', $zones_per_run);
- }
- LogHelper::addSuccessLog('Cron Importer', 'End Of Cron Importer Run');
- }
- public function fetchZonesList($params)
- {
- if($this->isInCliMode())
- {
- LogHelper::addSuccessLog('Cron Importer', 'Fetching Zones List For Server Id: '.$this->parent->getParams('from').' Started.');
- }
- $server_from = new server\Server($this->parent->getParams('from'));
- foreach($server_from->getModule()->getZones() as $domain => $ip)
- {
- #fix - should backup to file all zones
- // if(ZoneRepo::factory()->byServerID($server_from->id)->byName($domain)->one() === FALSE)
- // {
- $this->addResult(array(
- 'domain' => $domain,
- 'ip' => $ip,
- ));
- // }
- }
-
- $this->setStatus(task\TaskStatusEnum::FINISHED);
- if($this->isInCliMode())
- {
- LogHelper::addSuccessLog('Cron Importer', 'Fetching Zones List For Server Id: '.$this->parent->getParams('from').' Completed.');
- }
- }
- public function import($params)
- {
- try
- {
- $server_from = new server\Server($this->parent->getParams('from'));
- $module_from = $server_from->getModule();
- $module_from->setDomain(helpers\IdnaHelper::idnaDecode($params['domain']));
- if($module_from->isIPRequired())
- {
- $module_from->setIP($params['ip']);
- }
- $zone = new Zone();
- $zone->clientid = $this->getParams('clientid');
- $zone->type = $this->getParams('type');
- $zone->relid = $this->getParams('relid');
- $zone->name = helpers\IdnaHelper::idnaDecode( $this->getParams('domain'));
- $zone->ip = $this->getParams('ip');
- $zone->serverid = $server_from->id;
- $zone->status = 1;
- if(!$module_from->zoneExists())
- {
- $zone->status = 0;
- }
- helpers\ImportExportFileHelper::bulkImportZoneToFile($zone, 'zonesFilesStorage'.DIRECTORY_SEPARATOR.'bulkZones', $this->parent->getParams('toFile'));
- $this->setStatus(task\TaskStatusEnum::FINISHED);
- $result = TaskManager::getTaskResultByID($this->getParams('resultid'));
- $result->data['status'] = 'imported';
- $result->save();
-
- LogHelper::addSuccessLogUsingZone('Cron Importer - Import Zone', 'Zone Imported', $zone);
- }
- catch(Exception $exc)
- {
- $result = TaskManager::getTaskResultByID($this->getParams('resultid'));
- $result->data['status'] = $exc->getMessage();
- $result->save();
-
- LogHelper::addFailLog('Cron Importer - Import Failed', $exc->getMessage());
- }
- }
- }
|