Add locking for long runs

This commit is contained in:
Stefan Bethke 2024-08-04 17:38:06 +00:00
parent 53420ffc31
commit 448ef57b40
2 changed files with 52 additions and 35 deletions

1
.gitignore vendored
View file

@ -1 +1,2 @@
.venv
*~

View file

@ -13,6 +13,7 @@ import http.cookiejar
import datetime
import getopt
import errno
import fcntl
import functools
import logging
import logging.handlers
@ -30,11 +31,13 @@ import urllib.request, urllib.error, urllib.parse
import xml.dom.minidom
import yaml
from contextlib import contextmanager
from io import TextIOWrapper
class Config:
config = '~/.tivo/config.yaml'
lockfile = config + '.lock'
cookies = "cookies.txt"
gig = 1024.0 * 1024 * 1024
headers = requests.utils.default_headers()
@ -382,6 +385,16 @@ class FdLogger(threading.Thread):
self.logger.exception("")
@contextmanager
def exclusive():
with open(os.path.expanduser(config.lockfile), 'w') as f:
try:
fcntl.lockf(f, fcntl.LOCK_EX | fcntl.LOCK_NB)
except BlockingIOError as e:
raise TivoException('another tivomirror instance is already running')
yield 'locked'
fcntl.lockf(f, fcntl.LOCK_UN)
@timeout(43200)
def download_item(item, mak, target):
global config
@ -524,6 +537,7 @@ def mirror(toc, downloaddb, one=False):
(config.targetdir, avail / config.gig, config.minfree / config.gig))
sys.exit(1)
with exclusive() as lock:
items = toc.getItems()
logger.info("*** {} shows listed".format(len(items)))
for item in items:
@ -537,6 +551,7 @@ def mirror(toc, downloaddb, one=False):
def download_episode(toc, downloaddb, episode):
with exclusive() as lock:
items = toc.getItems()
options = {}
for item in items:
@ -549,6 +564,7 @@ def download_episode(toc, downloaddb, episode):
def printtoc(toc, downloaddb):
with exclusive() as lock:
items = toc.getItems()
print("*** {} shows listed".format(len(items)))
shows = {}