рефакторинг бекапов. бекапы для тестирования. бекапы по кнопке

++
This commit is contained in:
2025-03-08 02:42:21 +03:00
parent e428d621ac
commit c4157fc30b
18 changed files with 275 additions and 149 deletions

View File

@@ -1,5 +1,6 @@
package _VisualDVM.ComponentsServer;
import Common.Database.Objects.DBObject;
import Common.Database.Objects.riDBObject;
import Common.Database.RepositoryRefuseException;
import Common.Utils.Utils_;
import _VisualDVM.ComponentsServer.BugReport.BugReport;
@@ -39,68 +40,6 @@ import java.util.Date;
import java.util.Vector;
public class ComponentsServer extends RepositoryServer<BugReportsDatabase> {
public CredentialsDatabase credentials_db = null;
//--
public static ZipFolderPass zip = new ZipFolderPass();
public static UnzipFolderPass unzip = new UnzipFolderPass();
public static ArchivesBackupPass backupSession = new ArchivesBackupPass();
//-
public static Vector<Pair<Machine, User>> storages = new Vector<>(
Arrays.asList(new Pair<>(new Machine("titan",
"dvmh.keldysh.ru",
22, MachineType.Server),
new User("dvmuser1", "mprit_2011")) // todo с этим паролем что-то сделать.
));
//-
protected Thread backUp = new Thread(() -> {
while (true) {
try {
//-------------------------------------
Calendar rightNow = Calendar.getInstance();
int year = rightNow.get(Calendar.YEAR);
int month = rightNow.get(Calendar.MONTH);
int day = rightNow.get(Calendar.DAY_OF_MONTH);
int hour = rightNow.get(Calendar.HOUR_OF_DAY);
int minute = rightNow.get(Calendar.MINUTE);
if ((hour == Global.componentsServerProperties.BackupHour) && (minute == Global.componentsServerProperties.BackupMinute)) {
//определить имя папки с багом.
String backUpName = year + "_" + (month + 1) + "_" + (day);
File todayBackUp = Paths.get(Global.DataBackUpsDirectory.getAbsolutePath(), backUpName).toFile();
File todayBackUpArchive = Paths.get(Global.DataBackUpsDirectory.getAbsolutePath(), backUpName + ".zip").toFile();
//-
File bugsDBBackUp = Paths.get(todayBackUp.getAbsolutePath(), db.getFile().getName()).toFile();
File bugsArchives = Paths.get(todayBackUp.getAbsolutePath(), "Bugs.zip").toFile();
//-
// Чистка старых бекапов на самом сервере.
Utils.keepNewFiles(todayBackUp.getParentFile(), 2);
if (!todayBackUpArchive.exists()) {
FileUtils.forceMkdir(todayBackUp);
Files.copy(db.getFile().toPath(), bugsDBBackUp.toPath());
//-
zip.Do("Bugs", bugsArchives.getAbsolutePath());
zip.Do(todayBackUp.getAbsolutePath(), todayBackUpArchive.getAbsolutePath());
Utils_.forceDeleteWithCheck(todayBackUp);
//-
for (Pair<Machine, User> cred : storages) {
backupSession.Do(cred.getKey(), cred.getValue(), todayBackUpArchive);
}
//bonus backup
//todo оформить как у планировщика, так как это нить, напрямую сервер трогать нельзя.
if (rightNow.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY) {
EmailMessage message = new EmailMessage("db backup", "копия баз данных журнала ошибок");
message.addAttachement(db.getFile());
for (String address : Constants.admins_mails) {
Email(message, address);
}
}
}
}
//-------------------------------------
Thread.sleep(60000);
} catch (Exception ex) {
Utils_.MainLog.PrintException(ex);
}
}
});
public ComponentsServer() {
super(BugReportsDatabase.class);
}
@@ -113,6 +52,10 @@ public class ComponentsServer extends RepositoryServer<BugReportsDatabase> {
return Constants.ComponentsServerPort;
}
@Override
protected void extraBackup(File todayBackUp) {
zip.Do("Bugs",new File (todayBackUp, "Bugs.zip").getAbsolutePath());
}
@Override
protected void beforePublishAction(DBObject object) throws Exception {
if (object instanceof BugReport) {
BugReport bugReport = (BugReport) object;
@@ -151,10 +94,6 @@ public class ComponentsServer extends RepositoryServer<BugReportsDatabase> {
ex.printStackTrace();
}
}
@Override
protected void startAdditionalThreads() {
backUp.start();
}
//-
void DVMConvertProject() throws Exception {
String[] args = request.arg.split("\n");

View File

@@ -3,7 +3,7 @@ import Common.Utils.Vector_;
import java.util.Vector;
public class Constants {
public static final int version = 1200;
public static final int version = 1201;
public static final int planner_version = 12;
public static final int testingMaxKernels = 64;
//--

View File

@@ -39,7 +39,7 @@ public class Global {
public static File KeysDirectory;
public static File DataDirectory;
public static File BugReportsDirectory;
public static File BackUpsDirectory;
public static File BackUpsDirectory; //бекапы компонентов на локальной машине.
public static File TempDirectory;
public static File ProjectsDirectory;
public static File CompilationTasksDirectory;
@@ -48,7 +48,7 @@ public class Global {
public static File RepoDirectory;
public static File TestsDirectory;
public static File PerformanceAnalyzerDirectory;
public static File DataBackUpsDirectory;
public static File DataBackUpsDirectory; //бекапы данных сервера.
public static File DVMPackagesDirectory;
public static File SapforsDirectory;
public static File SapforPackagesDirectory;
@@ -61,8 +61,8 @@ public class Global {
public static Visualiser visualiser = null;
public static Visualizer_2 visualizer_2 = null;
public static void CheckVisualiserDirectories() {
Utils_.CheckDirectory(ComponentsDirectory = new File(Utils_.getHomeDirectory(), Constants.ComponentsDirectoryName));
Utils_.CheckAndCleanDirectory(TempDirectory = new File(Utils_.getHomeDirectory(), Constants.TempDirectoryName));
Utils_.CheckDirectory(ComponentsDirectory = new File(Utils_.getHomeDirectory(), Constants.ComponentsDirectoryName));
Utils_.CheckDirectory(DataDirectory = new File(Utils_.getHomeDirectory(), Constants.DataDirectoryName));
//-
Utils_.CheckDirectory(RepoDirectory = new File(Utils_.getHomeDirectory(), Constants.RepoDirectoryName));
@@ -84,17 +84,18 @@ public class Global {
}
}
public static void CheckServerDirectories() {
Utils_.CheckDirectory(ComponentsDirectory = new File(Utils_.getHomeDirectory(), Constants.ComponentsDirectoryName));
Utils_.CheckAndCleanDirectory(TempDirectory = new File(Utils_.getHomeDirectory(), Constants.TempDirectoryName));
Utils_.CheckDirectory(ComponentsDirectory = new File(Utils_.getHomeDirectory(), Constants.ComponentsDirectoryName));
Utils_.CheckDirectory(DataDirectory = new File(Utils_.getHomeDirectory(), Constants.DataDirectoryName));
//-
Utils_.CheckDirectory(BugReportsDirectory = new File(Utils_.getHomeDirectory(), Constants.BugsDirectoryName));
Utils_.CheckDirectory(DataBackUpsDirectory = new File(Utils_.getHomeDirectory(), Constants.DataBackUpsDirectoryName));
//--
Utils_.CheckDirectory(BugReportsDirectory = new File(Utils_.getHomeDirectory(), Constants.BugsDirectoryName));
Utils_.CheckDirectory(KeysDirectory = new File(Utils_.getHomeDirectory(), Constants.KeyDirectoryName));
}
public static void CheckTestingSystemDirectories() {
Utils_.CheckAndCleanDirectory(TempDirectory = new File(Utils_.getHomeDirectory(), Constants.TempDirectoryName));
Utils_.CheckDirectory(DataDirectory = new File(Utils_.getHomeDirectory(), Constants.DataDirectoryName));
Utils_.CheckDirectory(DataBackUpsDirectory = new File(Utils_.getHomeDirectory(), Constants.DataBackUpsDirectoryName));
//-
Utils_.CheckDirectory(TestsDirectory = new File(Utils_.getHomeDirectory(), Constants.TestsDirectoryName));
Utils_.CheckDirectory(RepoDirectory = new File(Utils_.getHomeDirectory(), Constants.RepoDirectoryName));

View File

@@ -28,10 +28,12 @@ public class MainModule extends MainModule_<GlobalDatabase, MainUI> {
PassCode.ShutdownComponentsServer,
PassCode.StartComponentsServer,
PassCode.PublishComponentsServer,
PassCode.ComponentsServerBackUp,
//
PassCode.ShutdownTestingServer,
PassCode.StartTestingServer,
PassCode.PublishTestingServer,
PassCode.TestingServerBackUp,
PassCode.CreateTestsGroupFromSelectedVersions,
PassCode.RefreshDVMPackageResults,
PassCode.ConvertCorrectnessTests

View File

@@ -0,0 +1,14 @@
package _VisualDVM.Passes.All;
import _VisualDVM.Global;
import _VisualDVM.Passes.Server.ComponentsServerPass;
import _VisualDVM.Repository.Server.ServerCode;
public class ComponentsServerBackUp extends ComponentsServerPass {
@Override
public String getIconPath() {
return "/icons/BackUp.png";
}
@Override
protected boolean canStart(Object... args) throws Exception {
return Global.mainModule.getAccount().isAdmin()&&SendRequest(ServerCode.CreateBackUp,"", true);
}
}

View File

@@ -0,0 +1,14 @@
package _VisualDVM.Passes.All;
import _VisualDVM.Global;
import _VisualDVM.Passes.Server.TestingServerPass;
import _VisualDVM.Repository.Server.ServerCode;
public class TestingServerBackUp extends TestingServerPass {
@Override
public String getIconPath() {
return "/icons/BackUp.png";
}
@Override
protected boolean canStart(Object... args) throws Exception {
return Global.mainModule.getAccount().isAdmin()&&SendRequest(ServerCode.CreateBackUp,"", true);
}
}

View File

@@ -354,12 +354,18 @@ public enum PassCode implements PassCode_ {
PublishUserAccount,
EditUserAccount,
DeleteUserAccount,
GetDVMPackageCredetials
GetDVMPackageCredetials,
ComponentsServerBackUp,
TestingServerBackUp
;
//--
@Override
public String getDescription() {
switch (this) {
case ComponentsServerBackUp:
return "Создание резервной копии данных сервера компонент";
case TestingServerBackUp:
return "Создание резервной копии данных сервера компонент";
case GetDVMPackageCredetials:
return "Получение входных данных пакета тестирования DVM";
case PublishUserAccount:

View File

@@ -0,0 +1,21 @@
package _VisualDVM.Repository.Server;
import java.util.Calendar;
public class BackUpsChecker extends RepositoryServerClient {
RepositoryServer server;
@Override
protected RepositoryServer getServer() {
return server;
}
public BackUpsChecker(RepositoryServer server_in) {
server = server_in;
}
@Override
protected void perform() throws Exception {
Calendar rightNow = Calendar.getInstance();
int hour = rightNow.get(Calendar.HOUR_OF_DAY);
int minute = rightNow.get(Calendar.MINUTE);
if ((hour == 0) && (minute == 0)) {
ServerCommand(ServerCode.CreateBackUp,false);
}
}
}

View File

@@ -6,10 +6,15 @@ import Common.Database.RepositoryRefuseException;
import Common.Utils.InterruptThread;
import Common.Utils.Utils_;
import _VisualDVM.Constants;
import _VisualDVM.Global;
import _VisualDVM.Passes.All.ArchivesBackupPass;
import _VisualDVM.Passes.All.UnzipFolderPass;
import _VisualDVM.Passes.All.ZipFolderPass;
import _VisualDVM.Passes.Server.RepositoryPass;
import _VisualDVM.Repository.EmailMessage;
import _VisualDVM.Utils;
import javafx.util.Pair;
import org.apache.commons.io.FileUtils;
import sun.misc.SignalHandler;
import javax.activation.DataHandler;
@@ -23,10 +28,8 @@ import javax.mail.internet.MimeMultipart;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Properties;
import java.util.Vector;
import java.nio.file.Files;
import java.util.*;
public abstract class RepositoryServer<D extends Database> {
protected static FileWriter Log;
protected static boolean printOn = true;
@@ -43,15 +46,40 @@ public abstract class RepositoryServer<D extends Database> {
//-
protected ServerCode code;
protected long count = 0; //для отладки.
BackUpsChecker backUpsChecker = null;
/*
public static Vector<Pair<Machine, User>> storages = new Vector<>(
Arrays.asList(new Pair<>(new Machine("titan",
"dvmh.keldysh.ru",
22, MachineType.Server),
new User("dvmuser1", "mprit_2011"))
));
//-
*/
protected Thread interruptThread = new InterruptThread(10000,
() -> {
System.exit(0);
return null;
});
protected Thread backUpsThread = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
backUpsChecker.Perform();
Utils_.sleep(60000);
}
}
});
Class<D> d_class;
//-----------RECURSION ->
SignalHandler signalHandler = signal -> {
};
//--
public static ZipFolderPass zip = new ZipFolderPass();
public static UnzipFolderPass unzip = new UnzipFolderPass();
public static ArchivesBackupPass backupSession = new ArchivesBackupPass();
//--
public RepositoryServer(Class<D> d_class_in) {
d_class = d_class_in;
}
@@ -160,7 +188,11 @@ public abstract class RepositoryServer<D extends Database> {
DiagnosticSignalHandler.install("TERM", signalHandler);
DiagnosticSignalHandler.install("INT", signalHandler);
DiagnosticSignalHandler.install("ABRT", signalHandler);
//--
interruptThread.start();
backUpsChecker = new BackUpsChecker(this);
backUpsThread.start();
//--
startAdditionalThreads();
server = new ServerSocket(getPort());
StartAction();
@@ -198,6 +230,9 @@ public abstract class RepositoryServer<D extends Database> {
case Email:
Email();
break;
case CreateBackUp:
CreateBackUp();
break;
case EditObject:
EditObject();
break;
@@ -324,6 +359,53 @@ public abstract class RepositoryServer<D extends Database> {
}
void Email() throws Exception {
Email((EmailMessage) request.object, request.arg);
}
void CreateBackUp() throws Exception {
//определить имя папки с багом.
boolean force = (boolean) request.object;
//--
Calendar rightNow = Calendar.getInstance();
int year = rightNow.get(Calendar.YEAR);
int month = rightNow.get(Calendar.MONTH);
int day = rightNow.get(Calendar.DAY_OF_MONTH);
//--
String backUpName = year + "_" + (month + 1) + "_" + (day);
File todayBackUp = new File(Global.DataBackUpsDirectory, backUpName);
File todayBackUpArchive = new File(Global.DataBackUpsDirectory, backUpName + ".zip");
if (force&&todayBackUpArchive.exists())
Utils_.forceDeleteWithCheck(todayBackUpArchive);
//-
// Чистка старых бекапов на самом сервере.
Utils.keepNewFiles(todayBackUp.getParentFile(), 2);
if (!todayBackUpArchive.exists()) {
FileUtils.forceMkdir(todayBackUp);
//--
File dbBackUp = new File(todayBackUp, db.getFile().getName());
Files.copy(db.getFile().toPath(), dbBackUp.toPath());
//--перегружаемая часть.
extraBackup(todayBackUp);
//--- общая часть.
zip.Do(todayBackUp.getAbsolutePath(), todayBackUpArchive.getAbsolutePath());
Utils_.forceDeleteWithCheck(todayBackUp);
//-
// for (Pair<Machine, User> cred : storages) {
// backupSession.Do(cred.getKey(), cred.getValue(), todayBackUpArchive);
// }
//bonus backup
if (rightNow.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY) {
EmailMessage message = new EmailMessage(getClass().getSimpleName()+" BackUp",
"Резервная копия файлов");
message.addAttachement(todayBackUpArchive);
for (String address : Constants.admins_mails) {
Email(message, address);
}
}
}
};
protected void extraBackup(File todayBackUp){
}
//----
void PublishObject() throws Exception {

View File

@@ -0,0 +1,77 @@
package _VisualDVM.Repository.Server;
import Common.Passes.PassException;
import Common.Utils.Utils_;
import _VisualDVM.Global;
import _VisualDVM.Passes.Server.RepositoryPass;
import _VisualDVM.TestingSystem.Common.TestingServer;
import java.io.FileWriter;
import java.io.Serializable;
import java.util.Date;
public abstract class RepositoryServerClient<T extends RepositoryServer> {
protected abstract T getServer();
protected int getSleepMillis() {
return 2000;
}
//---
protected boolean isPrintOn() {
return true;
}
protected void Print(String message) {
try {
if (isPrintOn()) {
FileWriter testLog = new FileWriter(getClass().getSimpleName() + "_Log.txt", true);
String dmessage = Utils_.Brackets(new Date()) + " " + message;
testLog.write(dmessage + "\n");
testLog.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
//--
protected Object ServerCommand(ServerCode code_in, String arg, Serializable object_in) throws Exception {
RepositoryPass<T,Object> pass = new RepositoryPass<T, Object>(getServer()) {
@Override
public String getDescription() {
return "";
}
@Override
protected void ServerAction() throws Exception {
Command(new SafeServerExchangeUnit(code_in, arg, object_in));
target = server_response.object;
}
@Override
protected boolean validate() {
return Log.isEmpty();
}
@Override
protected int getTimeout() {
return 0;
}
};
if (!pass.Do()) {
ServerConnectionError(code_in, pass.Log.toString());
}
return pass.target;
}
protected Object ServerCommand(ServerCode code_in, Serializable object_in) throws Exception {
return ServerCommand(code_in, "", object_in);
}
protected Object ServerCommand(ServerCode code_in) throws Exception {
return ServerCommand(code_in, "", null);
}
protected void ServerConnectionError(ServerCode code_in, String logText) throws Exception {
throw new PassException(Utils_.Brackets(new Date().toString()) + " Ошибка взаимодействия с сервером " + code_in);
}
protected abstract void perform() throws Exception;
public void Perform() {
try {
perform();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
Utils_.sleep(getSleepMillis());
}
}
}

View File

@@ -68,9 +68,12 @@ public enum ServerCode {
EditUserAccount,
PublishUserAccount,
DeleteUserAccount,
GetDVMPackageCredentials;
GetDVMPackageCredentials,
CreateBackUp;
public String getDescription(){
switch (this){
case CreateBackUp:
return "Создание резервной копии данных сервера";
case GetDVMPackageCredentials:
return "Получение входных данных пакета тестирования DVM на сервере";
case DeleteUserAccount:

View File

@@ -3,76 +3,16 @@ import Common.Passes.PassException;
import Common.Utils.Utils_;
import _VisualDVM.Global;
import _VisualDVM.Passes.Server.RepositoryPass;
import _VisualDVM.Repository.Server.RepositoryServerClient;
import _VisualDVM.Repository.Server.SafeServerExchangeUnit;
import _VisualDVM.Repository.Server.ServerCode;
import java.io.FileWriter;
import java.io.Serializable;
import java.util.Date;
public abstract class TestingClient {
//--
protected int getSleepMillis() {
return 2000;
}
//---
protected boolean isPrintOn() {
return true;
}
protected void Print(String message) {
try {
if (isPrintOn()) {
FileWriter testLog = new FileWriter(getClass().getSimpleName() + "_Log.txt", true);
String dmessage = Utils_.Brackets(new Date()) + " " + message;
testLog.write(dmessage + "\n");
testLog.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
//--
protected Object ServerCommand(ServerCode code_in, String arg, Serializable object_in) throws Exception {
RepositoryPass<TestingServer,Object> pass = new RepositoryPass<TestingServer, Object>(Global.testingServer) {
@Override
public String getDescription() {
return "";
}
@Override
protected void ServerAction() throws Exception {
Command(new SafeServerExchangeUnit(code_in, arg, object_in));
target = server_response.object;
}
@Override
protected boolean validate() {
return Log.isEmpty();
}
@Override
protected int getTimeout() {
return 0;
}
};
if (!pass.Do()) {
ServerConnectionError(code_in, pass.Log.toString());
}
return pass.target;
}
protected Object ServerCommand(ServerCode code_in, Serializable object_in) throws Exception {
return ServerCommand(code_in, "", object_in);
}
protected Object ServerCommand(ServerCode code_in) throws Exception {
return ServerCommand(code_in, "", null);
}
protected void ServerConnectionError(ServerCode code_in, String logText) throws Exception {
throw new PassException(Utils_.Brackets(new Date().toString()) + " Ошибка взаимодействия с сервером " + code_in);
}
public abstract void perform() throws Exception;
public void Perform() {
try {
perform();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
Utils_.sleep(getSleepMillis());
}
public abstract class TestingClient extends RepositoryServerClient<TestingServer> {
@Override
protected TestingServer getServer() {
return Global.testingServer;
}
}

View File

@@ -86,7 +86,10 @@ public class TestingMenuBar extends VisualiserMenuBar {
});
addSeparator();
serverAdminLabel = addLabel(" управление сервером ");
addPasses(PassCode.StartTestingServer, PassCode.ShutdownTestingServer, PassCode.PublishTestingServer);
addPasses(PassCode.StartTestingServer,
PassCode.ShutdownTestingServer,
PassCode.PublishTestingServer,
PassCode.TestingServerBackUp);
}
public void ShowAutoCheckTesting() {
autorefreshButton.setIcon(Utils_.getIcon(Global.normalProperties.AutoCheckTesting ? "/Common/icons/Pick.png" : "/Common/icons/NotPick.png"));

View File

@@ -85,6 +85,13 @@ public class TestingServer extends RepositoryServer<TestsDatabase> {
ex.printStackTrace();
}
}
@Override
protected void extraBackup(File todayBackUp) {
zip.Do("Tests", new File (todayBackUp, "Tests.zip").getAbsolutePath());
zip.Do("Sapfors", new File (todayBackUp, "Sapfors.zip").getAbsolutePath());
zip.Do("DVMPackages", new File (todayBackUp, "DVMPackages.zip").getAbsolutePath());
zip.Do("SapforPackages", new File (todayBackUp, "SapforPackages.zip").getAbsolutePath());
}
public static void TimerOn() {
checkTimer = new Timer(Global.normalProperties.CheckTestingIntervalSeconds * 1000, e -> {
Global.mainModule.getPass(PassCode.ActualizePackages).Do();

View File

@@ -264,6 +264,7 @@ public class CallbackForm implements FormWithSplitters, CallbackWindow {
accountTools.add(Global.mainModule.getPass(PassCode.StartComponentsServer).createButton());
accountTools.add(Global.mainModule.getPass(PassCode.ShutdownComponentsServer).createButton());
accountTools.add(Global.mainModule.getPass(PassCode.PublishComponentsServer).createButton());
accountTools.add(Global.mainModule.getPass(PassCode.ComponentsServerBackUp).createButton());
}
public void LockMyOnly() {
if (!BugReportsMyOnlyFilter.isSelected()) {