Skip to content

Commit

Permalink
Option to poll demo directory for changes. Closes #197
Browse files Browse the repository at this point in the history
Useful for people using network filesystems where inotify might not work.
  • Loading branch information
bugdone committed May 6, 2017
1 parent 0840d3f commit 431951e
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 6 deletions.
1 change: 1 addition & 0 deletions resources/public/js/controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
23 changes: 23 additions & 0 deletions resources/public/templates/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,29 @@
If you change the demo directory path <b>all</b> notes will be deleted.
</div>
</div>

<div class="form-group input-group">
<span class="input-group-addon">Interval to check for network mapped drive changes (minutes)</span>
<input type="number"
min="0"
class="form-control"
ng-model="$root.config.directory_scan_interval">
<span class="input-group-addon">
<button type="button"
class="btn btn-default btn-xs glyphicon glyphicon-question-sign"
ng-click="directoryScanIntervalCollapsed = !directoryScanIntervalCollapsed">
</button>
</span>
</div>
<div collapse="directoryScanIntervalCollapsed">
<div class="well well-lg text-left">
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).
<p/>
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.
</div>
</div>

</tab>


Expand Down
6 changes: 1 addition & 5 deletions src/hsbox/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
30 changes: 29 additions & 1 deletion src/hsbox/indexer.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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?)
Expand Down
9 changes: 9 additions & 0 deletions src/hsbox/stats.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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")))))

0 comments on commit 431951e

Please sign in to comment.