промежуточный. прописал для планировщиков общий класс-предок
This commit is contained in:
@@ -1,61 +1,23 @@
|
||||
package TestingSystem.Common;
|
||||
import Common.Global;
|
||||
import Common.Utils.Utils;
|
||||
import GlobalData.Machine.Machine;
|
||||
import GlobalData.User.User;
|
||||
import Repository.EmailMessage;
|
||||
import Repository.Server.ServerCode;
|
||||
import Repository.Server.ServerExchangeUnit_2021;
|
||||
import TestingSystem.DVM.Tasks.TestCompilationTask;
|
||||
import TestingSystem.DVM.TasksPackage.TasksPackage;
|
||||
import TestingSystem.Common.TestingPackage.TestingPackage;
|
||||
import TestingSystem.DVM.TasksPackage.TasksPackageState;
|
||||
import TestingSystem.DVM.TestsSupervisor_2022;
|
||||
import TestingSystem.DVM.UserConnection;
|
||||
import TestingSystem.SAPFOR.SapforTasksPackage.SapforTasksPackage;
|
||||
import TestingSystem.SAPFOR.SapforTasksPackageSupervisor.SapforTasksPackageSupervisor;
|
||||
import Visual_DVM_2021.Passes.PassException;
|
||||
import Visual_DVM_2021.Passes.SSH.ConnectionPass;
|
||||
import Visual_DVM_2021.Passes.Server.TestingSystemPass;
|
||||
import javafx.util.Pair;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Vector;
|
||||
|
||||
import static Common.Constants.tests_db_name;
|
||||
public class TestingPlanner {
|
||||
public String email;
|
||||
TasksPackage tasksPackage;
|
||||
TestsSupervisor_2022 supervisor;
|
||||
LinkedHashMap<String, Machine> machines = new LinkedHashMap<>();
|
||||
LinkedHashMap<String, User> users = new LinkedHashMap<>();
|
||||
protected Machine machine = null;
|
||||
protected User user = null;
|
||||
public LinkedHashMap<Long, TestCompilationTask> packageTasks = new LinkedHashMap<>();
|
||||
//----------
|
||||
SapforTasksPackage sapforTasksPackage = null;
|
||||
//----------
|
||||
public void UpdatePackage() throws Exception {
|
||||
tasksPackage.ChangeDate = new Date().getTime();
|
||||
ServerCommand(ServerCode.EditAccountObject, tasksPackage);
|
||||
//---------------
|
||||
if ((tasksPackage.needsEmail == 1) &&
|
||||
(tasksPackage.state.equals(TasksPackageState.PackageStart) ||
|
||||
(tasksPackage.state.equals(TasksPackageState.Done)) ||
|
||||
(tasksPackage.state.equals(TasksPackageState.Aborted))
|
||||
)) {
|
||||
EmailMessage message = new EmailMessage();
|
||||
message.subject = "Состояние пакета задач " + Utils.Brackets(tasksPackage.id) + " изменилось на " + Utils.Brackets(tasksPackage.state.getDescription());
|
||||
message.text = tasksPackage.summary;
|
||||
message.targets.add(email);
|
||||
ServerCommand(ServerCode.Email, message);
|
||||
}
|
||||
public abstract class TestingPlanner<P extends TestingPackage> {
|
||||
protected P testingPackage;
|
||||
protected int getSleepMillis() {
|
||||
return 2000;
|
||||
}
|
||||
//-
|
||||
public Object ServerCommand(ServerCode code_in, String arg, Serializable object_in) throws Exception {
|
||||
//---
|
||||
protected Object ServerCommand(ServerCode code_in, String arg, Serializable object_in) throws Exception {
|
||||
TestingSystemPass<Object> pass = new TestingSystemPass<Object>() {
|
||||
@Override
|
||||
public String getDescription() {
|
||||
@@ -70,163 +32,132 @@ public class TestingPlanner {
|
||||
if (!pass.Do()) throw new PassException("Ошибка взаимодействия с сервером " + code_in);
|
||||
return pass.target;
|
||||
}
|
||||
public Object ServerCommand(ServerCode code_in, Serializable object_in) throws Exception {
|
||||
return ServerCommand(code_in, email, object_in);
|
||||
protected Object ServerCommand(ServerCode code_in, Serializable object_in) throws Exception {
|
||||
return ServerCommand(code_in, "", object_in);
|
||||
}
|
||||
Object ServerCommand(ServerCode code_in) throws Exception {
|
||||
return ServerCommand(code_in, email, null);
|
||||
protected Object ServerCommand(ServerCode code_in) throws Exception {
|
||||
return ServerCommand(code_in, "", null);
|
||||
}
|
||||
//-
|
||||
boolean isPrintOn() {
|
||||
//---
|
||||
protected boolean isPrintOn() {
|
||||
return true;
|
||||
}
|
||||
public void Print(String message) {
|
||||
protected void Print(String message) {
|
||||
try {
|
||||
FileWriter testLog = new FileWriter(getClass().getSimpleName() + "_Log.txt", true);
|
||||
String dmessage = Utils.Brackets(new Date()) + " " + message;
|
||||
if (isPrintOn())
|
||||
if (isPrintOn()) {
|
||||
FileWriter testLog = new FileWriter(getClass().getSimpleName() + "_Log.txt", true);
|
||||
String dmessage = Utils.Brackets(new Date()) + " " + message;
|
||||
System.out.println(dmessage);
|
||||
testLog.write(dmessage + "\n");
|
||||
testLog.close();
|
||||
testLog.write(dmessage + "\n");
|
||||
testLog.close();
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
boolean CheckConnection(Machine machine, User user) {
|
||||
//каждый раз соединяемся по новой. из за проблем с Exists.
|
||||
// к тому же, теперь задачи гоняет модуль, тут только проверка
|
||||
//так что время на разрыв уже не критично.
|
||||
try {
|
||||
user.connection = null;
|
||||
user.connection = new UserConnection(machine, user);
|
||||
Print("Соединение c " + machine.getURL() + " " + user.login + " успешно установлено.");
|
||||
user.connection.ShellCommand("ulimit -s unlimited"); // нужно, для запуска сишной части.
|
||||
} catch (Exception ex) {
|
||||
Global.Log.PrintException(ex);
|
||||
user.connection = null;
|
||||
Print("Не удалось установить соединение.");
|
||||
}
|
||||
return user.connection != null;
|
||||
//---
|
||||
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() {
|
||||
}
|
||||
protected abstract void TestsSynchronize();
|
||||
protected abstract void PackageWorkspaceCreation();
|
||||
protected abstract void AnalyseResults();
|
||||
protected abstract void PackageStart();
|
||||
protected abstract boolean CheckNextState();
|
||||
protected abstract void DownloadResults();
|
||||
protected abstract void Kill();
|
||||
protected boolean Connect() {
|
||||
return true;
|
||||
}
|
||||
protected void Disconnect() {
|
||||
}
|
||||
//---
|
||||
//жизненный цикл планировщика
|
||||
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();
|
||||
UpdatePackageState(getStateAfterStart());
|
||||
EmailPackage();
|
||||
break;
|
||||
case CompilationWorkspacesCreation:
|
||||
case CompilationPreparation:
|
||||
case CompilationExecution:
|
||||
case RunningWorkspacesCreation:
|
||||
case RunningPreparation:
|
||||
case RunningExecution:
|
||||
if (CheckNextState()) UpdatePackage();
|
||||
break;
|
||||
case RunningEnd:
|
||||
DownloadResults();
|
||||
UpdatePackageState(TasksPackageState.Analysis);
|
||||
break;
|
||||
}
|
||||
}
|
||||
// ---
|
||||
public void Perform() {
|
||||
Vector<String> emails = new Vector<>();
|
||||
while (true) {
|
||||
emails.clear();
|
||||
try {
|
||||
File[] accountsBases_ = Global.DataDirectory.listFiles(pathname ->
|
||||
pathname.isFile() &&
|
||||
Utils.getExtension(pathname).equals("sqlite") &&
|
||||
!Utils.getNameWithoutExtension(pathname.getName()).isEmpty() &&
|
||||
!pathname.getName().equals(tests_db_name + ".sqlite")
|
||||
);
|
||||
if (accountsBases_ != null) {
|
||||
for (File accountBase : accountsBases_) {
|
||||
String fileName = accountBase.getName();
|
||||
String account_email = accountBase.getName().substring(0, fileName.lastIndexOf('_'));
|
||||
emails.add(account_email);
|
||||
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();
|
||||
}
|
||||
}
|
||||
for (String current_email : emails)
|
||||
emailPass(current_email);
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
Utils.sleep(getSleepMillis());
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
Utils.sleep(getSleepMillis());
|
||||
}
|
||||
}
|
||||
}
|
||||
protected int getSleepMillis() {
|
||||
return 2000;
|
||||
}
|
||||
void emailPass(String email_in) {
|
||||
email = email_in;
|
||||
try {
|
||||
// System.out.println(email+" testing planner starts...");
|
||||
Pair<TasksPackage, LinkedHashMap<Long, TestCompilationTask>> p = (Pair<TasksPackage, LinkedHashMap<Long, TestCompilationTask>>) ServerCommand(ServerCode.GetFirstActiveAccountPackage);
|
||||
sapforTasksPackage = (SapforTasksPackage) ServerCommand(ServerCode.GetFirstActiveSapforTasksPackage);
|
||||
tasksPackage = null;
|
||||
packageTasks = null;
|
||||
tasksPackage = p.getKey();
|
||||
packageTasks = p.getValue();
|
||||
if (tasksPackage != null) {
|
||||
// System.out.println("found dvm package: "+sapforTasksPackage.id);
|
||||
String machine_url = tasksPackage.machine_address + ":" + tasksPackage.machine_port;
|
||||
if (!machines.containsKey(machine_url))
|
||||
machines.put(machine_url, new Machine(
|
||||
tasksPackage.machine_name,
|
||||
tasksPackage.machine_address,
|
||||
tasksPackage.machine_port,
|
||||
tasksPackage.machine_type));
|
||||
if (!users.containsKey(tasksPackage.user_name))
|
||||
users.put(tasksPackage.user_name,
|
||||
new User(tasksPackage.user_name, tasksPackage.user_password, tasksPackage.user_workspace));
|
||||
machine = machines.get(machine_url);
|
||||
//-->>
|
||||
user = users.get(tasksPackage.user_name);
|
||||
if (CheckConnection(machine, user)) {
|
||||
try {
|
||||
supervisor = new TestsSupervisor_2022(this, user.connection, tasksPackage, new Vector<>(packageTasks.values()));
|
||||
supervisor.Perform();
|
||||
} catch (Exception ex) {
|
||||
Print("Ошибка сеанса.");
|
||||
Print(ex.getMessage());
|
||||
}
|
||||
finally {
|
||||
if (user.connection != null) {
|
||||
user.connection.Disconnect();
|
||||
user.connection = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (sapforTasksPackage != null) {
|
||||
System.out.println("found sapfor package: " + sapforTasksPackage.id + " state = " + sapforTasksPackage.state);
|
||||
try {
|
||||
(new SapforTasksPackageSupervisor(this, sapforTasksPackage)).Perform();
|
||||
} catch (Exception ex) {
|
||||
Print("Исключение при тестировании SAPROR");
|
||||
Print(ex.getMessage());
|
||||
}
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
Global.Log.PrintException(ex);
|
||||
}
|
||||
}
|
||||
public String getStarter() {
|
||||
return String.join("/", user.workspace, ConnectionPass.modules, ConnectionPass.starter);
|
||||
}
|
||||
public String getLauncher() {
|
||||
return String.join("/", user.workspace, ConnectionPass.modules, ConnectionPass.launcher);
|
||||
}
|
||||
public String getPlanner() {
|
||||
return String.join("/", user.workspace, ConnectionPass.modules, ConnectionPass.planner);
|
||||
}
|
||||
//--
|
||||
public void UpdateSapforPackage() throws Exception {
|
||||
sapforTasksPackage.ChangeDate = new Date().getTime();
|
||||
EmailMessage message = null;
|
||||
ServerCommand(ServerCode.EditAccountObject, sapforTasksPackage);
|
||||
if (sapforTasksPackage.needsEmail == 1) {
|
||||
switch (sapforTasksPackage.state) {
|
||||
case RunningExecution:
|
||||
case Aborted:
|
||||
case Done:
|
||||
//результаты.
|
||||
message = new EmailMessage();
|
||||
message.subject = "Состояние пакета задач SAPFOR" + Utils.Brackets(sapforTasksPackage.id) + " изменилось на " + Utils.Brackets(sapforTasksPackage.state.getDescription());
|
||||
message.text = sapforTasksPackage.summary;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (message != null) {
|
||||
message.targets.add(email);
|
||||
ServerCommand(ServerCode.Email, message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user