package TestingSystem.Common; import Common.Constants; import Common.Utils.Utils; import Repository.EmailMessage; import Repository.Server.ServerCode; import TestingSystem.Common.TestingPackage.TestingPackage; import TestingSystem.Common.TestingPackageToKill.TestingPackageToKill; import Repository.RepositoryClient; import Visual_DVM_2021.Passes.PassException; import javafx.util.Pair; import java.util.Date; import java.util.Vector; public abstract class TestingPlanner

extends RepositoryClient { protected P testingPackage; //--- 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 Aborted: case CompilationExecution: case RunningExecution: EmailPackage(); break; } } void UpdatePackage() throws Exception { testingPackage.ChangeDate = new Date().getTime(); ServerCommand(ServerCode.EditObject, testingPackage); } void EmailPackage() throws Exception { if (testingPackage.needsEmail == 1) { 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 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 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; protected boolean Connect() { return true; } protected void Disconnect() { } protected void MachineConnectionError() { } // --- 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); MachineConnectionError(); } else { //-- InitSessionCredentials(); if (testingPackage.state.equals(TasksPackageState.Analysis)) { AnalyseResults(); UpdatePackageState(TasksPackageState.Done); } else { try { if (Connect()) { int ptk_id = (int) ServerCommand(getCheckIfNeedsKillCode(), testingPackage.id); if (ptk_id != Constants.Nan) { Print("package " + testingPackage.id + " NEEDS TO KILL"); Kill(); UpdatePackageState(TasksPackageState.Aborted); ServerCommand(ServerCode.DeleteObjectByPK, new Pair(TestingPackageToKill.class, ptk_id)); } else { //-- 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(); } } catch (Exception ex) { Print("Ошибка сеанса. Соединение будет разорвано."); ex.printStackTrace(); Print(ex.getMessage()); // testingPackage.connectionErrosCount++; UpdatePackage(); } finally { Disconnect(); } } } //-- testingPackage.destructor(); testingPackage = null; System.gc(); //-- } @Override public void perform() throws Exception { testingPackage = null; Vector

activePackages = (Vector

) ServerCommand(getActivePackagesCode()); for (P activePackage : activePackages) PerformPackage(activePackage); } }