From 431951e7dba18e213bde112e6dc8863da1cec7ca Mon Sep 17 00:00:00 2001 From: bugdone Date: Sat, 6 May 2017 11:41:22 +0300 Subject: [PATCH] Option to poll demo directory for changes. Closes #197 Useful for people using network filesystems where inotify might not work. --- resources/public/js/controllers.js | 1 + resources/public/templates/settings.html | 23 ++++++++++++++++++ src/hsbox/core.clj | 6 +---- src/hsbox/indexer.clj | 30 +++++++++++++++++++++++- src/hsbox/stats.clj | 9 +++++++ 5 files changed, 63 insertions(+), 6 deletions(-) diff --git a/resources/public/js/controllers.js b/resources/public/js/controllers.js index d19e435..ea6ae8e 100644 --- a/resources/public/js/controllers.js +++ b/resources/public/js/controllers.js @@ -795,6 +795,7 @@ hsboxControllers.controller('RoundSearch', function ($scope, $http, $routeParams hsboxControllers.controller('Settings', function ($scope, $http, $rootScope, config) { $scope.steamApiCollapsed = false; $scope.demoDirectoryCollapsed = true; + $scope.directoryScanIntervalCollapsed = false; $scope.vdmCollapsed = true; $scope.demowebmodeCollapsed = true; $scope.demoloaderBaseurlCollapsed = true; diff --git a/resources/public/templates/settings.html b/resources/public/templates/settings.html index 239e150..336073d 100755 --- a/resources/public/templates/settings.html +++ b/resources/public/templates/settings.html @@ -58,6 +58,29 @@ If you change the demo directory path all notes will be deleted. + +
+ Interval to check for network mapped drive changes (minutes) + + + + +
+
+
+ Set to 0 to disable. If the demo directory is not on a network mapped drive this must be disabled (otherwise it creates unnecessary disk activity). +

+ If the demo directory is on a network mapped drive, the indexer may not detect directory changes. + In this case, set this value to the interval in minutes HeadshotBox should check for new demos. +

+
+ diff --git a/src/hsbox/core.clj b/src/hsbox/core.clj index bf7dcba..7f7e62a 100644 --- a/src/hsbox/core.clj +++ b/src/hsbox/core.clj @@ -75,11 +75,7 @@ (when run-indexer? (indexer/set-indexed-path (db/get-demo-directory)) (indexer/set-indexing-state true) - (db/keep-only (->> (db/get-demo-directory) - (clojure.java.io/as-file) - file-seq - (map #(.getCanonicalPath %)) - (filter #(.endsWith % ".dem"))))) + (stats/delete-old-demos)) (stats/init-cache) (hsbox.steamapi/init-steam-stale-days (get options :steamapi-cache 1)) (future (stats/update-players-steam-info)) diff --git a/src/hsbox/indexer.clj b/src/hsbox/indexer.clj index 0865661..5106b22 100644 --- a/src/hsbox/indexer.clj +++ b/src/hsbox/indexer.clj @@ -6,7 +6,8 @@ [hsbox.mynotify :as notify :refer [ENTRY_CREATE ENTRY_MODIFY ENTRY_DELETE]] [hsbox.util :refer [current-timestamp path-exists? last-modified is-dir? is-demo? get-canonical-path]] [taoensso.timbre :as timbre]) - (:import (java.io File))) + (:import (java.util.concurrent.locks ReentrantLock) + (java.util.concurrent TimeUnit))) (timbre/refer-timbre) @@ -97,19 +98,46 @@ ; (wipe-db) ; (update-db))) +(def directory-scan-lock (ReentrantLock.)) +(def directory-scan-cond (.newCondition directory-scan-lock)) +(def directory-scan-interval (atom 0)) + (defn set-config [config] (let [old-demo-dir (db/get-demo-directory) demo-dir (:demo_directory config)] (db/set-config config) + (reset! directory-scan-interval (get config :directory_scan_interval 0)) + (try + (.lock directory-scan-lock) + (.signal directory-scan-cond) + (finally + (.unlock directory-scan-lock))) (when (not= (get-canonical-path old-demo-dir) (get-canonical-path demo-dir)) (db/wipe-demos) (stats/init-cache) (set-indexed-path demo-dir) (add-demo-directory demo-dir)))) +(defn scan-demo-directory [] + (def hsbox.indexer/directory-scan-interval (atom (get (db/get-config) :directory_scan_interval 0))) + (while true + (try + (.lock directory-scan-lock) + (if (zero? @directory-scan-interval) + (.await directory-scan-cond) + (.await directory-scan-cond @directory-scan-interval TimeUnit/MINUTES)) + (when (and (not (zero? @directory-scan-interval)) (is-running?)) + (stats/delete-old-demos) + ; This should delete the old demos from the cache as well + ; But I'm too lazy + (add-demo-directory @current-indexed-path)) + (finally + (.unlock directory-scan-lock))))) + (defn run [] (debug "Indexer started") (future (notify/watch)) + (future (scan-demo-directory)) (while true (doseq [path (map key (filter #(< (+ (val %) grace-period) (current-timestamp)) @paths))] (while (not @indexing-running?) diff --git a/src/hsbox/stats.clj b/src/hsbox/stats.clj index 0909dc5..8b40a30 100644 --- a/src/hsbox/stats.clj +++ b/src/hsbox/stats.clj @@ -678,3 +678,12 @@ (.await api-refresh-cond 1 TimeUnit/HOURS) (finally (.unlock api-refresh-lock))))) + +(defn delete-old-demos + "Delete demos that are not below the demo directory" + [] + (db/keep-only (->> (db/get-demo-directory) + (clojure.java.io/as-file) + file-seq + (map #(.getCanonicalPath %)) + (filter #(.endsWith % ".dem")))))