Synchronization.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. namespace MGModule\DNSManager2\mgLibs\custom\task;
  3. use \Exception;
  4. use \MGModule\DNSManager2 as main;
  5. use \MGModule\DNSManager2\mgLibs\custom\helpers\TimeDiffHelper;
  6. use \MGModule\DNSManager2\mgLibs\custom\helpers\StatusHelper;
  7. use \MGModule\DNSManager2\mgLibs\custom\manager\DefaultNotifications;
  8. use \MGModule\DNSManager2\mgLibs\custom\manager\EmailNotificationHelper;
  9. use \MGModule\DNSManager2\mgLibs\custom\manager\GlobalSettingHelper;
  10. use \MGModule\DNSManager2\mgLibs\custom\manager\NotificationHelper;
  11. use \MGModule\DNSManager2\models\custom\globalsetting\GlobalSettingEnum;
  12. use \MGModule\DNSManager2\mgLibs\custom\manager\LogHelper;
  13. use \MGModule\DNSManager2\mgLibs\custom\task\TaskTypeCodesCodes;
  14. use \MGModule\DNSManager2\models\custom\task;
  15. class Synchronization extends TaskAbstract
  16. {
  17. protected $abort_after_repeats = array('main' => 0);
  18. protected $taskTypeCode = TaskTypeCodesCodes::SYNCHRONISATION;
  19. public function mainDescription()
  20. {
  21. return 'Synchronizator';
  22. }
  23. public function main($params)
  24. {
  25. $this->setStatus(task\TaskStatusEnum::IN_PROGRESS);
  26. $diff = TimeDiffHelper::diff(GlobalSettingHelper::getSetting(GlobalSettingEnum::CRON_SYNC_LAST_RUN)?:'1991-12-19');
  27. $cron_sync_run_each = GlobalSettingHelper::getSetting(GlobalSettingEnum::CRON_SYNC_RUN_EACH?:5);
  28. if(!$this->isReadyToRun( $cron_sync_run_each )) {
  29. return true;
  30. }
  31. LogHelper::addSuccessLog('Cron Sync', 'Sync Started');
  32. $last_id = GlobalSettingHelper::getSetting(GlobalSettingEnum::CRON_SYNC_LAST_ID)?:0;
  33. $zones_per_run = GlobalSettingHelper::getSetting(GlobalSettingEnum::CRON_SYNC_ZONES_PER_RUN);
  34. $zones_per_run<1?$zones_per_run=1:'';
  35. $rep = $this->getZonesRepoWithIDGreaterThan($last_id,$zones_per_run);
  36. $count_filtered = $rep->count();
  37. $count_synchronized = 0;
  38. $details = array();
  39. $notify_only = GlobalSettingHelper::getSetting(GlobalSettingEnum::CRON_SYNC_NOTIFY_ONLY);
  40. $notify_only = ($notify_only === 'on' || $notify_only === false);
  41. /** @var main\models\custom\zone\Zone $zone */
  42. foreach($rep->get() as $zone)
  43. {
  44. try
  45. {
  46. /** @var main\models\custom\server\Server $server */
  47. $server = $zone->getServer();
  48. $nameservers = $server->getModule()->getNameservers();
  49. $ns_records = $zone->getModule()->getRecords('NS');
  50. $domain_ns_records = (new StatusHelper($zone->id))->getNameservers();
  51. foreach($nameservers as $nameserver)
  52. {
  53. if(!empty($nameserver->name) || !empty($nameserver->ip))
  54. {
  55. $value = empty($nameserver->name) ? $nameserver->ip : $nameserver->name;
  56. foreach($ns_records as $record)
  57. {
  58. if(trim($record->rdata->__toString(), '.') == trim($value, '.'))
  59. {
  60. continue 2;
  61. }
  62. }
  63. foreach($domain_ns_records as $record)
  64. {
  65. if(trim($record, '.') == trim($value, '.'))
  66. {
  67. continue 2;
  68. }
  69. }
  70. if(!$this->checkIfZoneStillActive($zone->id))
  71. {
  72. continue;
  73. }
  74. $count_synchronized++;
  75. $details[] = $zone->name;
  76. if(!$notify_only)
  77. {
  78. try
  79. {
  80. $zone->getModule()->terminateZone();
  81. $this->updateZone($zone);
  82. }
  83. catch(Exception $exc)
  84. {
  85. LogHelper::addFailLogUsingZone('Cron Sync - Terminate Zone', $exc->getMessage(), $zone);
  86. }
  87. LogHelper::addSuccessLogUsingZone('Cron Sync - Terminate Zone', '', $zone);
  88. }
  89. }
  90. }
  91. }
  92. catch(Exception $exc)
  93. {
  94. }
  95. try{
  96. if($zone->getModule()->zoneExists())
  97. {
  98. $zone->setThatExist();
  99. }
  100. else
  101. {
  102. $zone->setThatNotExist();
  103. }
  104. }
  105. catch(Exception $exc)
  106. {
  107. LogHelper::addFailLogUsingZone('Cron Sync - Set Zone Status', $exc->getMessage(), $zone);
  108. }
  109. }
  110. if($count_filtered < $zones_per_run)
  111. {
  112. $last_id = 0;
  113. }
  114. GlobalSettingHelper::setSetting(GlobalSettingEnum::CRON_SYNC_LAST_ID, $last_id);
  115. if($count_synchronized > 0)
  116. {
  117. EmailNotificationHelper::sendAdminNotification(
  118. DefaultNotifications::ADMIN_CRON_SYNCHRONIZATOR_NOTIFIACTION,
  119. array(
  120. 'notify_only' => $notify_only,
  121. 'zones_synchronized_count' => $count_synchronized,
  122. 'zones_synchronized' => $details,
  123. )
  124. );
  125. $zonesCountWord = $count_synchronized == 1 ? 'zone' : 'zones';
  126. if($notify_only)
  127. {
  128. NotificationHelper::addInfoNotification("Cron Synchronization - ".$count_synchronized." ".$zonesCountWord." should be synchronized");
  129. LogHelper::addSuccessLog('Cron Sync', "End Of Sync Run - ".$count_synchronized." ".$zonesCountWord." should be synchronized");
  130. }
  131. else
  132. {
  133. NotificationHelper::addInfoNotification("Cron Synchronization - ".$count_synchronized." ".$zonesCountWord." synchronized");
  134. LogHelper::addSuccessLog('Cron Sync', "End Of Sync Run - ".$count_synchronized." ".$zonesCountWord." zones synchronized");
  135. }
  136. }
  137. else
  138. {
  139. LogHelper::addSuccessLog('Cron Sync', "End Of Sync Run");
  140. }
  141. // $this->setStatus(task\TaskStatusEnum::FINISHED);
  142. }
  143. private function getZonesRepoWithIDGreaterThan($id, $zones_per_run)
  144. {
  145. $rep = new main\models\custom\zone\Repository();
  146. $rep->setFilter(0, array(
  147. 'customQuery' => 'id > :last_id ',
  148. 'params' => array('last_id' => $id)
  149. ));
  150. $rep->limit($zones_per_run);
  151. return $rep;
  152. }
  153. private function checkIfZoneStillActive($zoneId)
  154. {
  155. $repo = new main\models\custom\zone\Repository();
  156. $repo->byId($zoneId);
  157. $zone = $repo->one();
  158. if(!$zone)
  159. {
  160. return false;
  161. }
  162. return $zone->status === '1';
  163. }
  164. private function updateZone($zone)
  165. {
  166. if($zone->getModule()->zoneExists())
  167. {
  168. $zone->setThatExist();
  169. }
  170. else
  171. {
  172. $zone->setThatNotExist();
  173. }
  174. }
  175. }