mirror of
https://github.com/clinton-hall/nzbToMedia.git
synced 2025-08-19 21:03:14 -07:00
add check for running instance. Fixes #466
This commit is contained in:
parent
863fd3ef5c
commit
8a58dedd1d
6 changed files with 93 additions and 7 deletions
|
@ -297,7 +297,7 @@ def main(args):
|
||||||
logger.info("The %s script completed successfully." % (args[0]))
|
logger.info("The %s script completed successfully." % (args[0]))
|
||||||
else:
|
else:
|
||||||
logger.error("A problem was reported in the %s script." % (args[0]))
|
logger.error("A problem was reported in the %s script." % (args[0]))
|
||||||
|
del nzbtomedia.MYAPP
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -12,6 +12,7 @@
|
||||||
git_user =
|
git_user =
|
||||||
# GitHUB branch for repo
|
# GitHUB branch for repo
|
||||||
git_branch =
|
git_branch =
|
||||||
|
# Enable/Disable forceful cleaning of leftover files following postprocess
|
||||||
force_clean = 0
|
force_clean = 0
|
||||||
# Enable/Disable logging debug messages to nzbtomedia.log
|
# Enable/Disable logging debug messages to nzbtomedia.log
|
||||||
log_debug = 0
|
log_debug = 0
|
||||||
|
|
|
@ -722,12 +722,14 @@ def main(args, section=None):
|
||||||
if result == 0:
|
if result == 0:
|
||||||
logger.info("The %s script completed successfully." % args[0])
|
logger.info("The %s script completed successfully." % args[0])
|
||||||
if os.environ.has_key('NZBOP_SCRIPTDIR'): # return code for nzbget v11
|
if os.environ.has_key('NZBOP_SCRIPTDIR'): # return code for nzbget v11
|
||||||
|
del nzbtomedia.MYAPP
|
||||||
return (nzbtomedia.NZBGET_POSTPROCESS_SUCCESS)
|
return (nzbtomedia.NZBGET_POSTPROCESS_SUCCESS)
|
||||||
else:
|
else:
|
||||||
logger.error("A problem was reported in the %s script." % args[0])
|
logger.error("A problem was reported in the %s script." % args[0])
|
||||||
if os.environ.has_key('NZBOP_SCRIPTDIR'): # return code for nzbget v11
|
if os.environ.has_key('NZBOP_SCRIPTDIR'): # return code for nzbget v11
|
||||||
|
del nzbtomedia.MYAPP
|
||||||
return (nzbtomedia.NZBGET_POSTPROCESS_ERROR)
|
return (nzbtomedia.NZBGET_POSTPROCESS_ERROR)
|
||||||
|
del nzbtomedia.MYAPP
|
||||||
return (result)
|
return (result)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,7 @@ import re
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
import platform
|
import platform
|
||||||
|
import time
|
||||||
|
|
||||||
# init libs
|
# init libs
|
||||||
PROGRAM_DIR = os.path.dirname(os.path.normpath(os.path.abspath(os.path.join(__file__, os.pardir))))
|
PROGRAM_DIR = os.path.dirname(os.path.normpath(os.path.abspath(os.path.join(__file__, os.pardir))))
|
||||||
|
@ -15,11 +16,13 @@ SYS_ARGV = sys.argv[1:]
|
||||||
APP_FILENAME = sys.argv[0]
|
APP_FILENAME = sys.argv[0]
|
||||||
APP_NAME = os.path.basename(APP_FILENAME)
|
APP_NAME = os.path.basename(APP_FILENAME)
|
||||||
LOG_DIR = os.path.join(PROGRAM_DIR, 'logs')
|
LOG_DIR = os.path.join(PROGRAM_DIR, 'logs')
|
||||||
LOG_FILE = os.path.join(LOG_DIR, 'postprocess.log')
|
LOG_FILE = os.path.join(LOG_DIR, 'nzbtomedia.log')
|
||||||
|
PID_FILE = os.path.join(LOG_DIR, 'nzbtomedia.pid')
|
||||||
CONFIG_FILE = os.path.join(PROGRAM_DIR, 'autoProcessMedia.cfg')
|
CONFIG_FILE = os.path.join(PROGRAM_DIR, 'autoProcessMedia.cfg')
|
||||||
CONFIG_SPEC_FILE = os.path.join(PROGRAM_DIR, 'autoProcessMedia.cfg.spec')
|
CONFIG_SPEC_FILE = os.path.join(PROGRAM_DIR, 'autoProcessMedia.cfg.spec')
|
||||||
CONFIG_MOVIE_FILE = os.path.join(PROGRAM_DIR, 'autoProcessMovie.cfg')
|
CONFIG_MOVIE_FILE = os.path.join(PROGRAM_DIR, 'autoProcessMovie.cfg')
|
||||||
CONFIG_TV_FILE = os.path.join(PROGRAM_DIR, 'autoProcessTv.cfg')
|
CONFIG_TV_FILE = os.path.join(PROGRAM_DIR, 'autoProcessTv.cfg')
|
||||||
|
MYAPP = None
|
||||||
|
|
||||||
from nzbtomedia.autoProcess.autoProcessComics import autoProcessComics
|
from nzbtomedia.autoProcess.autoProcessComics import autoProcessComics
|
||||||
from nzbtomedia.autoProcess.autoProcessGames import autoProcessGames
|
from nzbtomedia.autoProcess.autoProcessGames import autoProcessGames
|
||||||
|
@ -31,7 +34,7 @@ from nzbtomedia.nzbToMediaConfig import config
|
||||||
from nzbtomedia.nzbToMediaUtil import category_search, sanitizeName, copy_link, parse_args, flatten, getDirs, \
|
from nzbtomedia.nzbToMediaUtil import category_search, sanitizeName, copy_link, parse_args, flatten, getDirs, \
|
||||||
rmReadOnly,rmDir, pause_torrent, resume_torrent, remove_torrent, listMediaFiles, \
|
rmReadOnly,rmDir, pause_torrent, resume_torrent, remove_torrent, listMediaFiles, \
|
||||||
extractFiles, cleanDir, update_downloadInfoStatus, get_downloadInfo, WakeUp, makeDir, cleanDir, \
|
extractFiles, cleanDir, update_downloadInfoStatus, get_downloadInfo, WakeUp, makeDir, cleanDir, \
|
||||||
create_torrent_class, listMediaFiles
|
create_torrent_class, listMediaFiles, RunningProcess
|
||||||
from nzbtomedia.transcoder import transcoder
|
from nzbtomedia.transcoder import transcoder
|
||||||
from nzbtomedia.databases import mainDB
|
from nzbtomedia.databases import mainDB
|
||||||
|
|
||||||
|
@ -188,11 +191,16 @@ def initialize(section=None):
|
||||||
NICENESS, LOG_DEBUG, FORCE_CLEAN, FFMPEG_PATH, FFMPEG, FFPROBE, AUDIOCONTAINER, EXTCONTAINER, TORRENT_CLASS, \
|
NICENESS, LOG_DEBUG, FORCE_CLEAN, FFMPEG_PATH, FFMPEG, FFPROBE, AUDIOCONTAINER, EXTCONTAINER, TORRENT_CLASS, \
|
||||||
DELETE_ORIGINAL, PASSWORDSFILE, USER_DELAY, USER_SCRIPT, USER_SCRIPT_CLEAN, USER_SCRIPT_MEDIAEXTENSIONS, \
|
DELETE_ORIGINAL, PASSWORDSFILE, USER_DELAY, USER_SCRIPT, USER_SCRIPT_CLEAN, USER_SCRIPT_MEDIAEXTENSIONS, \
|
||||||
USER_SCRIPT_PARAM, USER_SCRIPT_RUNONCE, USER_SCRIPT_SUCCESSCODES, DOWNLOADINFO, CHECK_MEDIA, SAFE_MODE, \
|
USER_SCRIPT_PARAM, USER_SCRIPT_RUNONCE, USER_SCRIPT_SUCCESSCODES, DOWNLOADINFO, CHECK_MEDIA, SAFE_MODE, \
|
||||||
TORRENT_DEFAULTDIR, NZB_DEFAULTDIR, REMOTEPATHS, LOG_ENV
|
TORRENT_DEFAULTDIR, NZB_DEFAULTDIR, REMOTEPATHS, LOG_ENV, PID_FILE, MYAPP
|
||||||
|
|
||||||
if __INITIALIZED__:
|
if __INITIALIZED__:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
MYAPP = RunningProcess()
|
||||||
|
while MYAPP.alreadyrunning():
|
||||||
|
print("!!! Waiting for existing session to end!")
|
||||||
|
time.sleep(30)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
locale.setlocale(locale.LC_ALL, "")
|
locale.setlocale(locale.LC_ALL, "")
|
||||||
SYS_ENCODING = locale.getpreferredencoding()
|
SYS_ENCODING = locale.getpreferredencoding()
|
||||||
|
@ -272,6 +280,9 @@ def initialize(section=None):
|
||||||
updated = versionCheck.CheckVersion().update()
|
updated = versionCheck.CheckVersion().update()
|
||||||
if updated:
|
if updated:
|
||||||
# restart nzbToMedia
|
# restart nzbToMedia
|
||||||
|
try:
|
||||||
|
del MYAPP
|
||||||
|
except: pass
|
||||||
restart()
|
restart()
|
||||||
else:
|
else:
|
||||||
logger.error("Update wasn't successful, not restarting. Check your log for more information.")
|
logger.error("Update wasn't successful, not restarting. Check your log for more information.")
|
||||||
|
|
|
@ -242,7 +242,7 @@ class DispatchingFormatter:
|
||||||
formatter = self._formatters.get(record.name, self._default_formatter)
|
formatter = self._formatters.get(record.name, self._default_formatter)
|
||||||
return formatter.format(record)
|
return formatter.format(record)
|
||||||
|
|
||||||
ntm_log_instance = NTMRotatingLogHandler("nzbtomedia.log", NUM_LOGS, LOG_SIZE)
|
ntm_log_instance = NTMRotatingLogHandler(nzbtomedia.LOG_FILE, NUM_LOGS, LOG_SIZE)
|
||||||
|
|
||||||
def log(toLog, logLevel=MESSAGE, section='MAIN'):
|
def log(toLog, logLevel=MESSAGE, section='MAIN'):
|
||||||
ntm_log_instance.log(toLog, logLevel, section)
|
ntm_log_instance.log(toLog, logLevel, section)
|
||||||
|
|
|
@ -7,6 +7,7 @@ import struct
|
||||||
import shutil
|
import shutil
|
||||||
import time
|
import time
|
||||||
import datetime
|
import datetime
|
||||||
|
import platform
|
||||||
import guessit
|
import guessit
|
||||||
import beets
|
import beets
|
||||||
import requests
|
import requests
|
||||||
|
@ -932,4 +933,75 @@ def get_downloadInfo(inputName, status):
|
||||||
sqlResults = myDB.select("SELECT * FROM downloads WHERE input_name=? AND status=?",
|
sqlResults = myDB.select("SELECT * FROM downloads WHERE input_name=? AND status=?",
|
||||||
[unicode(inputName), status])
|
[unicode(inputName), status])
|
||||||
|
|
||||||
return sqlResults
|
return sqlResults
|
||||||
|
|
||||||
|
class RunningProcess():
|
||||||
|
""" Limits application to single instance """
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
if platform.system() == 'Windows':
|
||||||
|
self.process = WindowsProcess()
|
||||||
|
else:
|
||||||
|
self.process = PosixProcess()
|
||||||
|
|
||||||
|
def alreadyrunning(self):
|
||||||
|
return self.process.alreadyrunning()
|
||||||
|
|
||||||
|
#def __del__(self):
|
||||||
|
# self.process.__del__()
|
||||||
|
|
||||||
|
class WindowsProcess():
|
||||||
|
|
||||||
|
if platform.system() == 'Windows':
|
||||||
|
from win32event import CreateMutex
|
||||||
|
from win32api import CloseHandle, GetLastError
|
||||||
|
from winerror import ERROR_ALREADY_EXISTS
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.mutexname = "nzbtomedia_{D0E858DF-985E-4907-B7FB-8D732C3FC3B9}"
|
||||||
|
|
||||||
|
def alreadyrunning(self):
|
||||||
|
self.mutex = CreateMutex(None, False, self.mutexname)
|
||||||
|
self.lasterror = GetLastError()
|
||||||
|
return (self.lasterror == ERROR_ALREADY_EXISTS)
|
||||||
|
|
||||||
|
def __del__(self):
|
||||||
|
if self.mutex:
|
||||||
|
CloseHandle(self.mutex)
|
||||||
|
|
||||||
|
class PosixProcess():
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.pidpath = nzbtomedia.PID_FILE
|
||||||
|
|
||||||
|
def alreadyrunning(self):
|
||||||
|
if os.path.exists(self.pidpath):
|
||||||
|
# Make sure it is not a "stale" pidFile
|
||||||
|
try:
|
||||||
|
pid = int(open(self.pidpath, 'r').read().strip())
|
||||||
|
except:
|
||||||
|
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
|
||||||
|
fp = open(self.pidpath, 'w')
|
||||||
|
fp.write(str(os.getpid()))
|
||||||
|
fp.close()
|
||||||
|
|
||||||
|
return self.lasterror
|
||||||
|
|
||||||
|
def __del__(self):
|
||||||
|
if not self.lasterror:
|
||||||
|
if os.path.isfile(self.pidpath):
|
||||||
|
os.unlink(self.pidpath)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue