рефакторинг серверной части сапфора.

This commit is contained in:
2023-12-16 03:57:01 +03:00
parent b7b41ae59c
commit 34c08e7d44
32 changed files with 942 additions and 355 deletions

View File

@@ -32,9 +32,11 @@ import TestingSystem.DVM.TasksPackage.TasksPackageState;
import TestingSystem.DVM.UserConnection;
import TestingSystem.SAPFOR.SapforConfiguration.SapforConfiguration;
import TestingSystem.SAPFOR.SapforConfigurationCommand.SapforConfigurationCommand;
import TestingSystem.SAPFOR.SapforPackage.SapforPackage;
import TestingSystem.SAPFOR.SapforTask.SapforTask;
import TestingSystem.SAPFOR.SapforTasksPackage.SapforPackageData;
import TestingSystem.SAPFOR.SapforTasksPackage.SapforTasksPackage;
import TestingSystem.SAPFOR.SapforTestingPlanner;
import TestingSystem.SAPFOR.ServerSapfor.ServerSapfor;
import Visual_DVM_2021.Passes.All.DownloadRepository;
import Visual_DVM_2021.Passes.All.ZipFolderPass;
@@ -50,6 +52,149 @@ import java.util.*;
import static Common.Constants.tests_db_name;
public class TestingServer extends RepositoryServer<TestsDatabase> {
@Override
public void beforePublishAction(DBObject object) throws Exception {
if (object instanceof TasksPackage) {
TasksPackage tasksPackage = (TasksPackage) object;
tasksPackage.id = db.IncKey(SettingName.DVMPackageMaxId);
} else if (object instanceof SapforTasksPackage) {
SapforTasksPackage sapforTasksPackage = (SapforTasksPackage) object;
sapforTasksPackage.id = db.IncKey(SettingName.SAPFORPackageId);
}
}
@Override
public void afterPublishAction(DBObject object) throws Exception {
if (object instanceof TasksPackage) {
//объект уже вставлен.
TasksPackage tasksPackage = (TasksPackage) object;
//-
for (int group_id : tasksPackage.sorted_tasks.keySet()) {
if (db.groups.containsKey(group_id)) {
Group group = db.groups.get(group_id);
LinkedHashMap<Integer, Vector<TestCompilationTask>> group_tasks = tasksPackage.sorted_tasks.get((group_id));
for (int test_id : group_tasks.keySet()) {
if (db.tests.containsKey(test_id)) {
Test test = db.tests.get(test_id);
//---
for (TestCompilationTask task : group_tasks.get(test_id)) {
Print("принять задачу на компиляцию " + group_id + ":" + test_id + ":" + task.flags);
//Теперь эту задачу надо поставить в очередь. и вернуть пользователю, уже с id
task.state = TaskState.Waiting;
task.id = db.IncKey(SettingName.TaskMaxId);
task.taskspackage_id = tasksPackage.id;
task.makefile_text = group.GenerateMakefile(test, tasksPackage.dvm_drv, task.flags);
task.test_home = tasksPackage.user_workspace + "/projects/" + test_id;
//-->>
task.remote_workspace =
new RemoteFile(
tasksPackage.user_workspace + "/tests/" + tasksPackage.id,
String.valueOf(task.id), true).full_name;
account_db.Insert(task);
if (task.runTasks != null) {
for (TestRunTask rt : task.runTasks) {
rt.id = db.IncKey(SettingName.TaskMaxId);
rt.taskspackage_id = tasksPackage.id;
rt.testcompilationtask_id = task.id;
rt.remote_workspace =
new RemoteFile(
tasksPackage.user_workspace + "/tests/" + tasksPackage.id,
String.valueOf(rt.id), true).full_name;
rt.binary_name = "spf_" + rt.id + "_" + rt.matrix.replace(" ", "_");
account_db.Insert(rt);
}
}
}
}
}
}
}
} else if (object instanceof Test) {
Test test = (Test) object;
if (!test.unpackProjectOnServer()) {
db.Delete(test);
throw new RepositoryRefuseException(
"Не удалось прикрепить проект к тесту с id " + test.id
+ "\nТест будет удален"
);
}
} else if (object instanceof DVMPackage) {
DVMPackage dvmPackage = (DVMPackage) object;
//--
Utils.CheckAndCleanDirectory(dvmPackage.getLocalWorkspace());
//--
dvmPackage.saveJson();
dvmPackage.package_json = null; // объект больше не нужен.
} else if (object instanceof SapforPackage) {
SapforPackage sapforPackage = (SapforPackage) object;
//--
Utils.CheckAndCleanDirectory(sapforPackage.getLocalWorkspace());
//--
sapforPackage.saveJson();
sapforPackage.package_json = null; // объект больше не нужен.
}
}
@Override
public void afterDeleteAction(DBObject object) throws Exception {
if (object instanceof Test) {
Test test = (Test) object;
Utils.forceDeleteWithCheck(test.getArchive());
Utils.forceDeleteWithCheck(test.getServerPath());
} else if (object instanceof Group) {
Group group = (Group) object;
Vector<Test> tests = new Vector<>();
for (Test group_test : db.tests.Data.values()) {
if (group_test.group_id == group.id)
tests.add(group_test);
}
for (Test group_test : tests) {
db.Delete(group_test);
Utils.forceDeleteWithCheck(group_test.getArchive());
Utils.forceDeleteWithCheck(group_test.getServerPath());
}
} else if (object instanceof ServerSapfor) {
Utils.forceDeleteWithCheck(
new File(
((ServerSapfor) object).home_path
)
);
} else if (object instanceof SapforTasksPackage) {
SapforTasksPackage sapforTasksPackage = (SapforTasksPackage) object;
File workspace = new File(
sapforTasksPackage.workspace
);
System.out.println(Utils.Brackets(workspace.getAbsolutePath()));
Utils.forceDeleteWithCheck(workspace);
Utils.forceDeleteWithCheck(sapforTasksPackage.getArchive());
//внешние ключи не работают
Vector<SapforTask> tasks = new Vector<>();
for (SapforTask task : account_db.sapforTasks.Data.values()) {
if (task.sapfortaskspackage_id == sapforTasksPackage.id) // todo group_name -> group_id
tasks.add(task);
}
for (SapforTask task : tasks) {
account_db.Delete(task);
}
} else if (object instanceof SapforConfiguration) {
SapforConfiguration sapforConfiguration = (SapforConfiguration) object;
Vector<SapforConfigurationCommand> commands = new Vector<>();
for (SapforConfigurationCommand command : db.sapforConfigurationCommands.Data.values()) {
if (command.sapforconfiguration_id == sapforConfiguration.id)
commands.add(command);
}
for (SapforConfigurationCommand command : commands) {
db.Delete(command);
}
} else if (object instanceof DVMPackage) {
DVMPackage dvmPackage = (DVMPackage) object;
File workspace = dvmPackage.getLocalWorkspace();
Utils.forceDeleteWithCheck(workspace);
} else if (object instanceof SapforPackage) {
SapforPackage sapforPackage = (SapforPackage) object;
File workspace = sapforPackage.getLocalWorkspace();
Utils.forceDeleteWithCheck(workspace);
}
}
//-->>>
LinkedHashMap<String, TasksDatabase> accountsBases = new LinkedHashMap<>();
//--------------------------------->>>
public TestingServer() {
@@ -109,8 +254,16 @@ public class TestingServer extends RepositoryServer<TestsDatabase> {
protected TestingPlanner_OLD testingPlannerOLD = new TestingPlanner_OLD();
protected Thread testingThread = new Thread(() -> testingPlannerOLD.Perform());
//---
protected DVMTestingPlanner DVMTestingPlanner_ = new DVMTestingPlanner();
protected Thread testingThread_2023 = new Thread(() -> DVMTestingPlanner_.Perform());
protected DVMTestingPlanner DVMTestingPlanner = new DVMTestingPlanner();
protected SapforTestingPlanner sapforTestingPlanner = new SapforTestingPlanner();
//--
protected Thread testingThread_2023 = new Thread(() -> {
while (true) {
DVMTestingPlanner.Perform();
sapforTestingPlanner.Perform();
}
});
//------>>>
//------>>>
public static Timer checkTimer = null;
public static void TimerOn() {
@@ -380,13 +533,25 @@ public class TestingServer extends RepositoryServer<TestsDatabase> {
GetFirstActiveDVMPackage();
break;
case DVMPackageNeedsKill:
Print("Проверить нуждает ли пакет DVM в убийстве");
Print("Проверить нуждается ли пакет DVM в убийстве");
DVMPackageNeedsKill();
break;
case UpdateActiveDVMPackages:
Print("Получить данные по пакетам DVM");
UpdateActiveDVMPackages();
break;
case GetFirstActiveSapforPackage:
Print("Получить первый активный пакет задач SAPFOR");
GetFirstActiveSapforPackage();
break;
case SapforPackageNeedsKill:
Print("Проверить нуждает ли пакет SAPFOR в убийстве");
SapforPackageNeedsKill();
break;
case UpdateActiveSapforPackages:
Print("Получить данные по пакетам Sapfor");
UpdateActiveSapforPackages();
break;
default:
throw new RepositoryRefuseException("Неподдерживаемый код: " + code);
}
@@ -539,137 +704,6 @@ public class TestingServer extends RepositoryServer<TestsDatabase> {
SetCurrentAccountDB(request.arg);
return account_db;
}
@Override
public void beforePublishAction(DBObject object) throws Exception {
if (object instanceof TasksPackage) {
TasksPackage tasksPackage = (TasksPackage) object;
tasksPackage.id = db.IncKey(SettingName.DVMPackageMaxId);
} else if (object instanceof SapforTasksPackage) {
SapforTasksPackage sapforTasksPackage = (SapforTasksPackage) object;
sapforTasksPackage.id = db.IncKey(SettingName.SAPFORPackageId);
}
}
@Override
public void afterPublishAction(DBObject object) throws Exception {
if (object instanceof TasksPackage) {
//объект уже вставлен.
TasksPackage tasksPackage = (TasksPackage) object;
//-
for (int group_id : tasksPackage.sorted_tasks.keySet()) {
if (db.groups.containsKey(group_id)) {
Group group = db.groups.get(group_id);
LinkedHashMap<Integer, Vector<TestCompilationTask>> group_tasks = tasksPackage.sorted_tasks.get((group_id));
for (int test_id : group_tasks.keySet()) {
if (db.tests.containsKey(test_id)) {
Test test = db.tests.get(test_id);
//---
for (TestCompilationTask task : group_tasks.get(test_id)) {
Print("принять задачу на компиляцию " + group_id + ":" + test_id + ":" + task.flags);
//Теперь эту задачу надо поставить в очередь. и вернуть пользователю, уже с id
task.state = TaskState.Waiting;
task.id = db.IncKey(SettingName.TaskMaxId);
task.taskspackage_id = tasksPackage.id;
task.makefile_text = group.GenerateMakefile(test, tasksPackage.dvm_drv, task.flags);
task.test_home = tasksPackage.user_workspace + "/projects/" + test_id;
//-->>
task.remote_workspace =
new RemoteFile(
tasksPackage.user_workspace + "/tests/" + tasksPackage.id,
String.valueOf(task.id), true).full_name;
account_db.Insert(task);
if (task.runTasks != null) {
for (TestRunTask rt : task.runTasks) {
rt.id = db.IncKey(SettingName.TaskMaxId);
rt.taskspackage_id = tasksPackage.id;
rt.testcompilationtask_id = task.id;
rt.remote_workspace =
new RemoteFile(
tasksPackage.user_workspace + "/tests/" + tasksPackage.id,
String.valueOf(rt.id), true).full_name;
rt.binary_name = "spf_" + rt.id + "_" + rt.matrix.replace(" ", "_");
account_db.Insert(rt);
}
}
}
}
}
}
}
} else if (object instanceof Test) {
Test test = (Test) object;
if (!test.unpackProjectOnServer()) {
db.Delete(test);
throw new RepositoryRefuseException(
"Не удалось прикрепить проект к тесту с id " + test.id
+ "\nТест будет удален"
);
}
} else if (object instanceof DVMPackage) {
DVMPackage dvmPackage = (DVMPackage) object;
//--
Utils.CheckAndCleanDirectory(dvmPackage.getLocalWorkspace());
//--
dvmPackage.saveJson();
dvmPackage.package_json = null; // объект больше не нужен.
}
}
@Override
public void afterDeleteAction(DBObject object) throws Exception {
if (object instanceof Test) {
Test test = (Test) object;
Utils.forceDeleteWithCheck(test.getArchive());
Utils.forceDeleteWithCheck(test.getServerPath());
} else if (object instanceof Group) {
Group group = (Group) object;
Vector<Test> tests = new Vector<>();
for (Test group_test : db.tests.Data.values()) {
if (group_test.group_id == group.id)
tests.add(group_test);
}
for (Test group_test : tests) {
db.Delete(group_test);
Utils.forceDeleteWithCheck(group_test.getArchive());
Utils.forceDeleteWithCheck(group_test.getServerPath());
}
} else if (object instanceof ServerSapfor) {
Utils.forceDeleteWithCheck(
new File(
((ServerSapfor) object).home_path
)
);
} else if (object instanceof SapforTasksPackage) {
SapforTasksPackage sapforTasksPackage = (SapforTasksPackage) object;
File workspace = new File(
sapforTasksPackage.workspace
);
System.out.println(Utils.Brackets(workspace.getAbsolutePath()));
Utils.forceDeleteWithCheck(workspace);
Utils.forceDeleteWithCheck(sapforTasksPackage.getArchive());
//внешние ключи не работают
Vector<SapforTask> tasks = new Vector<>();
for (SapforTask task : account_db.sapforTasks.Data.values()) {
if (task.sapfortaskspackage_id == sapforTasksPackage.id) // todo group_name -> group_id
tasks.add(task);
}
for (SapforTask task : tasks) {
account_db.Delete(task);
}
} else if (object instanceof SapforConfiguration) {
SapforConfiguration sapforConfiguration = (SapforConfiguration) object;
Vector<SapforConfigurationCommand> commands = new Vector<>();
for (SapforConfigurationCommand command : db.sapforConfigurationCommands.Data.values()) {
if (command.sapforconfiguration_id == sapforConfiguration.id)
commands.add(command);
}
for (SapforConfigurationCommand command : commands) {
db.Delete(command);
}
} else if (object instanceof DVMPackage) {
DVMPackage dvmPackage = (DVMPackage) object;
File workspace = new File(Global.PackagesDirectory, String.valueOf(dvmPackage.id));
Utils.forceDeleteWithCheck(workspace);
}
}
//---------------------------------------------------------------------------------------------->>>
//устарели. убрать.
void ActualizeDVMPackages() throws Exception {
@@ -736,6 +770,15 @@ public class TestingServer extends RepositoryServer<TestsDatabase> {
response.object = new DVMPackage(dvmPackage);
}
}
private void GetFirstActiveSapforPackage() throws Exception {
response = new ServerExchangeUnit_2021(ServerCode.OK);
response.object = null;
SapforPackage sapforPackage = db.getFirstActiveSapforPackage();
if (sapforPackage != null) {
//нужно вернуть копию объекта с иным адресом!!
response.object = new SapforPackage(sapforPackage);
}
}
//---
void UpdateActiveDVMPackages() throws Exception {
response = new ServerExchangeUnit_2021(ServerCode.OK);
@@ -751,6 +794,20 @@ public class TestingServer extends RepositoryServer<TestsDatabase> {
}
response.object = res;
}
private void UpdateActiveSapforPackages() {
response = new ServerExchangeUnit_2021(ServerCode.OK);
Vector<Pair<Integer, Long>> keys_pairs = (Vector<Pair<Integer, Long>>) request.object;
Vector<SapforPackage> res = new Vector<>();
//--
for (Pair<Integer, Long> p : keys_pairs) {
if (db.sapforPackages.containsKey(p.getKey())) {
SapforPackage actual = db.sapforPackages.get(p.getKey());
if (actual.ChangeDate != p.getValue())
res.add(new SapforPackage(actual));
}
}
response.object = res;
}
private void DVMPackageNeedsKill() {
response = new ServerExchangeUnit_2021(ServerCode.OK);
int packageId = (int) request.object;
@@ -763,4 +820,16 @@ public class TestingServer extends RepositoryServer<TestsDatabase> {
}
response.object = res_;
}
private void SapforPackageNeedsKill() throws Exception {
response = new ServerExchangeUnit_2021(ServerCode.OK);
int packageId = (int) request.object;
boolean res_ = false;
for (TestingPackageToKill packageToKill : db.testingPackagesToKill.Data.values()) {
if ((packageToKill.packageId == packageId) && (packageToKill.type == 1)) {
res_ = true;
break;
}
}
response.object = res_;
}
}