<?php
namespace App\Cron\Job; class DatabaseCleanupForOldUserTraces extends \App\Cron\Job\Utils\ShortRunningCronJobAbstract { protected $category = 'system'; public function getSettingsDialog(array $fields) { $form = parent::getSettingsDialog($fields); $_LANG = \App\Loader_Lang::getInstance()->getGlobal(); $lang = \App\Loader_Lang::getInstance()->getSection("Admin/CronJobs/Management/DatabaseCleanupForOldUserTraces"); $formContainer = \Engine\Form\Container\Fieldset::create($_LANG['Admin']['CronJobs']['Management']['formFields']['legend']); $form->addContainer($formContainer); $fld = new \Engine\Form\Input\Select($lang['options']['tracesPeriod'], 'tracesPeriod', $fields['settings']['tracesPeriod'], [ '6m' => $lang['options']['tracesPeriod_values_6m'], '1y' => $lang['options']['tracesPeriod_values_1y'], '1.5y' => $lang['options']['tracesPeriod_values_1.5y'], '2y' => $lang['options']['tracesPeriod_values_2y'], '2.5y' => $lang['options']['tracesPeriod_values_2.5y'], '3y' => $lang['options']['tracesPeriod_values_3y'], '4y' => $lang['options']['tracesPeriod_values_4y'], '5y' => $lang['options']['tracesPeriod_values_5y'], ]); $fld->setTooltip($lang['options']['tracesPeriodTooltip']); $formContainer->addElement($fld); $formContainer->groupInputsInArray("settings"); return $form; } protected function executeJob() { $cutOffDate = null; switch ($this->settings['tracesPeriod']) { case '6m': $cutOffDate = \Engine\DateTime::create("-6 months")->format("Ym"); break; case '1y': $cutOffDate = \Engine\DateTime::create("-12 months")->format("Ym"); break; case '1.5y': $cutOffDate = \Engine\DateTime::create("-18 months")->format("Ym"); break; case '2y': $cutOffDate = \Engine\DateTime::create("-24 months")->format("Ym"); break; case '2.5y': $cutOffDate = \Engine\DateTime::create("-30 months")->format("Ym"); break; case '3y': $cutOffDate = \Engine\DateTime::create("-36 months")->format("Ym"); break; case '4y': $cutOffDate = \Engine\DateTime::create("-48 months")->format("Ym"); break; case '5y': $cutOffDate = \Engine\DateTime::create("-60 months")->format("Ym"); break; default: throw new \Exception("Unknown logsPeriod value: {$this->settings['logsPeriod']}"); } global $DBTUserTraces; $outdatedTables = []; $existentTables = \App\Utils\Db\TablesUtilsCached::getInstance()->getMonthTablesList($DBTUserTraces); if (count($existentTables)) { foreach($existentTables as $tableMonth => $tableName) { if ($tableMonth < $cutOffDate) { $outdatedTables[$tableMonth] = $tableName; } } } if(empty($outdatedTables)) { $this->logger->idle("There are no old user traces to delete"); return; } $dropTableCount = 0; foreach($outdatedTables as $tableMonth => $tableName) { \App\Utils\Db\TablesUtilsCached::getInstance()->dropMonthTable($DBTUserTraces, $tableMonth); $this->logger->success(sprintf("Dropped old user trace table for %s", $tableMonth)); $dropTableCount++; } $this->logger->success(sprintf("Total dropped tables %s", $dropTableCount)); } }