| 19 | |
| 20 | @dataclass |
| 21 | class ProcessInfo: |
| 22 | pid: Optional[int] |
| 23 | terminal: Optional[str] |
| 24 | is_running: bool |
| 25 | ngrok_pid: Optional[int] = None |
| 26 | cloudflare_pid: Optional[int] = None |
| 27 | tailscale_enabled: bool = False |
| 28 | tailscale_port: Optional[int] = None |
| 29 | |
| 30 | _env_file = utils.get_default_path() / ".service.env" |
| 31 | |
| 32 | @classmethod |
| 33 | def _str_to_bool(cls, value: str) -> bool: |
| 34 | return value.lower() in ('true', '1', 'yes', 'on') |
| 35 | |
| 36 | @classmethod |
| 37 | def save(cls, pid, is_running: bool, ngrok_pid: Optional[int] = None, cloudflare_pid: Optional[int] = None, |
| 38 | tailscale_enabled: bool = False, tailscale_port: Optional[int] = None) -> None: |
| 39 | """Save current process information to .env file.""" |
| 40 | |
| 41 | env_path = str(cls._env_file) |
| 42 | |
| 43 | # Create the file if it doesn't exist |
| 44 | if not cls._env_file.exists(): |
| 45 | cls._env_file.touch() |
| 46 | |
| 47 | process_info = { |
| 48 | 'KEEPER_SERVICE_PID': str(pid), |
| 49 | 'KEEPER_SERVICE_TERMINAL': TerminalHandler.get_terminal_info() or '', |
| 50 | 'KEEPER_SERVICE_IS_RUNNING': str(is_running).lower() |
| 51 | } |
| 52 | |
| 53 | if ngrok_pid is not None: |
| 54 | process_info['KEEPER_SERVICE_NGROK_PID'] = str(ngrok_pid) |
| 55 | |
| 56 | if cloudflare_pid is not None: |
| 57 | process_info['KEEPER_SERVICE_CLOUDFLARE_PID'] = str(cloudflare_pid) |
| 58 | |
| 59 | if tailscale_enabled: |
| 60 | process_info['KEEPER_SERVICE_TAILSCALE_ENABLED'] = str(tailscale_enabled).lower() |
| 61 | |
| 62 | if tailscale_port is not None: |
| 63 | process_info['KEEPER_SERVICE_TAILSCALE_PORT'] = str(tailscale_port) |
| 64 | |
| 65 | try: |
| 66 | for key, value in process_info.items(): |
| 67 | set_key(env_path, key, value, quote_mode='never') |
| 68 | logger.debug("Process information saved successfully to .env") |
| 69 | except Exception as e: |
| 70 | logger.error(f"Failed to save process information: {e}") |
| 71 | pass |
| 72 | |
| 73 | @classmethod |
| 74 | def load(cls) -> 'ProcessInfo': |
| 75 | """Load process information from .env file.""" |
| 76 | try: |
| 77 | for key in dotenv_values(cls._env_file).keys(): |
| 78 | os.environ.pop(key, None) # Remove old values |