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 .venv
*~

View file

@ -13,6 +13,7 @@ import http.cookiejar
import datetime import datetime
import getopt import getopt
import errno import errno
import fcntl
import functools import functools
import logging import logging
import logging.handlers import logging.handlers
@ -30,11 +31,13 @@ import urllib.request, urllib.error, urllib.parse
import xml.dom.minidom import xml.dom.minidom
import yaml import yaml
from contextlib import contextmanager
from io import TextIOWrapper from io import TextIOWrapper
class Config: class Config:
config = '~/.tivo/config.yaml' config = '~/.tivo/config.yaml'
lockfile = config + '.lock'
cookies = "cookies.txt" cookies = "cookies.txt"
gig = 1024.0 * 1024 * 1024 gig = 1024.0 * 1024 * 1024
headers = requests.utils.default_headers() headers = requests.utils.default_headers()
@ -382,6 +385,16 @@ class FdLogger(threading.Thread):
self.logger.exception("") 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) @timeout(43200)
def download_item(item, mak, target): def download_item(item, mak, target):
global config global config
@ -524,6 +537,7 @@ def mirror(toc, downloaddb, one=False):
(config.targetdir, avail / config.gig, config.minfree / config.gig)) (config.targetdir, avail / config.gig, config.minfree / config.gig))
sys.exit(1) sys.exit(1)
with exclusive() as lock:
items = toc.getItems() items = toc.getItems()
logger.info("*** {} shows listed".format(len(items))) logger.info("*** {} shows listed".format(len(items)))
for item in items: for item in items:
@ -537,6 +551,7 @@ def mirror(toc, downloaddb, one=False):
def download_episode(toc, downloaddb, episode): def download_episode(toc, downloaddb, episode):
with exclusive() as lock:
items = toc.getItems() items = toc.getItems()
options = {} options = {}
for item in items: for item in items:
@ -549,6 +564,7 @@ def download_episode(toc, downloaddb, episode):
def printtoc(toc, downloaddb): def printtoc(toc, downloaddb):
with exclusive() as lock:
items = toc.getItems() items = toc.getItems()
print("*** {} shows listed".format(len(items))) print("*** {} shows listed".format(len(items)))
shows = {} shows = {}