Skip to content
This repository has been archived by the owner on Oct 4, 2024. It is now read-only.

Commit

Permalink
added locking, only one instance can run against a given database loc…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
gilesknap committed Nov 30, 2017
1 parent 3ef32ec commit cbe040d
Showing 1 changed file with 28 additions and 19 deletions.
47 changes: 28 additions & 19 deletions gphotos/Main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import logging
import sys
import signal
import fcntl

from appdirs import AppDirs
from GoogleDriveSync import GoogleDriveSync
Expand All @@ -24,6 +25,7 @@ def sigterm_handler(_signo, _stack_frame):
text_file.write(traceback.format_exc())
sys.exit(0)


class GooglePhotosSyncMain:
def __init__(self):
self.data_store = None
Expand Down Expand Up @@ -115,13 +117,9 @@ def __init__(self):
help="only index a single drive file (for testing)",
default=None)

def setup(self, args):
def setup(self, args, db_path):
app_dirs = AppDirs(APP_NAME)

if not os.path.exists(args.root_folder):
os.makedirs(args.root_folder, 0o700)

db_path = args.db_path if args.db_path else args.root_folder
self.data_store = LocalData(db_path, args.flush_index)

credentials_file = os.path.join(
Expand Down Expand Up @@ -178,9 +176,6 @@ def logging(cls, args):
# add ch to logger
log.addHandler(ch)

if sys.argv[1] == "handle_signal":
signal.signal(signal.SIGTERM, sigterm_handler)

def start(self, args):
with self.data_store:
try:
Expand Down Expand Up @@ -217,16 +212,30 @@ def start(self, args):
log.info("Done.")

def main(self):
signal.signal(signal.SIGTERM, sigterm_handler)
args = self.parser.parse_args()

self.logging(args)
try:
log.info('version: {}'.format(
pkg_resources.get_distribution("gphotos-sync").version))
except Exception:
log.info('version not available')

# configure and launch
self.setup(args)
self.start(args)
signal.signal(signal.SIGTERM, sigterm_handler)

db_path = args.db_path if args.db_path else args.root_folder
if not os.path.exists(args.root_folder):
os.makedirs(args.root_folder, 0o700)

lock_file = os.path.join(db_path, 'gphotos.lock')
fp = open(lock_file, 'w')
with fp:
try:
fcntl.lockf(fp, fcntl.LOCK_EX | fcntl.LOCK_NB)
except IOError:
log.warning(u'EXITING: database is locked')
sys.exit(0)

# noinspection PyBroadException
try:
log.info('version: {}'.format(
pkg_resources.get_distribution("gphotos-sync").version))
except Exception:
log.info('version not available')

# configure and launch
self.setup(args, db_path)
self.start(args)

0 comments on commit cbe040d

Please sign in to comment.