mirror of
https://github.com/clinton-hall/nzbToMedia.git
synced 2025-08-20 21:33:13 -07:00
Refactor utils.*Process -> utils.processes.*Process
This commit is contained in:
parent
3b670b895b
commit
21fa4e3896
2 changed files with 93 additions and 91 deletions
|
@ -26,14 +26,7 @@ from utorrent.client import UTorrentClient
|
|||
|
||||
import core
|
||||
from core import extractor, logger, main_db
|
||||
|
||||
try:
|
||||
from win32event import CreateMutex
|
||||
from win32api import CloseHandle, GetLastError
|
||||
from winerror import ERROR_ALREADY_EXISTS
|
||||
except ImportError:
|
||||
if os.name == 'nt':
|
||||
raise
|
||||
from core.utils.processes import RunningProcess
|
||||
|
||||
try:
|
||||
import jaraco
|
||||
|
@ -1321,86 +1314,3 @@ def get_download_info(input_name, status):
|
|||
[text_type(input_name), status])
|
||||
|
||||
return sql_results
|
||||
|
||||
|
||||
class WindowsProcess(object):
|
||||
def __init__(self):
|
||||
self.mutex = None
|
||||
self.mutexname = 'nzbtomedia_{pid}'.format(pid=core.PID_FILE.replace('\\', '/')) # {D0E858DF-985E-4907-B7FB-8D732C3FC3B9}'
|
||||
self.CreateMutex = CreateMutex
|
||||
self.CloseHandle = CloseHandle
|
||||
self.GetLastError = GetLastError
|
||||
self.ERROR_ALREADY_EXISTS = ERROR_ALREADY_EXISTS
|
||||
|
||||
def alreadyrunning(self):
|
||||
self.mutex = self.CreateMutex(None, 0, self.mutexname)
|
||||
self.lasterror = self.GetLastError()
|
||||
if self.lasterror == self.ERROR_ALREADY_EXISTS:
|
||||
self.CloseHandle(self.mutex)
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def __del__(self):
|
||||
if self.mutex:
|
||||
self.CloseHandle(self.mutex)
|
||||
|
||||
|
||||
class PosixProcess(object):
|
||||
def __init__(self):
|
||||
self.pidpath = core.PID_FILE
|
||||
self.lock_socket = None
|
||||
|
||||
def alreadyrunning(self):
|
||||
try:
|
||||
self.lock_socket = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
|
||||
self.lock_socket.bind('\0{path}'.format(path=self.pidpath))
|
||||
self.lasterror = False
|
||||
return self.lasterror
|
||||
except socket.error as e:
|
||||
if 'Address already in use' in e:
|
||||
self.lasterror = True
|
||||
return self.lasterror
|
||||
except AttributeError:
|
||||
pass
|
||||
if os.path.exists(self.pidpath):
|
||||
# Make sure it is not a 'stale' pidFile
|
||||
try:
|
||||
pid = int(open(self.pidpath, 'r').read().strip())
|
||||
except Exception:
|
||||
pid = None
|
||||
# Check list of running pids, if not running it is stale so overwrite
|
||||
if isinstance(pid, int):
|
||||
try:
|
||||
os.kill(pid, 0)
|
||||
self.lasterror = True
|
||||
except OSError:
|
||||
self.lasterror = False
|
||||
else:
|
||||
self.lasterror = False
|
||||
else:
|
||||
self.lasterror = False
|
||||
|
||||
if not self.lasterror:
|
||||
# Write my pid into pidFile to keep multiple copies of program from running
|
||||
try:
|
||||
fp = open(self.pidpath, 'w')
|
||||
fp.write(str(os.getpid()))
|
||||
fp.close()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return self.lasterror
|
||||
|
||||
def __del__(self):
|
||||
if not self.lasterror:
|
||||
if self.lock_socket:
|
||||
self.lock_socket.close()
|
||||
if os.path.isfile(self.pidpath):
|
||||
os.unlink(self.pidpath)
|
||||
|
||||
|
||||
if os.name == 'nt':
|
||||
RunningProcess = WindowsProcess
|
||||
else:
|
||||
RunningProcess = PosixProcess
|
||||
|
|
92
core/utils/processes.py
Normal file
92
core/utils/processes.py
Normal file
|
@ -0,0 +1,92 @@
|
|||
import os
|
||||
import socket
|
||||
|
||||
import core
|
||||
|
||||
if os.name == 'nt':
|
||||
from win32event import CreateMutex
|
||||
from win32api import CloseHandle, GetLastError
|
||||
from winerror import ERROR_ALREADY_EXISTS
|
||||
|
||||
|
||||
class WindowsProcess(object):
|
||||
def __init__(self):
|
||||
self.mutex = None
|
||||
self.mutexname = 'nzbtomedia_{pid}'.format(pid=core.PID_FILE.replace('\\', '/')) # {D0E858DF-985E-4907-B7FB-8D732C3FC3B9}'
|
||||
self.CreateMutex = CreateMutex
|
||||
self.CloseHandle = CloseHandle
|
||||
self.GetLastError = GetLastError
|
||||
self.ERROR_ALREADY_EXISTS = ERROR_ALREADY_EXISTS
|
||||
|
||||
def alreadyrunning(self):
|
||||
self.mutex = self.CreateMutex(None, 0, self.mutexname)
|
||||
self.lasterror = self.GetLastError()
|
||||
if self.lasterror == self.ERROR_ALREADY_EXISTS:
|
||||
self.CloseHandle(self.mutex)
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def __del__(self):
|
||||
if self.mutex:
|
||||
self.CloseHandle(self.mutex)
|
||||
|
||||
|
||||
class PosixProcess(object):
|
||||
def __init__(self):
|
||||
self.pidpath = core.PID_FILE
|
||||
self.lock_socket = None
|
||||
|
||||
def alreadyrunning(self):
|
||||
try:
|
||||
self.lock_socket = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
|
||||
self.lock_socket.bind('\0{path}'.format(path=self.pidpath))
|
||||
self.lasterror = False
|
||||
return self.lasterror
|
||||
except socket.error as e:
|
||||
if 'Address already in use' in e:
|
||||
self.lasterror = True
|
||||
return self.lasterror
|
||||
except AttributeError:
|
||||
pass
|
||||
if os.path.exists(self.pidpath):
|
||||
# Make sure it is not a 'stale' pidFile
|
||||
try:
|
||||
pid = int(open(self.pidpath, 'r').read().strip())
|
||||
except Exception:
|
||||
pid = None
|
||||
# Check list of running pids, if not running it is stale so overwrite
|
||||
if isinstance(pid, int):
|
||||
try:
|
||||
os.kill(pid, 0)
|
||||
self.lasterror = True
|
||||
except OSError:
|
||||
self.lasterror = False
|
||||
else:
|
||||
self.lasterror = False
|
||||
else:
|
||||
self.lasterror = False
|
||||
|
||||
if not self.lasterror:
|
||||
# Write my pid into pidFile to keep multiple copies of program from running
|
||||
try:
|
||||
fp = open(self.pidpath, 'w')
|
||||
fp.write(str(os.getpid()))
|
||||
fp.close()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return self.lasterror
|
||||
|
||||
def __del__(self):
|
||||
if not self.lasterror:
|
||||
if self.lock_socket:
|
||||
self.lock_socket.close()
|
||||
if os.path.isfile(self.pidpath):
|
||||
os.unlink(self.pidpath)
|
||||
|
||||
|
||||
if os.name == 'nt':
|
||||
RunningProcess = WindowsProcess
|
||||
else:
|
||||
RunningProcess = PosixProcess
|
Loading…
Add table
Add a link
Reference in a new issue