@@ -50,6 +50,13 @@ async def connectionHandler(websocket, path):
5050 return connectionHandler
5151
5252
53+ def writeStatus (message ):
54+ try :
55+ with open ('statusfile.js' , 'w' ) as status :
56+ print (f'status="Server status: { message } ";' , file = status )
57+ except Exception :
58+ pass
59+
5360#if the python module has been launched as the main one
5461if __name__ == "__main__" :
5562 #first, we allow for command line arguments formatted such:
@@ -71,13 +78,16 @@ async def connectionHandler(websocket, path):
7178 except (IndexError , ValueError ):
7279 motorInterfaceType = 0
7380
81+ writeStatus ("server booting" )
82+
7483 #next, write the host:port combo to a file that js can read.
7584 try :
7685 with open ("hostPort.js" , 'w' ) as fileOut :
7786 fileOut .write (f"myHost = '{ host } ';" )
7887 fileOut .write (f"myPort = '{ port } ';" )
7988 #with open() as syntax automatically closes the file on exit
8089 except (FileNotFoundError , FileExistsError , OSError ) as e :
90+ writeStatus ("server crashed writing host-port file" )
8191 print ("Something went wrong trying to write the hostport.js file. Type:" + str (type (e )))
8292
8393 print (f"Starting Server at { host } :{ port } " )
@@ -91,7 +101,7 @@ async def connectionHandler(websocket, path):
91101
92102 #This outer while loop "solves" the race condition.
93103 #More on this at the end of the file.
94- failCtr = 5
104+ failCtr = 0
95105 while (True ):
96106 #start the server
97107 server = websockets .serve (buildConnectionHandler (interface ), host , port )
@@ -101,6 +111,7 @@ async def connectionHandler(websocket, path):
101111 # under normal circumstances, this means we wait forever
102112 asyncio .get_event_loop ().run_until_complete (server )
103113 print ("server started successfully." )
114+ writeStatus ("server started nominally" )
104115 failCtr = - 1
105116 asyncio .get_event_loop ().run_forever ()
106117 #any code down here would not be reachable until the server closes its socket
@@ -110,13 +121,16 @@ async def connectionHandler(websocket, path):
110121 #the interrupt was fired (ctrl-c), time to exit
111122 #note, the interrupt wont happen till the next async event happens
112123 print ("Exiting via KeyboardInterrupt" )
124+ writeStatus ("Exited via keyboard interrupt" )
113125 exit (- 1 )
114126 except OSError as e :
115127 failCtr += 1
116- if (failCtr > 5 ):
117- print ("The server startup failed too many times, allowing exception to fire. " )
128+ if (failCtr % 10 == 0 ):
129+ print ("The server startup failed too many times" )
118130 print ("\t This may be a result of too many packages slowing down the loading of the network interface." )
119- raise e #allow the error to fire.
131+ print (f"\t { e } " )
132+ # raise e #allow the error to fire.
133+ writeStatus (f"Sever bind failure after { failCtr } retries" )
120134 else :
121135 sleep (5 )
122136
0 commit comments