M src/straatnaam/core.clj => src/straatnaam/core.clj +11 -6
@@ 1,6 1,6 @@
;; Straatnaam - Expose BAG Extract data to do postcode/number completion
;;
-;; Copyright (C) 2021 Remco van 't Veer
+;; Copyright (C) 2021-2023 Remco van 't Veer
;;
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU Affero General Public License as published by
@@ 40,7 40,9 @@
:feed-url "https://service.pdok.nl/kadaster/adressen/atom/v1_0/adressen.xml"
:retention-success "2"
- :retention-failure "1"})
+ :retention-failure "1"
+
+ :update-time "01:00"})
(defn get-env
[env k]
@@ 87,7 89,9 @@
:port (get-int env :http-port)
:join? false}
- :web {:allowed-origins (get-set env :http-allowed-origins)}}))
+ :web {:allowed-origins (get-set env :http-allowed-origins)}
+
+ :update-time (get-str env :update-time)}))
(defonce server-atom (atom nil))
@@ 112,7 116,8 @@
(defn -main [& _]
(let [config (mk-config environ/env)]
(migratus/migrate (:migratus config))
- (data/start-updater! (:db-spec config)
- (:feed-url config)
- (:retention config))
+ (data/start-updater! {:db (:db-spec config)
+ :feed-url (:feed-url config)
+ :retention (:retention config)
+ :time (:update-time config)})
(start! config)))
M src/straatnaam/data.clj => src/straatnaam/data.clj +18 -5
@@ 1,6 1,6 @@
;; Straatnaam - Expose BAG Extract data to do postcode/number completion
;;
-;; Copyright (C) 2021 Remco van 't Veer
+;; Copyright (C) 2021-2023 Remco van 't Veer
;;
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU Affero General Public License as published by
@@ 25,6 25,7 @@
(:import java.net.URL
java.sql.Timestamp
java.text.SimpleDateFormat
+ (java.time Duration LocalDateTime LocalTime)
java.util.Date))
(defn activate-version!
@@ 117,8 118,6 @@
(log/error "synchronization failed" e)
::failed)))
-(def update-frequency-ms (* 1000 60 60 24))
-
(defonce updater-running (atom false))
(def prune-select "SELECT schema
@@ 141,15 140,29 @@
(log/debug "versions pruned" sns))
(log/info "nothing to prune"))))
+(defn- ms-to-next-update
+ "Calculate milliseconds until next given local `time` (time without a
+ time-zone in the ISO-8601 calendar system, such as \"10:15:30\")."
+ [^String time]
+ (let [time (LocalTime/parse time)
+ date-time (-> (LocalDateTime/now)
+ (.withHour (.getHour time))
+ (.withMinute (.getMinute time))
+ (.withSecond (.getSecond time)))
+ date-time (if (.isAfter date-time (LocalDateTime/now))
+ date-time
+ (.plusDays date-time 1))]
+ (.toMillis (Duration/between (LocalDateTime/now) date-time))))
+
(defn start-updater!
"Start an async updater when not already running."
- [db feed-url retention]
+ [{:keys [db feed-url retention time]}]
(when (compare-and-set! updater-running false true)
(log/info "starting updater")
(async/go-loop []
(prune db retention)
(sync db feed-url)
- (async/<! (async/timeout update-frequency-ms))
+ (async/<! (async/timeout (ms-to-next-update time)))
(recur))))
(defn lookup