Index: units/admin/admin_config.php =================================================================== --- units/admin/admin_config.php (revision 14590) +++ units/admin/admin_config.php (working copy) @@ -24,6 +24,10 @@ 1 => 'event', ), + 'RegularEvents' => Array( + 'optimize_performance' => Array('EventName' => 'OnOptimizePerformance', 'RunInterval' => 86400, 'Type' => reAFTER), + ), + 'TitlePresets' => Array ( 'tree_root' => Array ('format' => '!la_section_overview!'), Index: units/admin/admin_events_handler.php =================================================================== --- units/admin/admin_events_handler.php (revision 14637) +++ units/admin/admin_events_handler.php (working copy) @@ -1404,6 +1404,51 @@ $event->SetRedirectParam('action_completed', 1); } } + + /** + * [AGENT] + * 1. Delete all Debug files from system/.restricted folder (format debug_@977827436@.txt) + * 2. Run MySQL OPTIMIZE SQL one by one on all In-Portal tables (found by prefix). + * + * @param kEvent $event + * @return void + * @access protected + */ + protected function OnOptimizePerformance(&$event) + { + $start_time = adodb_mktime(); + + $sql = 'SELECT SessionKey + FROM ' . TABLE_PREFIX . 'UserSession + WHERE LastAccessed > ' . $start_time; + $active_sessions = array_flip($this->Conn->GetCol($sql)); + + $files = scandir(RESTRICTED); + $file_path = RESTRICTED . '/'; + + foreach ($files AS $file_name) { + if ( !preg_match('#^debug_@([0-9]{9})@.txt$#', $file_name, $matches) ) { + // not debug file + continue; + } + + $sid = $matches[1]; + + if ( isset($active_sessions[$sid]) || (filemtime($file_path . $file_name) > $start_time ) ) { + // debug file belongs to an active session + // debug file is recently created (after sessions snapshot) + continue; + } + + unlink($file_path . $file_name); + } + + $system_tables = $this->Conn->GetCol('SHOW TABLES LIKE "' . TABLE_PREFIX . '%"'); + + foreach ($system_tables AS $table_name) { + $this->Conn->Query('OPTIMIZE TABLE ' . $table_name); + } + } }