From 749dfd7f53c3531741c152f82e170a1c17d661e2 Mon Sep 17 00:00:00 2001 From: Akilan Date: Tue, 14 Jan 2025 02:03:40 +0000 Subject: [PATCH] added background tracking process --- python/.gitignore | 1 + python/library.py | 121 +++++++++++++++++++++++++++++++++++++++- python/requirements.txt | 3 +- python/test.py | 14 ++--- 4 files changed, 128 insertions(+), 11 deletions(-) diff --git a/python/.gitignore b/python/.gitignore index b63d853..42f6bd0 100644 --- a/python/.gitignore +++ b/python/.gitignore @@ -1,3 +1,4 @@ p2prc-virtualenv/ SharedObjects/ __pycache__/ +data.json diff --git a/python/library.py b/python/library.py index 5cd1004..e6d4e06 100644 --- a/python/library.py +++ b/python/library.py @@ -9,11 +9,23 @@ import subprocess import uuid import dacite import os +import sqlalchemy +import dataclasses +from typing import Union +import schedule +import threading +import requests p2prc = ctypes.CDLL("SharedObjects/p2prc.so") p2prc.Init("") +# Start running schedule +schedule.run_pending() + +# # Create Sqlite database to track processes +# engine=sqlalchemy.create_engine(f'sqlite:///homeserver.db') + # Global variable # A global variable will be populated on runtime. # It is read from P2PRC directly or from a local @@ -108,22 +120,129 @@ class Process: Status: bool DomainName: str +@dataclass +class Processes: + Processes: List[Process] + +PublicProcesses: Union[Processes, None] = None + # Initial defined functions # Exposed functions def SpinProcess(process: Process): # Starts P2PRC process os.system(process.CommandToRunScript) + + # 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=process.NodeInfo.ip_address.IPV4 + ":" + process.NodeInfo.ip_address.ServerPort) + process.ExternalAddress = P2PRCMapPort(port=process.InternalPortNo, + domainname=process.DomainName, + serveraddress=ServerAddress) process.ID = str(uuid.uuid4()) process.Status = True + + # Save the process to memory + AddProcessToMemory(process) + + # Save to disk + SaveProcess() + return process # Kill process based on the process provided def KillProcess(process: Process): os.system(process.CommandToKillScript) process.Status = False + + # Remove the process from memory + PublicProcesses.Processes.remove(process) + + # Remove from disk + SaveProcess() + 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(): diff --git a/python/requirements.txt b/python/requirements.txt index 5b78deb..ba8f76e 100644 --- a/python/requirements.txt +++ b/python/requirements.txt @@ -1 +1,2 @@ -dacite \ No newline at end of file +dacite +schedule \ No newline at end of file diff --git a/python/test.py b/python/test.py index e463a47..da3b8f6 100644 --- a/python/test.py +++ b/python/test.py @@ -9,7 +9,7 @@ if __name__ == "__main__": ID="", TaskName="TestServer", InternalPortNo="8084", - NodeInfo=IPAddress(P2PRCNodes.ip_address[2]), + NodeInfo=P2PRCNodes.ip_address[2], CommandToRunScript="sh SamplePythonTestServer/server.sh &", CommandToKillScript="sh SamplePythonTestServer/killserver.sh", DomainName="", @@ -23,10 +23,9 @@ if __name__ == "__main__": sample_process = SpinProcess(sample_process) print("------------ Process Public address -------------") - - # Prints status of the current process - print(sample_process.ExternalAddress) + print(ListProcess()) + print("------------ Process kept waiting for 20 seconds -------------") # Runs the process for 20 seconds @@ -35,9 +34,6 @@ if __name__ == "__main__": print("------------ Process getting killed -------------") # Kills the process - KillProcess(sample_process) + # KillProcess(sample_process) - print("------------ Process current process -------------") - - # Prints process status - print(sample_process.Status) \ No newline at end of file + print("------------ Process current process -------------") \ No newline at end of file