Skip to content

Commit

Permalink
fix the multi user logging out problem
Browse files Browse the repository at this point in the history
  • Loading branch information
ZihengSun committed Oct 22, 2023
1 parent 67a28f4 commit 390685b
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 67 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</parent>
<groupId>com.gw</groupId>
<artifactId>geoweaver</artifactId>
<version>1.2.0</version>
<version>1.2.1</version>
<name>geoweaver</name>
<description>A lightweight workflow management software for organizing data analysis workflows,
preserving history of every workflow run, and improving scientist producitvity and workflow FAIRness,
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/com/gw/GeoweaverApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,6 @@ public static void addDefaultPublicUser(){

}

logger.debug("test what is going on");

//set everything that doesn't have an owner to this user
ut.belongToPublicUser();

Expand Down
6 changes: 4 additions & 2 deletions src/main/java/com/gw/local/LocalSessionNixImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

Expand Down Expand Up @@ -93,6 +94,7 @@ public class LocalSessionNixImpl implements LocalSession {
public LocalSessionNixImpl() {

//this is for spring
log.info("localsessionniximpl object is created for " + token);

}

Expand All @@ -112,10 +114,10 @@ public void initHistory(String history_id, String script, String processid, bool
this.isClose = false;

history = history_tool.initProcessHistory(history_id, processid, script);

// this.sender = createLocalSessionOutput();

}



/**
* If the process ends with error
Expand Down
42 changes: 20 additions & 22 deletions src/main/java/com/gw/local/LocalSessionOutput.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.gw.local;

import java.io.BufferedReader;
import java.io.IOException;

import javax.websocket.Session;

Expand Down Expand Up @@ -73,7 +74,7 @@ public LocalSessionOutput() {
* @param jupyterfilepath The Jupyter file path, if applicable.
*/
public void init(BufferedReader in, String token, String history_id, String lang, String jupyterfilepath) {
log.info("LocalSessionOutput created");
log.info("LocalSessionOutput created for token "+ token);
this.in = in;
this.token = token;
this.run = true;
Expand All @@ -96,21 +97,19 @@ public void stop() {
* @param msg The message to be sent to the WebSocket.
*/
public void sendMessage2WebSocket(String msg) {
if (!BaseTool.isNull(wsout)) {
synchronized (wsout) {
try {
if (wsout.isOpen()) {
wsout.getBasicRemote().sendText(msg);
} else {
log.debug("WebSocket is closed, message didn't send: " + msg);
}
} catch (Exception e) {
e.printStackTrace();
log.debug("Exception happens, message didn't send: " + msg);
try {
this.wsout = CommandServlet.findSessionById(token);
if (!BaseTool.isNull(wsout)){
if(wsout.isOpen()) {
wsout.getBasicRemote().sendText(msg);
}else{
CommandServlet.removeSessionById(token);
}
}else{
log.warn(String.format("cannot find websocket for token %s", token));
}
} else {
log.debug("WebSocket is null, message didn't send: " + msg);
} catch (IOException e1) {
e1.printStackTrace();
}
}

Expand Down Expand Up @@ -162,7 +161,7 @@ public void endWithCode(String token, int exitvalue) {

ht.saveHistory(h);

CommandServlet.sendMessageToSocket(token, "Exit Code: " + exitvalue);
this.sendMessage2WebSocket("Exit Code: " + exitvalue);
}

/**
Expand Down Expand Up @@ -248,7 +247,7 @@ public void run() {
this.updateStatus("Running", "Running");

// Send a message to the WebSocket indicating that the process has started
CommandServlet.sendMessageToSocket(token, "Process " + this.history_id + " Started");
this.sendMessage2WebSocket("Process " + this.history_id + " Started");

String line = null; // Initialize a variable to store each line of output

Expand All @@ -265,7 +264,7 @@ public void run() {
}

linenumber++; // Increment the line number

if(linenumber%1==0){
this.updateStatus(logs.toString(), "Running");
}
Expand Down Expand Up @@ -297,16 +296,15 @@ public void run() {
} else if (line.contains("==== Geoweaver Bash Output Finished ====")) {
// Handle specific marker lines if present
} else {
log.info("Local thread output >> " + line); // Log each line of output
log.info("Local thread output >> " + line + " - token: " + token); // Log each line of output
logs.append(line).append("\n"); // Append the line to the logs

if (!BaseTool.isNull(wsout) && wsout.isOpen()) {
if (prelog.toString() != null) {
line = prelog.toString() + line;
prelog = new StringBuffer();
}
//this.sendMessage2WebSocket(line); // Send the line to the WebSocket
CommandServlet.sendMessageToSocket(token, line);
this.sendMessage2WebSocket(line);
} else {
prelog.append(line).append("\n"); // Append to the prelog if WebSocket isn't available
}
Expand Down Expand Up @@ -344,7 +342,7 @@ public void run() {
}

// Send a message to the WebSocket indicating that the process has finished
CommandServlet.sendMessageToSocket(token, "The process " + history_id + " is finished.");
this.sendMessage2WebSocket("The process " + history_id + " is finished.");

// This thread will end by itself when the task is finished; you don't have to close it manually
GeoweaverController.sessionManager.closeByToken(token); // Close the session by token
Expand All @@ -355,7 +353,7 @@ public void run() {
// Depending on the language, update the status to "Failed"
this.updateStatus(logs.toString() + "\n" + e.getLocalizedMessage(), "Failed");
} finally {
CommandServlet.sendMessageToSocket(token, "======= Process " + this.history_id + " ended");
this.sendMessage2WebSocket("======= Process " + this.history_id + " ended");
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/gw/server/CommandServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public static javax.websocket.Session findSessionById(String token) {
* @param token The token of the target WebSocket session.
* @param message The message to be sent.
*/
public static void sendMessageToSocket(String token, String message) {
public static Session sendMessageToSocket(String token, String message) {
try {
Session wsout = CommandServlet.findSessionById(token);
if (!BaseTool.isNull(wsout)){
Expand All @@ -253,9 +253,11 @@ public static void sendMessageToSocket(String token, String message) {
}else{
logger.warn(String.format("cannot find websocket for token %s", token));
}
return wsout;
} catch (IOException e1) {
e1.printStackTrace();
}
return null;
}

/**
Expand Down
24 changes: 12 additions & 12 deletions src/main/java/com/gw/tools/ExecutionTool.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.gw.tools;

import com.gw.utils.BaseTool;
import com.gw.utils.BeanTool;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

Expand All @@ -14,20 +16,8 @@
@Scope("prototype")
public class ExecutionTool {

@Autowired
ProcessTool pt;

@Autowired
LocalhostTool lt;

@Autowired
RemotehostTool rt;

@Autowired
BaseTool bt;

@Autowired
EnvironmentTool envt;

@Autowired
TaskManager tm;
Expand All @@ -51,6 +41,8 @@ public String executeProcess(String history_id, String id, String hid, String ps

String bin = null, pyenv = null, basedir = null;

EnvironmentTool envt = BeanTool.getBean(EnvironmentTool.class);

Environment envobj = envt.getEnvironmentById(envid);

if(!BaseTool.isNull(envobj)){
Expand Down Expand Up @@ -85,6 +77,7 @@ public String executeProcess(String history_id, String id, String hid, String ps
public String executeProcess(String history_id, String id, String hid, String pswd, String httpsessionid,
boolean isjoin, String bin, String pyenv, String basedir) {

ProcessTool pt = BeanTool.getBean(ProcessTool.class);

String category = pt.getTypeById(id);

Expand All @@ -95,6 +88,8 @@ public String executeProcess(String history_id, String id, String hid, String ps
if(BaseTool.isNull(basedir)) basedir = "~";

if(bt.islocal(hid)) {

LocalhostTool lt = BeanTool.getBean(LocalhostTool.class);

//localhost
if("shell".equals(category)) {
Expand Down Expand Up @@ -123,6 +118,7 @@ public String executeProcess(String history_id, String id, String hid, String ps
}else {

//non-local remote server
RemotehostTool rt = BeanTool.getBean(RemotehostTool.class);

if("shell".equals(category)) {

Expand Down Expand Up @@ -166,10 +162,14 @@ public String readEnvironment(String hid, String password){

if(bt.islocal(hid)){

LocalhostTool lt = BeanTool.getBean(LocalhostTool.class);

resp = lt.readPythonEnvironment(hid, password);

}else{

RemotehostTool rt = BeanTool.getBean(RemotehostTool.class);

resp = rt.readPythonEnvironment(hid, password);

}
Expand Down
11 changes: 3 additions & 8 deletions src/main/java/com/gw/tools/LocalhostTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.gw.tasks.GeoweaverProcessTask;
import com.gw.tasks.TaskManager;
import com.gw.utils.BaseTool;
import com.gw.utils.BeanTool;
import com.gw.utils.OSValidator;
import com.gw.web.GeoweaverController;

Expand Down Expand Up @@ -62,12 +63,6 @@ public class LocalhostTool {
@Autowired
ProcessRepository processrepository;

@Autowired
LocalSessionNixImpl nixsession;

@Autowired
LocalSessionWinImpl winsession;

public void saveHistory(String processid, String script, String history_id){

History history = histool.getHistoryById(history_id);
Expand Down Expand Up @@ -182,11 +177,11 @@ public LocalSession getLocalSession() {

if(OSValidator.isWindows()) {

session = winsession;
session = BeanTool.getBean(LocalSessionWinImpl.class);

}else if(OSValidator.isMac() || OSValidator.isUnix()) {

session = nixsession;
session = BeanTool.getBean(LocalSessionNixImpl.class);

}else {

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/gw/tools/WorkflowTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ public String execute(String history_id, String wid, String mode,
try {

task.initialize(history_id, wid, mode, hosts, pswds, envs, token);

task.execute();

resp = "{\"history_id\": \""+task.getHistory_id()+
Expand Down
53 changes: 36 additions & 17 deletions src/main/java/com/gw/utils/BeanTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,45 @@
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
* BeanTool is a utility class designed to interact with Spring Application Context for bean management.
* It provides methods to access beans and the application context within a Spring application.
*/
@Component
public class BeanTool implements ApplicationContextAware {

private static ApplicationContext context;

// The application context is set by Spring when this class is ApplicationContextAware.
private static ApplicationContext context;

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

context = applicationContext;

}

public static ApplicationContext getApplicationContext() {
return context;
}

public static <T extends Object> T getBean(Class<T> beanClass) {

return context.getBean(beanClass);
/**
* Called by Spring to set the application context for this class.
*
* @param applicationContext The Spring application context.
* @throws BeansException If an error occurs while setting the application context.
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
context = applicationContext;
}


/**
* Get the current Spring Application Context.
*
* @return The Spring Application Context.
*/
public static ApplicationContext getApplicationContext() {
return context;
}

/**
* Get a bean of the specified class from the Spring Application Context.
*
* @param <T> The type of the bean to retrieve.
* @param beanClass The class of the bean to retrieve.
* @return An instance of the specified bean class.
*/
public static <T extends Object> T getBean(Class<T> beanClass) {
return context.getBean(beanClass);
}
}

2 changes: 1 addition & 1 deletion src/main/resources/static/js/gw.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ edu = {

sponsor: "ESIPLab incubator project, NASA ACCESS project, NSF Geoinformatics project, NSF Cybertraining project",

version: "1.2.0",
version: "1.2.1",

author: "open source contributors",

Expand Down

0 comments on commit 390685b

Please sign in to comment.