Improve fixtures and media loader entry points.

This commit is contained in:
Andrey Antukh 2020-04-08 13:54:30 +02:00
parent a9b2951d8b
commit d737069ef9
4 changed files with 50 additions and 22 deletions

View file

@ -67,7 +67,7 @@
returning id;") returning id;")
(def sql:create-icon-library (def sql:create-icon-library
"insert into icon_library (team_id, name) "insert into icon_library (team_id, name)
values ($1, $2) values ($1, $2)
returning id;") returning id;")
@ -87,7 +87,7 @@
:num-draft-files-per-profile 10 :num-draft-files-per-profile 10
:num-draft-pages-per-file 3}) :num-draft-pages-per-file 3})
(defn rng-ids (defn- rng-ids
[rng n max] [rng n max]
(let [stream (->> (.longs rng 0 max) (let [stream (->> (.longs rng 0 max)
(.iterator) (.iterator)
@ -99,19 +99,19 @@
#{} #{}
stream))) stream)))
(defn rng-vec (defn- rng-vec
[rng vdata n] [rng vdata n]
(let [ids (rng-ids rng n (count vdata))] (let [ids (rng-ids rng n (count vdata))]
(mapv #(nth vdata %) ids))) (mapv #(nth vdata %) ids)))
(defn rng-nth (defn- rng-nth
[rng vdata] [rng vdata]
(let [stream (->> (.longs rng 0 (count vdata)) (let [stream (->> (.longs rng 0 (count vdata))
(.iterator) (.iterator)
(iterator-seq))] (iterator-seq))]
(nth vdata (first stream)))) (nth vdata (first stream))))
(defn collect (defn- collect
[f items] [f items]
(reduce (fn [acc n] (reduce (fn [acc n]
(p/then acc (fn [acc] (p/then acc (fn [acc]
@ -121,7 +121,7 @@
(p/promise []) (p/promise [])
items)) items))
(defn run (defn impl-run
[opts] [opts]
(let [rng (java.util.Random. 1) (let [rng (java.util.Random. 1)
@ -269,6 +269,17 @@
(assign-teams-and-profiles conn teams (map :id profiles)) (assign-teams-and-profiles conn teams (map :id profiles))
(p/run! (partial create-draft-files conn) profiles))))) (p/run! (partial create-draft-files conn) profiles)))))
(defn run
[preset]
(let [preset (if (map? preset)
preset
(case preset
(nil "small" :small) preset-small
;; "medium" preset-medium
;; "big" preset-big
preset-small))]
(deref (impl-run preset))))
(defn -main (defn -main
[& args] [& args]
(try (try
@ -277,12 +288,7 @@
#'uxbox.db/pool #'uxbox.db/pool
#'uxbox.migrations/migrations}) #'uxbox.migrations/migrations})
(mount/start)) (mount/start))
(let [preset (case (first args)
(nil "small") preset-small (run (first args))
;; "medium" preset-medium
;; "big" preset-big
preset-small)]
(log/info "Using preset:" (pr-str preset))
(deref (run preset)))
(finally (finally
(mount/stop)))) (mount/stop))))

View file

@ -310,8 +310,7 @@
(defn- read-file (defn- read-file
[path] [path]
(let [path (validate-path path) (let [reader (java.io.PushbackReader. (io/reader path))]
reader (java.io.PushbackReader. (io/reader path))]
[(fs/parent path) [(fs/parent path)
(read reader)])) (read reader)]))
@ -335,11 +334,16 @@
(p/run! #(process-colors-library conn %) colors) (p/run! #(process-colors-library conn %) colors)
nil))) nil)))
(defn run
[path]
(p/let [[basedir data] (read-file path)]
(db/with-atomic [conn db/pool]
(importer conn basedir data))))
(defn -main (defn -main
[& [path]] [& [path]]
(let [[basedir data] (read-file path)] (let [path (validate-path path)]
(start-system) (start-system)
(-> (db/with-atomic [conn db/pool] (-> (run path)
(importer conn basedir data)) (p/finally (fn [_ _] (stop-system))))))
(p/finally (fn [_ _]
(stop-system))))))

View file

@ -10,9 +10,8 @@ This is a development feature that allows populate the database with a
good amount of random content (usually used for just test the good amount of random content (usually used for just test the
application or perform performance tweaks on queries). application or perform performance tweaks on queries).
In order to load fixtures, enter to the REPL environment executing the In order to load fixtures, enter to the REPL environment executing the
`bin/repl` script, and then execute `(uxbox.fixtures/-main)`. `bin/repl` script, and then execute `(uxbox.fixtures/run :small)`.
You also can execute this as a standalone script with: You also can execute this as a standalone script with:

View file

@ -36,6 +36,18 @@ respective defaults):
- `UXBOX_DEBUG_HUMANIZE_TRANSIT=true` - `UXBOX_DEBUG_HUMANIZE_TRANSIT=true`
## REPL ##
The production environment by default starts a server REPL where you
can connect and perform diagnosis operations. For this you will need
`netcat` or `telnet` installed in the server.
```bash
$ rlwrap netcat localhost 5555
user=>
```
## Collections import ## ## Collections import ##
This is the way we can preload default collections of images and icons to the This is the way we can preload default collections of images and icons to the
@ -64,3 +76,10 @@ Then, you need to execute:
```bash ```bash
clojure -Adev -m uxbox.media-loader ../path/to/config.edn clojure -Adev -m uxbox.media-loader ../path/to/config.edn
``` ```
If you have a REPL access to the running process, you can execute it from there:
```clojure
(require 'uxbox.media-loader)
@(uxbox.media-loader/run "/path/to/config.edn")
```