From ee3ccfa5989accfdaeee38c5e4c6e6ac8261d4ff Mon Sep 17 00:00:00 2001 From: Bas Stottelaar Date: Thu, 13 Nov 2014 02:32:48 +0100 Subject: [PATCH] Allow one to adjust interval settings without restarting. --- data/interfaces/default/config.html | 2 +- headphones/__init__.py | 36 +++++++++++++++++++++++------ headphones/webserve.py | 5 +++- 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/data/interfaces/default/config.html b/data/interfaces/default/config.html index 47d81587..4c288063 100644 --- a/data/interfaces/default/config.html +++ b/data/interfaces/default/config.html @@ -137,7 +137,7 @@
- Web Interface changes require a restart to take effect + Web Interface changes require a restart to take effect. Saving settings will restart intervals.
diff --git a/headphones/__init__.py b/headphones/__init__.py index c4f47793..0e01b9b1 100644 --- a/headphones/__init__.py +++ b/headphones/__init__.py @@ -64,6 +64,7 @@ CREATEPID = False PIDFILE = None SCHED = BackgroundScheduler() +SCHED_LOCK = threading.Lock() INIT_LOCK = threading.Lock() _INITIALIZED = False @@ -256,14 +257,25 @@ def launch_browser(host, port, root): logger.error('Could not launch browser: %s', e) -def start(): +def initialize_scheduler(): + """ + Start the scheduled background tasks. Because this method can be called + multiple times, the old tasks will be first removed. + """ - global started + from headphones import updater, searcher, librarysync, postprocessor, \ + torrentfinished - if _INITIALIZED: + with SCHED_LOCK: + # Remove all jobs + count = len(SCHED.get_jobs()) - # Start our scheduled background tasks - from headphones import updater, searcher, librarysync, postprocessor, torrentfinished + if count > 0: + logger.debug("Current number of background tasks: %d", count) + SCHED.shutdown() + SCHED.remove_all_jobs() + + # Then add all jobs SCHED.add_job(updater.dbUpdate, trigger=IntervalTrigger( hours=CONFIG.UPDATE_DB_INTERVAL)) SCHED.add_job(searcher.searchforalbum, trigger=IntervalTrigger( @@ -281,11 +293,21 @@ def start(): # Remove Torrent + data if Post Processed and finished Seeding if CONFIG.TORRENT_REMOVAL_INTERVAL > 0: - SCHED.add_job(torrentfinished.checkTorrentFinished, trigger=IntervalTrigger( - minutes=CONFIG.TORRENT_REMOVAL_INTERVAL)) + SCHED.add_job(torrentfinished.checkTorrentFinished, + trigger=IntervalTrigger( + minutes=CONFIG.TORRENT_REMOVAL_INTERVAL)) + # Start scheduler + logger.info("(Re-)Scheduling background tasks") SCHED.start() + +def start(): + + global started + + if _INITIALIZED: + initialize_scheduler() started = True diff --git a/headphones/webserve.py b/headphones/webserve.py index fa5c0c56..5f5624db 100644 --- a/headphones/webserve.py +++ b/headphones/webserve.py @@ -1248,7 +1248,10 @@ class WebInterface(object): # Write the config headphones.CONFIG.write() - #reconfigure musicbrainz database connection with the new values + # Reconfigure scheduler + headphones.initialize_scheduler() + + # Reconfigure musicbrainz database connection with the new values mb.startmb() raise cherrypy.HTTPRedirect("config")