Use abstract named sockets for plugins mirror single instance instead of lock files

This commit is contained in:
Kovid Goyal 2014-04-08 10:31:06 +05:30
parent 68f20774fc
commit cf2f7ffd7f

View File

@ -6,7 +6,7 @@ from __future__ import (unicode_literals, division, absolute_import,
__license__ = 'GPL v3' __license__ = 'GPL v3'
__copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>' __copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
import urllib2, re, HTMLParser, zlib, gzip, io, sys, bz2, json, errno, urlparse, os, zipfile, ast, tempfile, glob, fcntl, atexit, stat, socket import urllib2, re, HTMLParser, zlib, gzip, io, sys, bz2, json, errno, urlparse, os, zipfile, ast, tempfile, glob, stat, socket
from future_builtins import map, zip, filter from future_builtins import map, zip, filter
from collections import namedtuple from collections import namedtuple
from multiprocessing.pool import ThreadPool from multiprocessing.pool import ThreadPool
@ -419,18 +419,17 @@ h1 { text-align: center }
atomic_write(raw, 'index.html') atomic_write(raw, 'index.html')
_singleinstance = None
def singleinstance(): def singleinstance():
path = os.path.abspath('plugins_mirror_update.lock') global _singleinstance
s = _singleinstance = socket.socket(socket.AF_UNIX)
try: try:
f = open(path, 'w') s.bind(b'\0calibre-plugins-mirror-singleinstance')
fcntl.lockf(f.fileno(), fcntl.LOCK_EX|fcntl.LOCK_NB) except socket.error as err:
f.write(str(os.getpid())) if getattr(err, 'errno', None) == errno.EADDRINUSE:
f.flush() return False
atexit.register(f.close) raise
return True return True
except IOError:
return False
return False
def main(): def main():
try: try:
@ -445,7 +444,7 @@ def main():
else: else:
raise raise
if not singleinstance(): if not singleinstance():
print('Another instance is running or you dont have permission to create lock file, aborting.', file=sys.stderr) print('Another instance of plugins-mirror is running', file=sys.stderr)
raise SystemExit(1) raise SystemExit(1)
open('log', 'w').close() open('log', 'w').close()
try: try: