| 
									
										
										
										
											2023-02-23 18:48:37 -06:00
										 |  |  | # Python imports | 
					
						
							|  |  |  | import os | 
					
						
							|  |  |  | import json | 
					
						
							|  |  |  | import inspect | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Lib imports | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Application imports | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class StartCheckMixin: | 
					
						
							| 
									
										
										
										
											2024-01-08 20:01:14 -06:00
										 |  |  |     def is_dirty_start(self) -> bool: | 
					
						
							|  |  |  |         return self._dirty_start | 
					
						
							| 
									
										
										
										
											2024-01-08 19:32:53 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def clear_pid(self): | 
					
						
							|  |  |  |         if not self.is_trace_debug(): | 
					
						
							|  |  |  |             self._clean_pid() | 
					
						
							| 
									
										
										
										
											2023-02-23 18:48:37 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def do_dirty_start_check(self): | 
					
						
							| 
									
										
										
										
											2024-01-08 19:32:53 -06:00
										 |  |  |         if self.is_trace_debug(): | 
					
						
							|  |  |  |             pid = os.getpid() | 
					
						
							|  |  |  |             self._print_pid(pid) | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-08 20:01:14 -06:00
										 |  |  |         if os.path.exists(self._PID_FILE): | 
					
						
							|  |  |  |             with open(self._PID_FILE, "r") as f: | 
					
						
							|  |  |  |                 pid = f.readline().strip() | 
					
						
							| 
									
										
										
										
											2023-02-23 18:48:37 -06:00
										 |  |  |                 if pid not in ("", None): | 
					
						
							| 
									
										
										
										
											2024-01-08 20:01:14 -06:00
										 |  |  |                     if self.is_pid_alive( int(pid) ): | 
					
						
							|  |  |  |                         print("PID file exists and PID is alive... Letting downstream errors (sans debug args) handle app closure propigation.") | 
					
						
							|  |  |  |                         return | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self._write_new_pid() | 
					
						
							| 
									
										
										
										
											2023-02-23 18:48:37 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |     """ Check For the existence of a unix pid. """ | 
					
						
							| 
									
										
										
										
											2024-01-08 20:01:14 -06:00
										 |  |  |     def is_pid_alive(self, pid): | 
					
						
							| 
									
										
										
										
											2023-02-23 18:48:37 -06:00
										 |  |  |         print(f"PID Found: {pid}") | 
					
						
							| 
									
										
										
										
											2024-01-08 20:01:14 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-23 18:48:37 -06:00
										 |  |  |         try: | 
					
						
							|  |  |  |             os.kill(pid, 0) | 
					
						
							|  |  |  |         except OSError: | 
					
						
							| 
									
										
										
										
											2024-01-08 20:01:14 -06:00
										 |  |  |             print(f"{app_name} PID file exists but PID is irrelevant; starting dirty...") | 
					
						
							| 
									
										
										
										
											2023-02-23 18:48:37 -06:00
										 |  |  |             self._dirty_start = True | 
					
						
							| 
									
										
										
										
											2024-01-08 20:01:14 -06:00
										 |  |  |             return False | 
					
						
							| 
									
										
										
										
											2023-02-23 18:48:37 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-08 20:01:14 -06:00
										 |  |  |         return True | 
					
						
							| 
									
										
										
										
											2023-02-23 18:48:37 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def _write_new_pid(self): | 
					
						
							|  |  |  |         pid = os.getpid() | 
					
						
							|  |  |  |         self._write_pid(pid) | 
					
						
							| 
									
										
										
										
											2024-01-08 19:32:53 -06:00
										 |  |  |         self._print_pid(pid) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def _print_pid(self, pid): | 
					
						
							| 
									
										
										
										
											2023-07-30 00:36:52 -05:00
										 |  |  |         print(f"{app_name} PID:  {pid}") | 
					
						
							| 
									
										
										
										
											2023-02-23 18:48:37 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def _clean_pid(self): | 
					
						
							|  |  |  |         os.unlink(self._PID_FILE) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def _write_pid(self, pid): | 
					
						
							|  |  |  |         with open(self._PID_FILE, "w") as _pid: | 
					
						
							| 
									
										
										
										
											2024-01-08 19:32:53 -06:00
										 |  |  |             _pid.write(f"{pid}") |