|
a |
|
b/src/Parser/multiner_server.py |
|
|
1 |
import subprocess |
|
|
2 |
import os |
|
|
3 |
import time |
|
|
4 |
import requests |
|
|
5 |
|
|
|
6 |
def start_multiner_server(): |
|
|
7 |
current_directory = os.getcwd() |
|
|
8 |
working_directory = "../resources/BERN2/scripts/" |
|
|
9 |
os.chdir(working_directory) |
|
|
10 |
run_path = "run_bern2.sh" |
|
|
11 |
stop_path = "stop_bern2.sh" |
|
|
12 |
print("Stopping any existing Multi-NER server instance.") |
|
|
13 |
stop_process = subprocess.Popen(["bash", stop_path], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT) |
|
|
14 |
stop_process.wait() |
|
|
15 |
print("Activating Mutli-NER Server... This can take approx. 1 minute") |
|
|
16 |
try: |
|
|
17 |
subprocess.Popen(["bash", run_path], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT) |
|
|
18 |
timeout = 120 # Adjust this value as needed |
|
|
19 |
# Define the server's URL that you want to check |
|
|
20 |
server_url = "http://localhost:8888" # Update with the actual URL |
|
|
21 |
# Wait for the server to become available or reach the timeout |
|
|
22 |
start_time = time.time() |
|
|
23 |
while True: |
|
|
24 |
try: |
|
|
25 |
# Send a request to the server to check its availability |
|
|
26 |
response = requests.get(server_url) |
|
|
27 |
response.raise_for_status() # Raises an exception for non-2xx status codes |
|
|
28 |
break # Server is available, exit the loop |
|
|
29 |
except (requests.ConnectionError, requests.HTTPError) as e: |
|
|
30 |
if time.time() - start_time >= timeout: |
|
|
31 |
print(f"Server did not become available within {timeout} seconds.") |
|
|
32 |
break # Timeout reached |
|
|
33 |
else: |
|
|
34 |
# Wait for a short time before checking again |
|
|
35 |
time.sleep(1) |
|
|
36 |
|
|
|
37 |
# Continue with other tasks |
|
|
38 |
print("Server is now available.") |
|
|
39 |
except subprocess.CalledProcessError as e: |
|
|
40 |
print(f"Error executing the script: {e}") |
|
|
41 |
os.chdir(current_directory) |
|
|
42 |
|
|
|
43 |
def stop_multiner_server(): |
|
|
44 |
current_directory = os.getcwd() |
|
|
45 |
working_directory = "../resources/BERN2/scripts/" |
|
|
46 |
os.chdir(working_directory) |
|
|
47 |
stop_path = "stop_bern2.sh" |
|
|
48 |
stop_process = subprocess.Popen(["bash", stop_path], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT) |
|
|
49 |
stop_process.wait() |
|
|
50 |
print("Multi-NER server instance terminated.") |
|
|
51 |
|
|
|
52 |
|