added background tracking process
This commit is contained in:
1
python/.gitignore
vendored
1
python/.gitignore
vendored
@@ -1,3 +1,4 @@
|
|||||||
p2prc-virtualenv/
|
p2prc-virtualenv/
|
||||||
SharedObjects/
|
SharedObjects/
|
||||||
__pycache__/
|
__pycache__/
|
||||||
|
data.json
|
||||||
|
|||||||
@@ -9,11 +9,23 @@ import subprocess
|
|||||||
import uuid
|
import uuid
|
||||||
import dacite
|
import dacite
|
||||||
import os
|
import os
|
||||||
|
import sqlalchemy
|
||||||
|
import dataclasses
|
||||||
|
from typing import Union
|
||||||
|
import schedule
|
||||||
|
import threading
|
||||||
|
import requests
|
||||||
|
|
||||||
p2prc = ctypes.CDLL("SharedObjects/p2prc.so")
|
p2prc = ctypes.CDLL("SharedObjects/p2prc.so")
|
||||||
|
|
||||||
p2prc.Init("")
|
p2prc.Init("")
|
||||||
|
|
||||||
|
# Start running schedule
|
||||||
|
schedule.run_pending()
|
||||||
|
|
||||||
|
# # Create Sqlite database to track processes
|
||||||
|
# engine=sqlalchemy.create_engine(f'sqlite:///homeserver.db')
|
||||||
|
|
||||||
# Global variable
|
# Global variable
|
||||||
# A global variable will be populated on runtime.
|
# A global variable will be populated on runtime.
|
||||||
# It is read from P2PRC directly or from a local
|
# It is read from P2PRC directly or from a local
|
||||||
@@ -108,23 +120,130 @@ class Process:
|
|||||||
Status: bool
|
Status: bool
|
||||||
DomainName: str
|
DomainName: str
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Processes:
|
||||||
|
Processes: List[Process]
|
||||||
|
|
||||||
|
PublicProcesses: Union[Processes, None] = None
|
||||||
|
|
||||||
# Initial defined functions
|
# Initial defined functions
|
||||||
# Exposed functions
|
# Exposed functions
|
||||||
def SpinProcess(process: Process):
|
def SpinProcess(process: Process):
|
||||||
# Starts P2PRC process
|
# Starts P2PRC process
|
||||||
os.system(process.CommandToRunScript)
|
os.system(process.CommandToRunScript)
|
||||||
|
|
||||||
process.ExternalAddress = P2PRCMapPort(port=process.InternalPortNo,domainname=process.DomainName,serveraddress=process.NodeInfo.ip_address.IPV4 + ":" + process.NodeInfo.ip_address.ServerPort)
|
# concats to the public exposed IP address + The Node information exposed P2PRC
|
||||||
|
# server port.
|
||||||
|
ServerAddress = process.NodeInfo.IPV4 + ":" + process.NodeInfo.ServerPort
|
||||||
|
|
||||||
|
process.ExternalAddress = P2PRCMapPort(port=process.InternalPortNo,
|
||||||
|
domainname=process.DomainName,
|
||||||
|
serveraddress=ServerAddress)
|
||||||
process.ID = str(uuid.uuid4())
|
process.ID = str(uuid.uuid4())
|
||||||
process.Status = True
|
process.Status = True
|
||||||
|
|
||||||
|
# Save the process to memory
|
||||||
|
AddProcessToMemory(process)
|
||||||
|
|
||||||
|
# Save to disk
|
||||||
|
SaveProcess()
|
||||||
|
|
||||||
return process
|
return process
|
||||||
|
|
||||||
# Kill process based on the process provided
|
# Kill process based on the process provided
|
||||||
def KillProcess(process: Process):
|
def KillProcess(process: Process):
|
||||||
os.system(process.CommandToKillScript)
|
os.system(process.CommandToKillScript)
|
||||||
process.Status = False
|
process.Status = False
|
||||||
|
|
||||||
|
# Remove the process from memory
|
||||||
|
PublicProcesses.Processes.remove(process)
|
||||||
|
|
||||||
|
# Remove from disk
|
||||||
|
SaveProcess()
|
||||||
|
|
||||||
return process
|
return process
|
||||||
|
|
||||||
|
# Saves the list of processes provided as a JSON file
|
||||||
|
def SaveProcess():
|
||||||
|
global PublicProcesses
|
||||||
|
with open('data.json', 'w+') as f:
|
||||||
|
json.dump(dataclasses.asdict(PublicProcesses), f, indent=4)
|
||||||
|
|
||||||
|
# Read saves processes
|
||||||
|
def ReadSavedProcesses():
|
||||||
|
try:
|
||||||
|
global PublicProcesses
|
||||||
|
with open('data.json', 'r') as file:
|
||||||
|
data = json.load(file)
|
||||||
|
PublicProcesses = dacite.from_dict(Processes,data)
|
||||||
|
except Exception as e:
|
||||||
|
PublicProcesses = None
|
||||||
|
|
||||||
|
# Gets called when the python file is called
|
||||||
|
ReadSavedProcesses()
|
||||||
|
|
||||||
|
# List processes
|
||||||
|
def ListProcess():
|
||||||
|
global PublicProcesses
|
||||||
|
return PublicProcesses
|
||||||
|
|
||||||
|
# Adds the process dataclass the to
|
||||||
|
# the global variable PublicProcess.
|
||||||
|
def AddProcessToMemory(process: Process):
|
||||||
|
global PublicProcesses
|
||||||
|
if PublicProcesses == None:
|
||||||
|
PublicProcesses = Processes(Processes=[process])
|
||||||
|
# PublicProcesses.Processes.append(process)
|
||||||
|
else:
|
||||||
|
PublicProcesses.Processes.append(process)
|
||||||
|
|
||||||
|
# Tracks processes in the background and removes them
|
||||||
|
# if not able to ping to the process
|
||||||
|
def BackgroundTrackProcess():
|
||||||
|
global PublicProcesses
|
||||||
|
while 1:
|
||||||
|
print("starting background process")
|
||||||
|
for i in PublicProcesses.Processes:
|
||||||
|
if not check_ping(i.ExternalAddress):
|
||||||
|
i.Status = False
|
||||||
|
SaveProcess()
|
||||||
|
time.sleep(3)
|
||||||
|
|
||||||
|
# Run the track processes every 3 seconds
|
||||||
|
# schedule.every(3).seconds.do(BackgroundTrackProcess)
|
||||||
|
|
||||||
|
Background_track_thread = threading.Thread(target=BackgroundTrackProcess, name="Track Process")
|
||||||
|
# Kills the tread of system exit
|
||||||
|
Background_track_thread.setDaemon(True)
|
||||||
|
Background_track_thread.start()
|
||||||
|
|
||||||
|
|
||||||
|
def check_ping(host):
|
||||||
|
try:
|
||||||
|
page = requests.get('http://' + host)
|
||||||
|
if page.status_code == 200:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
except:
|
||||||
|
return False
|
||||||
|
|
||||||
|
# source: https://stackoverflow.com/questions/26468640/python-function-to-test-ping
|
||||||
|
# def check_ping(host: str):
|
||||||
|
# response = os.system("ping -c 1 " + host)
|
||||||
|
# if response == 0:
|
||||||
|
# pingstatus = True
|
||||||
|
# else:
|
||||||
|
# pingstatus = False
|
||||||
|
# return pingstatus
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# def ProcessInformation():
|
# def ProcessInformation():
|
||||||
|
|
||||||
|
|||||||
@@ -1 +1,2 @@
|
|||||||
dacite
|
dacite
|
||||||
|
schedule
|
||||||
@@ -9,7 +9,7 @@ if __name__ == "__main__":
|
|||||||
ID="",
|
ID="",
|
||||||
TaskName="TestServer",
|
TaskName="TestServer",
|
||||||
InternalPortNo="8084",
|
InternalPortNo="8084",
|
||||||
NodeInfo=IPAddress(P2PRCNodes.ip_address[2]),
|
NodeInfo=P2PRCNodes.ip_address[2],
|
||||||
CommandToRunScript="sh SamplePythonTestServer/server.sh &",
|
CommandToRunScript="sh SamplePythonTestServer/server.sh &",
|
||||||
CommandToKillScript="sh SamplePythonTestServer/killserver.sh",
|
CommandToKillScript="sh SamplePythonTestServer/killserver.sh",
|
||||||
DomainName="",
|
DomainName="",
|
||||||
@@ -24,8 +24,7 @@ if __name__ == "__main__":
|
|||||||
|
|
||||||
print("------------ Process Public address -------------")
|
print("------------ Process Public address -------------")
|
||||||
|
|
||||||
# Prints status of the current process
|
print(ListProcess())
|
||||||
print(sample_process.ExternalAddress)
|
|
||||||
|
|
||||||
print("------------ Process kept waiting for 20 seconds -------------")
|
print("------------ Process kept waiting for 20 seconds -------------")
|
||||||
|
|
||||||
@@ -35,9 +34,6 @@ if __name__ == "__main__":
|
|||||||
print("------------ Process getting killed -------------")
|
print("------------ Process getting killed -------------")
|
||||||
|
|
||||||
# Kills the process
|
# Kills the process
|
||||||
KillProcess(sample_process)
|
# KillProcess(sample_process)
|
||||||
|
|
||||||
print("------------ Process current process -------------")
|
print("------------ Process current process -------------")
|
||||||
|
|
||||||
# Prints process status
|
|
||||||
print(sample_process.Status)
|
|
||||||
Reference in New Issue
Block a user