Оптимизация команды Exists для SSH
This commit is contained in:
@@ -6,14 +6,13 @@ import GlobalData.User.User;
|
||||
import Repository.EmailMessage;
|
||||
import Repository.Server.ServerCode;
|
||||
import Repository.Server.ServerExchangeUnit_2021;
|
||||
import TestingSystem.DVM.UserConnection;
|
||||
import TestingSystem.SAPFOR.SapforTasksPackage.SapforTasksPackage;
|
||||
import TestingSystem.SAPFOR.SapforTasksPackageSupervisor.SapforTasksPackageSupervisor;
|
||||
import TestingSystem.DVM.Tasks.TestCompilationTask;
|
||||
import TestingSystem.DVM.Tasks.TestTask;
|
||||
import TestingSystem.DVM.TasksPackage.TasksPackage;
|
||||
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;
|
||||
@@ -39,10 +38,6 @@ public class TestingPlanner {
|
||||
//----------
|
||||
SapforTasksPackage sapforTasksPackage = null;
|
||||
//----------
|
||||
public void UpdateTask(TestTask task_in) throws Exception {
|
||||
task_in.ChangeDate = new Date().getTime();
|
||||
ServerCommand(ServerCode.EditAccountObject, task_in);
|
||||
}
|
||||
public void UpdatePackage() throws Exception {
|
||||
tasksPackage.ChangeDate = new Date().getTime();
|
||||
ServerCommand(ServerCode.EditAccountObject, tasksPackage);
|
||||
|
||||
@@ -12,7 +12,6 @@ import TestingSystem.DVM.Tasks.TestRunTask;
|
||||
import TestingSystem.DVM.Tasks.TestTask;
|
||||
import TestingSystem.DVM.TasksPackage.TasksPackage;
|
||||
import TestingSystem.DVM.TasksPackage.TasksPackageState;
|
||||
import Visual_DVM_2021.Passes.All.UnzipFolderPass;
|
||||
import com.jcraft.jsch.ChannelSftp;
|
||||
import javafx.util.Pair;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
@@ -140,11 +139,12 @@ public class TestsSupervisor_2022 {
|
||||
);
|
||||
connection.ShellCommand(plannerStartCommand);
|
||||
RemoteFile PID = new RemoteFile(packageRemoteWorkspace, "PID");
|
||||
while (!connection.Exists(packageRemoteWorkspace.full_name, "STARTED")) {
|
||||
RemoteFile STARTED = new RemoteFile(packageRemoteWorkspace, "STARTED");
|
||||
while (!connection.Exists(STARTED)) {
|
||||
System.out.println("waiting for package start...");
|
||||
Utils.sleep(1000);
|
||||
}
|
||||
if (connection.Exists(packageRemoteWorkspace.full_name, "PID")) {
|
||||
if (connection.Exists(PID)) {
|
||||
tasksPackage.pid = connection.readFromFile(PID);
|
||||
}
|
||||
}
|
||||
@@ -170,13 +170,15 @@ public class TestsSupervisor_2022 {
|
||||
}
|
||||
public void DownloadResults() throws Exception {
|
||||
Utils.CheckDirectory(packageLocalWorkspace);
|
||||
/*
|
||||
RemoteFile remote_results_archive = new RemoteFile(packageRemoteWorkspace.full_name, "results.zip");
|
||||
File results_archive = new File(packageLocalWorkspace, "results.zip");
|
||||
if (connection.Exists(packageRemoteWorkspace.full_name, "results.zip")) {
|
||||
connection.getSingleFile(remote_results_archive, results_archive, 0);
|
||||
if (connection.Exists()) {
|
||||
connection.getSingleFile(remote_results_archive, results_archive);
|
||||
UnzipFolderPass unzipFolderPass = new UnzipFolderPass();
|
||||
unzipFolderPass.Do(results_archive.getAbsolutePath(), packageLocalWorkspace.getAbsolutePath(), false);
|
||||
}
|
||||
*/
|
||||
}
|
||||
public void AnalyseResults() throws Exception {
|
||||
System.out.println("analysing results");
|
||||
@@ -248,7 +250,7 @@ public class TestsSupervisor_2022 {
|
||||
if (stateFile.exists()) {
|
||||
String stateText = FileUtils.readFileToString(stateFile, Charset.defaultCharset()).replace("\n", "");
|
||||
testTask.state = TaskState.valueOf(stateText);
|
||||
} else testTask.state = TaskState.InternalError; //поменять на то что состояние не найдено. ?
|
||||
} else testTask.state = TaskState.InternalError;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
||||
@@ -7,10 +7,7 @@ import GlobalData.Machine.Machine;
|
||||
import GlobalData.RemoteFile.RemoteFile;
|
||||
import GlobalData.User.User;
|
||||
import Visual_DVM_2021.Passes.PassException;
|
||||
import com.jcraft.jsch.ChannelSftp;
|
||||
import com.jcraft.jsch.ChannelShell;
|
||||
import com.jcraft.jsch.JSch;
|
||||
import com.jcraft.jsch.Session;
|
||||
import com.jcraft.jsch.*;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
@@ -108,8 +105,8 @@ public class UserConnection {
|
||||
System.gc();
|
||||
}
|
||||
//--
|
||||
|
||||
//из за мусора результатом пользоваться в общем случае невозможно.
|
||||
//todo из за мусора результатом пользоваться в общем случае невозможно.
|
||||
//следует перенаправлять вывод в какой нибудь временный файл на сервере.
|
||||
public String ShellCommand(String command) throws Exception {
|
||||
StringBuilder result = new StringBuilder();
|
||||
// System.out.println("command=" + Utils.Brackets(command));
|
||||
@@ -122,43 +119,26 @@ public class UserConnection {
|
||||
// System.out.println("res="+Utils.Brackets(res));
|
||||
return (data.length > 0) ? data[data.length - 1] : result.toString();
|
||||
}
|
||||
//--
|
||||
//тут имя файла короткое.
|
||||
public boolean Exists(String folder, String name) throws Exception {
|
||||
Vector<ChannelSftp.LsEntry> files = sftpChannel.ls(folder);
|
||||
for (ChannelSftp.LsEntry file : files) {
|
||||
file.getAttrs().getSize();
|
||||
if (file.getFilename().equals(name)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
//--
|
||||
public void getSingleFile(String src, String dst) throws Exception {
|
||||
sftpChannel.get(src, dst);
|
||||
}
|
||||
public long getFileKBSize(String path) throws Exception{
|
||||
public long getFileKBSize(String path) throws Exception {
|
||||
long size = sftpChannel.lstat(path).getSize();
|
||||
return size/1024;
|
||||
return size / 1024;
|
||||
}
|
||||
public boolean getSingleFile(RemoteFile src, File dst, int maxSize) throws Exception {
|
||||
if (Exists(src.parent, src.name)) {
|
||||
if ((maxSize == 0) || getFileKBSize(src.full_name) <= maxSize) {
|
||||
getSingleFile(src.full_name, dst.getAbsolutePath());
|
||||
return true;
|
||||
} else {
|
||||
Utils.WriteToFile(dst, "Размер файла превышает " + maxSize + " KB.\n" + "Файл не загружен. Его можно просмотреть на машине по адресу\n" + Utils.Brackets(src.full_name));
|
||||
}
|
||||
public void getSingleFileWithMaxSize(RemoteFile src, File dst, int maxSize) throws Exception {
|
||||
if ((maxSize == 0) || getFileKBSize(src.full_name) <= maxSize) {
|
||||
getSingleFile(src.full_name, dst.getAbsolutePath());
|
||||
} else {
|
||||
Utils.WriteToFile(dst, "Размер файла превышает " + maxSize + " KB.\n" + "Файл не загружен. Его можно просмотреть на машине по адресу\n" + Utils.Brackets(src.full_name));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
public void putSingleFile(File src, RemoteFile dst) throws Exception {
|
||||
sftpChannel.put(src.getAbsolutePath(), dst.full_name);
|
||||
}
|
||||
//-
|
||||
public void MKDIR(RemoteFile dir) throws Exception {
|
||||
if (!Exists(dir.parent, dir.name)) sftpChannel.mkdir(dir.full_name);
|
||||
if (!Exists(dir)) sftpChannel.mkdir(dir.full_name);
|
||||
}
|
||||
//-
|
||||
public void RMDIR(String dir) throws Exception {
|
||||
@@ -210,12 +190,6 @@ public class UserConnection {
|
||||
}
|
||||
}
|
||||
}
|
||||
/*
|
||||
public void copy(RemoteFile src, RemoteFile dst) throws Exception {
|
||||
ShellCommand("cp " + Utils.DQuotes(src.full_name) + " " + Utils.DQuotes(dst.full_name));
|
||||
}
|
||||
*/
|
||||
//-------
|
||||
public void writeToFile(String text, RemoteFile dst) throws Exception {
|
||||
sftpChannel.put(new ByteArrayInputStream(text.getBytes(StandardCharsets.UTF_8)), dst.full_name);
|
||||
sftpChannel.chmod(0777, dst.full_name);
|
||||
@@ -225,4 +199,22 @@ public class UserConnection {
|
||||
sftpChannel.get(src.full_name, outputStream);
|
||||
return outputStream.toString(StandardCharsets.UTF_8.name());
|
||||
}
|
||||
//--
|
||||
public boolean Exists(String file_full_name) throws Exception {
|
||||
try {
|
||||
sftpChannel.lstat(file_full_name);
|
||||
return true;
|
||||
} catch (SftpException e) {
|
||||
if (e.id == ChannelSftp.SSH_FX_NO_SUCH_FILE) {
|
||||
// file doesn't exist
|
||||
return false;
|
||||
} else {
|
||||
// something else went wrong
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
public boolean Exists(RemoteFile file) throws Exception {
|
||||
return Exists(file.full_name);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user