Skip to content

Commit

Permalink
use python script, discard the bat scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
ZihengSun committed Feb 13, 2024
1 parent 8696767 commit 1129b02
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 11 deletions.
76 changes: 67 additions & 9 deletions pygeoweaver/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import socket
import subprocess
import webbrowser
import time
import requests
from pygeoweaver.constants import GEOWEAVER_DEFAULT_ENDPOINT_URL
from pygeoweaver.jdk_utils import check_java
from pygeoweaver.utils import (
Expand All @@ -23,15 +25,75 @@
logger = get_logger(__name__)


def start_on_windows():

# Stop running Geoweaver if any
print("Stop running Geoweaver if any..")
subprocess.run(["taskkill", "/f", "/im", "geoweaver.exe"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)

# Get the user's home directory
home_dir = os.path.expanduser("~")

# Check if Java command exists in the system PATH
print("Check java exists..")
java_cmd = "java"
try:
subprocess.run(["where", "java"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=True)
except subprocess.CalledProcessError:
# Java command not found in PATH, check JDK folder in home directory
jdk_home = os.path.join(home_dir, "jdk", "jdk-11.0.18+10") # Change this to your JDK installation directory
print("Check jdk_home", jdk_home)
java_cmd = os.path.join(jdk_home, "bin", "java.exe")
if not os.path.exists(java_cmd):
print("Java command not found.")
exit(1)

print("Start Geoweaver..")
geoweaver_jar = os.path.join(home_dir, "geoweaver.jar")
print(f'"{java_cmd}" -jar "{geoweaver_jar}"')
subprocess.Popen([java_cmd, "-jar", geoweaver_jar], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, creationflags=subprocess.CREATE_NEW_CONSOLE)

status = 0
counter = 0
max_attempts = 20
retry_delay = 2
while counter < max_attempts:
time.sleep(retry_delay)
counter += 1
try:
response = requests.get("http://localhost:8070/Geoweaver", allow_redirects=False)
if response.status_code == 302:
log_file = os.path.join(home_dir, "geoweaver.log")
# Ensure the log file exists, create it if it doesn't
if not os.path.exists(log_file):
open(log_file, "a").close() # Create an empty file if it doesn't exist

# Now you can safely open the log file for reading
with open(log_file, "r") as f:
print(f.read())
print("Success: Geoweaver is up")
exit(0)
except Exception as e:
# print(f"Error occurred during request: {e}")
continue

print("Error: Geoweaver is not up")
exit(1)


def stop_on_windows():
print("Stopping Geoweaver...")
subprocess.run(["taskkill", "/f", "/im", "java.exe"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
print("Geoweaver stopped successfully.")


def start(force=False):
download_geoweaver_jar(overwrite=force)
check_java()

if check_os() == 3:
logger.debug(f"Detected Windows, running {get_module_absolute_path()}/start.bat")
subprocess.run(
[f"{get_module_absolute_path()}/start.bat"], cwd=f"{get_root_dir()}/"
)
logger.debug(f"Detected Windows, running start python script..")
start_on_windows()
else:
logger.debug(f"Detected Linux/MacOs, running {get_module_absolute_path()}/start.sh")
subprocess.run(
Expand All @@ -42,11 +104,7 @@ def start(force=False):
def stop():
check_java()
if check_os() == 3:
subprocess.run(
[f"{get_module_absolute_path()}/stop.bat"],
cwd=f"{get_root_dir()}/",
shell=True,
)
stop_on_windows()
else:
subprocess.run(
[f"{get_module_absolute_path()}/stop.sh"],
Expand Down
1 change: 1 addition & 0 deletions pygeoweaver/start.bat
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ where java > nul 2>&1
if %ERRORLEVEL% NEQ 0 (
rem Java command not found in PATH, check JDK folder in home directory
set "jdk_home=%home_dir%\jdk\jdk-11.0.18+10" REM Change this to your JDK installation directory
echo Check jdk_home %jdk_home%
if exist "%jdk_home%\bin\java.exe" (
set "java_cmd=%jdk_home%\bin\java.exe"
echo Java command found in JDK directory: %java_cmd%
Expand Down
2 changes: 1 addition & 1 deletion pygeoweaver/stop.bat
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

echo Stopping Geoweaver...

taskkill /f /im javaw.exe > nul
taskkill /f /im java.exe > nul

echo Geoweaver stopped successfully.
2 changes: 1 addition & 1 deletion pygeoweaver/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def download_geoweaver_jar(overwrite=False):
subprocess.run(
["chmod", "+x", get_geoweaver_jar_path()], cwd=f"{get_root_dir()}/"
)
return
return

print("Downloading latest version of Geoweaver...")
geoweaver_url = (
Expand Down

0 comments on commit 1129b02

Please sign in to comment.