Files
VisualSapfor/src/TestingSystem/Common/TestingPlanner.java

246 lines
10 KiB
Java
Raw Normal View History

package TestingSystem.Common;
2024-10-07 14:22:52 +03:00
import Common.CommonConstants;
import Common.Utils.CommonUtils;
import Common_old.Constants;
import _VisualDVM.Global;
import Common_old.Utils.Utils;
import GlobalData.Machine.Machine;
import GlobalData.Machine.MachineType;
import GlobalData.User.User;
2023-09-17 22:13:42 +03:00
import Repository.EmailMessage;
import Repository.Server.ServerCode;
import TestingSystem.Common.TestingPackage.TestingPackage;
import TestingSystem.Common.TestingPackageToKill.TestingPackageToKill;
2024-04-26 17:57:58 +03:00
import Repository.RepositoryClient;
2024-10-07 17:46:38 +03:00
import Common.Utils.Loggable;
import javafx.util.Pair;
import org.apache.commons.io.FileUtils;
2023-09-17 22:13:42 +03:00
import java.io.File;
2023-09-17 22:13:42 +03:00
import java.util.Date;
import java.util.Vector;
2024-04-26 17:57:58 +03:00
public abstract class TestingPlanner<P extends TestingPackage> extends RepositoryClient {
protected P testingPackage;
protected File packageLocalWorkspace = null;
protected String serverName = "";
protected File supervisorHome = null;
protected Machine machine = null;
protected User user = null;
//----
protected void UpdatePackageState(TasksPackageState state_in) throws Exception {
testingPackage.state = state_in;
testingPackage.ChangeDate = new Date().getTime();
ServerCommand(ServerCode.EditObject, testingPackage);
switch (testingPackage.state) {
case Done:
case DoneWithErrors:
case Aborted:
case CompilationExecution:
case RunningExecution:
EmailPackage();
break;
}
}
protected void UpdatePackageState() throws Exception {
testingPackage.ChangeDate = new Date().getTime();
ServerCommand(ServerCode.EditObject, testingPackage);
switch (testingPackage.state) {
case Done:
case DoneWithErrors:
case Aborted:
case CompilationExecution:
case RunningExecution:
EmailPackage();
break;
}
}
void UpdatePackage() throws Exception {
testingPackage.ChangeDate = new Date().getTime();
ServerCommand(ServerCode.EditObject, testingPackage);
}
public abstract String packageDescription();
void EmailPackage() throws Exception {
if (testingPackage.needsEmail == 1) {
EmailMessage message = new EmailMessage();
message.subject = "Состояние пакета тестирования "+packageDescription()+ " "+
2024-10-07 14:22:52 +03:00
CommonUtils.Brackets(testingPackage.id) + " изменилось на " + CommonUtils.Brackets(testingPackage.state.getDescription());
message.text = testingPackage.description;
message.targets.add(testingPackage.sender_address);
ServerCommand(ServerCode.Email, message);
}
}
//---
protected abstract ServerCode getActivePackagesCode();
protected abstract ServerCode getCheckIfNeedsKillCode();
protected abstract TasksPackageState getStateAfterStart();
protected void InitSessionCredentials() {
}
protected abstract void TestsSynchronize() throws Exception;
protected abstract void PackageWorkspaceCreation() throws Exception;
protected void AnalyseResults() throws Exception {
UpdatePackageState(TasksPackageState.Done);
};
protected abstract void PackageStart() throws Exception;
protected abstract boolean CheckNextState() throws Exception;
protected abstract void DownloadResults() throws Exception;
protected abstract void Kill() throws Exception;
protected boolean Connect() {
return true;
}
protected void Disconnect() {
}
protected void MachineConnectionError() {
2024-04-21 00:01:17 +03:00
}
protected void PerformPackage(TestingPackage package_in) throws Exception {
testingPackage = (P) package_in;
//--
Print(testingPackage.id + ":" + testingPackage.state.getDescription());
//--
if (testingPackage.connectionErrosCount >= 10) {
Print(testingPackage.id + " had 10 connection errors. stop");
UpdatePackageState(TasksPackageState.ConnectionError);
2024-04-21 00:01:17 +03:00
MachineConnectionError();
} else {
//--
InitSessionCredentials();
if (testingPackage.state.equals(TasksPackageState.Analysis)) {
AnalyseResults(); //todo ввести состояние DoneWithErrors
} else {
try {
if (Connect()) {
int ptk_id = (int) ServerCommand(getCheckIfNeedsKillCode(), testingPackage.id);
2024-10-07 14:22:52 +03:00
if (ptk_id != CommonConstants.Nan) {
Print("package " + testingPackage.id + " NEEDS TO KILL");
Kill();
UpdatePackageState(TasksPackageState.Aborted);
ServerCommand(ServerCode.DeleteObjectByPK, new Pair(TestingPackageToKill.class, ptk_id));
} else {
2024-04-21 00:01:17 +03:00
//--
2024-09-14 00:18:27 +03:00
System.out.println(testingPackage.id+":"+testingPackage.state.getDescription());
2024-04-21 00:01:17 +03:00
switch (testingPackage.state) {
case TestsSynchronize:
TestsSynchronize();
UpdatePackageState(TasksPackageState.PackageWorkspaceCreation);
break;
case PackageWorkspaceCreation:
PackageWorkspaceCreation();
UpdatePackageState(TasksPackageState.PackageStart);
break;
case PackageStart:
PackageStart();
testingPackage.StartDate = new Date().getTime();
UpdatePackageState(getStateAfterStart());
break;
case RunningEnd:
DownloadResults();
UpdatePackageState(TasksPackageState.Analysis);
break;
default:
if (CheckNextState()) UpdatePackage();
break;
}
//--
}
} else {
testingPackage.connectionErrosCount++;
UpdatePackage();
2023-09-17 22:13:42 +03:00
}
} catch (Exception ex) {
Print("Ошибка сеанса. Соединение будет разорвано.");
ex.printStackTrace();
Print(ex.getMessage());
//
testingPackage.connectionErrosCount++;
UpdatePackage();
} finally {
2024-04-10 00:29:36 +03:00
Disconnect();
2023-09-17 22:13:42 +03:00
}
}
}
//--
testingPackage.destructor();
testingPackage = null;
System.gc();
//--
}
2024-04-26 17:57:58 +03:00
@Override
public void perform() throws Exception {
testingPackage = null;
Vector<P> activePackages = (Vector<P>) ServerCommand(getActivePackagesCode());
for (P activePackage : activePackages)
PerformPackage(activePackage);
2023-09-17 22:13:42 +03:00
}
protected void Finalize(String reason) {
Print(reason);
File stateFile = new File(supervisorHome, Constants.ABORTED);
try {
FileUtils.writeStringToFile(stateFile, reason);
} catch (Exception ex) {
ex.printStackTrace();
}
System.exit(0);
}
//---------------------------------------------
public String getPlanner() {
return String.join("/", user.workspace, "modules", "planner");
}
//---
public TestingPlanner(){}
public TestingPlanner(String... args) {
2024-10-07 22:04:09 +03:00
CommonUtils.isWindows = System.getProperty("os.name").startsWith("Windows");
//---
String machineAddress = args[0];
int machinePort = Integer.parseInt(args[1]);
String userName = args[2];
String userPassword = args[3];
String userWorkspace = args[4];
String testingSystemRoot = args[5];
serverName = args[6];
2024-10-07 22:04:09 +03:00
supervisorHome = new File(CommonUtils.Home); //при инициализации это текущая папка.
//---
2024-10-07 22:04:09 +03:00
CommonUtils.MainLog = new Loggable() {
@Override
public String getLogHomePath() {
return supervisorHome.getAbsolutePath();
}
@Override
public String getLogName() {
2024-10-08 15:32:39 +03:00
return CommonUtils.mode.toString();
}
};
2024-10-07 22:04:09 +03:00
CommonUtils.MainLog.ClearLog();
//--
2024-10-07 22:04:09 +03:00
CommonUtils.Home = testingSystemRoot;
Global.CheckTestingSystemDirectories();
//---
machine = new Machine(machineAddress, machineAddress, machinePort, MachineType.Server);
user = new User(userName, userPassword, userWorkspace);
//---
2024-10-07 14:22:52 +03:00
Print("machineAddress=" + CommonUtils.Brackets(machineAddress));
Print("machinePort=" + CommonUtils.Brackets(String.valueOf(machinePort)));
Print("userName=" + CommonUtils.Brackets(userName));
Print("userPassword=" + CommonUtils.Brackets(userPassword));
Print("userWorkspace=" + CommonUtils.Brackets(userWorkspace));
2024-10-07 22:04:09 +03:00
Print("root=" + CommonUtils.Brackets(CommonUtils.Home));
Print("serverName=" + serverName);
Print("=====");
//----
Utils.createEmptyFile(Constants.STARTED);
}
/*
void CheckLocal() {
local = false;
try {
InetAddress address = InetAddress.getByName(machine.address);
InetAddress localAddress = InetAddress.getByName("alex-freenas.ddns.net");
Print("machine ip=" + Utils.Brackets(address.getHostAddress()));
Print("server ip=" + Utils.Brackets(localAddress.getHostAddress()));
local = localAddress.getHostAddress().equals(address.getHostAddress());
//todo в этом случае отдельный режим без ssh
} catch (Exception ex) {
Global.Log.PrintException(ex);
}
}
*/
2023-09-17 22:13:42 +03:00
}