2023-11-19 02:12:44 +03:00
|
|
|
|
package TestingSystem.Common;
|
2023-09-17 22:13:42 +03:00
|
|
|
|
import Common.Utils.Utils;
|
|
|
|
|
|
import Repository.EmailMessage;
|
2023-11-19 02:12:44 +03:00
|
|
|
|
import Repository.Server.ServerCode;
|
|
|
|
|
|
import Repository.Server.ServerExchangeUnit_2021;
|
2023-12-14 02:30:56 +03:00
|
|
|
|
import TestingSystem.Common.TestingPackage.TestingPackage;
|
2023-11-19 02:12:44 +03:00
|
|
|
|
import TestingSystem.DVM.TasksPackage.TasksPackageState;
|
|
|
|
|
|
import Visual_DVM_2021.Passes.PassException;
|
|
|
|
|
|
import Visual_DVM_2021.Passes.Server.TestingSystemPass;
|
2023-09-17 22:13:42 +03:00
|
|
|
|
|
|
|
|
|
|
import java.io.FileWriter;
|
|
|
|
|
|
import java.io.Serializable;
|
|
|
|
|
|
import java.util.Date;
|
2023-12-14 02:30:56 +03:00
|
|
|
|
public abstract class TestingPlanner<P extends TestingPackage> {
|
|
|
|
|
|
protected P testingPackage;
|
|
|
|
|
|
protected int getSleepMillis() {
|
|
|
|
|
|
return 2000;
|
2023-09-17 22:13:42 +03:00
|
|
|
|
}
|
2023-12-14 02:30:56 +03:00
|
|
|
|
//---
|
|
|
|
|
|
protected Object ServerCommand(ServerCode code_in, String arg, Serializable object_in) throws Exception {
|
2023-09-17 22:13:42 +03:00
|
|
|
|
TestingSystemPass<Object> pass = new TestingSystemPass<Object>() {
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public String getDescription() {
|
|
|
|
|
|
return "";
|
|
|
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
|
|
|
protected void ServerAction() throws Exception {
|
|
|
|
|
|
Command(new ServerExchangeUnit_2021(code_in, arg, object_in));
|
|
|
|
|
|
target = response.object;
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
if (!pass.Do()) throw new PassException("Ошибка взаимодействия с сервером " + code_in);
|
|
|
|
|
|
return pass.target;
|
|
|
|
|
|
}
|
2023-12-14 02:30:56 +03:00
|
|
|
|
protected Object ServerCommand(ServerCode code_in, Serializable object_in) throws Exception {
|
|
|
|
|
|
return ServerCommand(code_in, "", object_in);
|
2023-09-17 22:13:42 +03:00
|
|
|
|
}
|
2023-12-14 02:30:56 +03:00
|
|
|
|
protected Object ServerCommand(ServerCode code_in) throws Exception {
|
|
|
|
|
|
return ServerCommand(code_in, "", null);
|
2023-09-17 22:13:42 +03:00
|
|
|
|
}
|
2023-12-14 02:30:56 +03:00
|
|
|
|
//---
|
|
|
|
|
|
protected boolean isPrintOn() {
|
2023-09-17 22:13:42 +03:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
2023-12-14 02:30:56 +03:00
|
|
|
|
protected void Print(String message) {
|
2023-09-17 22:13:42 +03:00
|
|
|
|
try {
|
2023-12-14 02:30:56 +03:00
|
|
|
|
if (isPrintOn()) {
|
|
|
|
|
|
FileWriter testLog = new FileWriter(getClass().getSimpleName() + "_Log.txt", true);
|
|
|
|
|
|
String dmessage = Utils.Brackets(new Date()) + " " + message;
|
2023-09-17 22:13:42 +03:00
|
|
|
|
System.out.println(dmessage);
|
2023-12-14 02:30:56 +03:00
|
|
|
|
testLog.write(dmessage + "\n");
|
|
|
|
|
|
testLog.close();
|
|
|
|
|
|
}
|
2023-09-17 22:13:42 +03:00
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
|
ex.printStackTrace();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-12-14 02:30:56 +03:00
|
|
|
|
//---
|
|
|
|
|
|
void UpdatePackageState(TasksPackageState state_in) throws Exception {
|
|
|
|
|
|
testingPackage.state = state_in;
|
|
|
|
|
|
testingPackage.ChangeDate = new Date().getTime();
|
|
|
|
|
|
ServerCommand(ServerCode.EditObject, testingPackage);
|
|
|
|
|
|
}
|
|
|
|
|
|
void UpdatePackage() throws Exception {
|
|
|
|
|
|
testingPackage.ChangeDate = new Date().getTime();
|
|
|
|
|
|
ServerCommand(ServerCode.EditObject, testingPackage);
|
|
|
|
|
|
}
|
|
|
|
|
|
void EmailPackage() throws Exception {
|
|
|
|
|
|
EmailMessage message = new EmailMessage();
|
|
|
|
|
|
message.subject = "Состояние пакета задач " + Utils.Brackets(testingPackage) + " изменилось на " + Utils.Brackets(testingPackage.state.getDescription());
|
|
|
|
|
|
message.text = testingPackage.description;
|
|
|
|
|
|
message.targets.add(testingPackage.sender_address);
|
|
|
|
|
|
ServerCommand(ServerCode.Email, message);
|
|
|
|
|
|
}
|
|
|
|
|
|
//---
|
|
|
|
|
|
protected abstract ServerCode getActivePackageCode();
|
|
|
|
|
|
protected abstract ServerCode getCheckIfNeedsKillCode();
|
|
|
|
|
|
protected abstract TasksPackageState getStateAfterStart();
|
|
|
|
|
|
protected void InitSessionCredentials() {
|
|
|
|
|
|
}
|
2023-12-14 18:45:41 +03:00
|
|
|
|
protected abstract void TestsSynchronize() throws Exception;
|
|
|
|
|
|
protected abstract void PackageWorkspaceCreation() throws Exception;
|
|
|
|
|
|
protected abstract void AnalyseResults() throws Exception;
|
|
|
|
|
|
protected abstract void PackageStart() throws Exception;
|
|
|
|
|
|
protected abstract boolean CheckNextState() throws Exception;
|
|
|
|
|
|
protected abstract void DownloadResults() throws Exception;
|
|
|
|
|
|
protected abstract void Kill() throws Exception;
|
2023-12-14 02:30:56 +03:00
|
|
|
|
protected boolean Connect() {
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
protected void Disconnect() {
|
|
|
|
|
|
}
|
|
|
|
|
|
//---
|
2023-12-15 14:55:16 +03:00
|
|
|
|
/*
|
2023-12-15 14:02:12 +03:00
|
|
|
|
protected void CheckExecutionStates() throws Exception{
|
|
|
|
|
|
|
2023-12-15 14:55:16 +03:00
|
|
|
|
case CompilationWorkspacesCreation:
|
|
|
|
|
|
case CompilationPreparation:
|
|
|
|
|
|
case CompilationExecution:
|
|
|
|
|
|
case RunningWorkspacesCreation:
|
|
|
|
|
|
case RunningPreparation:
|
|
|
|
|
|
case RunningExecution:
|
|
|
|
|
|
if (CheckNextState()) UpdatePackage();
|
|
|
|
|
|
break;
|
2023-12-15 14:02:12 +03:00
|
|
|
|
}
|
2023-12-15 14:55:16 +03:00
|
|
|
|
*/
|
2023-12-14 02:30:56 +03:00
|
|
|
|
//жизненный цикл планировщика
|
|
|
|
|
|
protected void Session() throws Exception {
|
|
|
|
|
|
switch (testingPackage.state) {
|
|
|
|
|
|
case TestsSynchronize:
|
|
|
|
|
|
TestsSynchronize();
|
|
|
|
|
|
UpdatePackageState(TasksPackageState.PackageWorkspaceCreation);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case PackageWorkspaceCreation:
|
|
|
|
|
|
PackageWorkspaceCreation();
|
|
|
|
|
|
UpdatePackageState(TasksPackageState.PackageStart);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case PackageStart:
|
|
|
|
|
|
PackageStart();
|
|
|
|
|
|
EmailPackage();
|
2023-12-15 14:02:12 +03:00
|
|
|
|
testingPackage.StartDate = new Date().getTime();
|
2023-12-14 19:57:47 +03:00
|
|
|
|
UpdatePackageState(getStateAfterStart());
|
2023-12-14 02:30:56 +03:00
|
|
|
|
break;
|
|
|
|
|
|
case RunningEnd:
|
|
|
|
|
|
DownloadResults();
|
|
|
|
|
|
UpdatePackageState(TasksPackageState.Analysis);
|
|
|
|
|
|
break;
|
2023-12-15 14:55:16 +03:00
|
|
|
|
default:
|
|
|
|
|
|
if (CheckNextState()) UpdatePackage();
|
|
|
|
|
|
break;
|
2023-10-13 21:23:25 +03:00
|
|
|
|
}
|
2023-09-17 22:13:42 +03:00
|
|
|
|
}
|
2023-12-14 02:30:56 +03:00
|
|
|
|
// ---
|
2023-09-17 22:13:42 +03:00
|
|
|
|
public void Perform() {
|
|
|
|
|
|
while (true) {
|
|
|
|
|
|
try {
|
2023-12-14 02:30:56 +03:00
|
|
|
|
testingPackage = (P) ServerCommand(getActivePackageCode());
|
|
|
|
|
|
if (testingPackage != null) {
|
|
|
|
|
|
Print(testingPackage.id + ":" + testingPackage.state.getDescription());
|
|
|
|
|
|
//--
|
|
|
|
|
|
InitSessionCredentials();
|
|
|
|
|
|
if (testingPackage.state.equals(TasksPackageState.Analysis)) {
|
|
|
|
|
|
AnalyseResults();
|
|
|
|
|
|
UpdatePackageState(TasksPackageState.Done);
|
|
|
|
|
|
EmailPackage();
|
|
|
|
|
|
} else {
|
|
|
|
|
|
try {
|
|
|
|
|
|
if (Connect()) {
|
|
|
|
|
|
if ((boolean) ServerCommand(getCheckIfNeedsKillCode(), testingPackage.id)) {
|
|
|
|
|
|
Print("package " + testingPackage.id + " NEEDS TO KILL");
|
|
|
|
|
|
Kill();
|
|
|
|
|
|
UpdatePackageState(TasksPackageState.Aborted);
|
|
|
|
|
|
EmailPackage();
|
|
|
|
|
|
} else
|
|
|
|
|
|
Session();
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
|
Print("Ошибка сеанса.");
|
|
|
|
|
|
Print(ex.getMessage());
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
Disconnect();
|
|
|
|
|
|
}
|
2023-09-17 22:13:42 +03:00
|
|
|
|
}
|
2023-12-14 18:45:41 +03:00
|
|
|
|
//--
|
|
|
|
|
|
testingPackage.destructor();
|
|
|
|
|
|
testingPackage = null;
|
|
|
|
|
|
System.gc();
|
|
|
|
|
|
//--
|
2023-09-17 22:13:42 +03:00
|
|
|
|
}
|
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
|
ex.printStackTrace();
|
|
|
|
|
|
} finally {
|
2023-12-14 02:30:56 +03:00
|
|
|
|
Utils.sleep(getSleepMillis());
|
2023-09-17 22:13:42 +03:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|