Fix pidfile access

This commit is contained in:
Labrys of Knossos 2022-12-14 03:56:01 -05:00
commit fc3d6a5744

View file

@ -21,9 +21,9 @@ if os.name == 'nt':
class WindowsProcess: class WindowsProcess:
def __init__(self): def __init__(self):
self.mutex = None self.mutex = None
self.mutexname = 'nzbtomedia_{pid}'.format( # {D0E858DF-985E-4907-B7FB-8D732C3FC3B9}
pid=core.PID_FILE.replace('\\', '/'), _path_str = os.fspath(core.PID_FILE).replace('\\', '/')
) # {D0E858DF-985E-4907-B7FB-8D732C3FC3B9}' self.mutexname = f'nzbtomedia_{_path_str}'
self.CreateMutex = CreateMutex self.CreateMutex = CreateMutex
self.CloseHandle = CloseHandle self.CloseHandle = CloseHandle
self.GetLastError = GetLastError self.GetLastError = GetLastError
@ -80,21 +80,15 @@ class PosixProcess:
if not self.lasterror: if not self.lasterror:
# Write my pid into pidFile to keep multiple copies of program from running # Write my pid into pidFile to keep multiple copies of program from running
try: with self.pidpath.open(mode='w') as fp:
fp = open(self.pidpath, 'w') fp.write(os.getpid())
fp.write(str(os.getpid()))
fp.close()
except Exception:
pass
return self.lasterror return self.lasterror
def __del__(self): def __del__(self):
if not self.lasterror: if not self.lasterror:
if self.lock_socket: if self.lock_socket:
self.lock_socket.close() self.lock_socket.close()
if os.path.isfile(self.pidpath): self.pidpath.unlink(missing_ok=True)
os.unlink(self.pidpath)
ProcessType = typing.Type[typing.Union[PosixProcess, WindowsProcess]] ProcessType = typing.Type[typing.Union[PosixProcess, WindowsProcess]]