Skip to content

Commit

Permalink
wip: update config schema/examples for file_stores
Browse files Browse the repository at this point in the history
Specifically, Config.Rdb_dest.file_stores. This code is untested but
compiles up to the point that something outside the config tries to
access the old files_path field.

issue #4
  • Loading branch information
donut committed Feb 23, 2022
1 parent 6c41ec0 commit e9a74a0
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 8 deletions.
19 changes: 16 additions & 3 deletions _env/config.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,22 @@
, "log_namespace" : "Source" }

, "rdb_dest" :
{ // Absolute path to where the video and thumbnail files should be
// downloaded to within the Docker container.
"files_path" : "/data/files"
{ // Where to store files. Multiple locations can be specified to accomodate
// spanning multiple volumes. The system distributes files based on order
// of location definition (first to last), available space, and max allowed
// usage.
//
// Each location is defined by a "path" and "max_usage". Path should be
// absolute. Max usage follows the PHP-style suffixes of K, M, G, and T
// (kilo-, mega-, giga-, and terabytes respectively). Case-insensitive. If
// no suffix is given, then the number will be taken as bytes. Setting
// "max_usage" to `null` means use as much storage as is available.
//
// See https://www.php.net/manual/en/faq.using.php#faq.using.shorthandbytes
"file_stores" :
[ {"path": "/data/files/volume_a", "max_usage": "3G"}
, {"path": "/data/files/volume_b", "max_usage": "4G"}
, {"path": "/data/files/volume_c", "max_usage": null} ]
, "log_level" : "info"
, "log_namespace" : "Dest" }

Expand Down
2 changes: 1 addition & 1 deletion app/bin/config/dune
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
(library
(name config)
(libraries atdgen batteries))
(libraries atdgen batteries re re.perl))
9 changes: 9 additions & 0 deletions app/bin/config/file_store.atd
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@


type max_usage = string option wrap
<ocaml module="Rdb_dest.File_store.Max_usage">

type t =
{ path : string
; max_usage : max_usage
<ocaml valid="Rdb_dest.File_store.Max_usage.validate"> }
3 changes: 2 additions & 1 deletion app/bin/config/rdb_dest.atd
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@

type log_level <ocaml from="Log_level" t="t"> = abstract
type file_store <ocaml from="File_store" t="t"> = abstract

type t =
{ files_path : string
{ file_stores : file_store list
; log_level : log_level
; log_namespace : string }
60 changes: 60 additions & 0 deletions app/bin/config/rdb_dest.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@


module File_store = struct
module Max_usage = struct
type t = int option

let valid_pattern = "^([1-9]\\d*)([kmgt])?$" |> Re.Perl.compile_pat

let clean s =
let separators = "[_, ]+" |> Re.Perl.compile_pat in
s
|> Re.replace_string separators ~by:""
|> String.lowercase_ascii

let validate s =
match s with
| None -> true
| Some s ->
s |> clean |> Re.execp valid_pattern

let wrap s =
if not (validate s) then failwith "Invalid byte string."
else

match s with
| None ->
None

| Some s -> begin
let matches = s |> clean |> Re.exec valid_pattern in
match matches |> Re.Group.all with
| [| _; amount; "" |] ->
Some (amount |> int_of_string)

| [| _; amount; suffix |] -> begin
let amount = amount |> int_of_string in
match suffix with
| "k" -> Some (amount * 1024)
| "m" -> Some (amount * 1024 * 1024)
| "g" -> Some (amount * 1024 * 1024 * 1024)
| "t" -> Some (amount * 1024 * 1024 * 1024 * 1024)
| suffix ->
Printf.sprintf "Unknown suffix '%s'" suffix
|> failwith
end

| _ ->
failwith "Unexpected array structure returned from Re.Group.all"
end

let unwrap t =
match t with
| None ->
None

| Some b ->
Some (b |> string_of_int)

end
end
6 changes: 4 additions & 2 deletions docker-compose.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ services:
# Required for building.
- ./app:${APP_PREFIX_DIR}/app

# Required for running app
# - ./_env/config.json:${APP_PREFIX_DIR}/_env/config.json:ro
# File stores
- ./_data/app/store_a:${APP_PREFIX_DIR}/_data/store_a
- ./_data/app/store_b:${APP_PREFIX_DIR}/_data/store_b
- /Volumes/WILLY/OVPSync:${APP_PREFIX_DIR}/_data/store_c

db:
ports: [ "3306:3306" ]
Expand Down
6 changes: 5 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ services:
volumes:
- ./app/entrypoint.sh:${APP_PREFIX_DIR}/entrypoint.sh
- ./_env/config.json:${APP_PREFIX_DIR}/_env/config.json:ro
- ./_data/app:${APP_PREFIX_DIR}/_data
# File store volumes should be defined in docker-compose.override.yml.
# They need to match up with the [rdb_dest.file_store] parameter in
# _env/config.json. Below values are just examples.
# - ./_data/app:${APP_PREFIX_DIR}/_data/volume_a
# - /Volumes/OVPSync_extra:${APP_PREFIX_DIR}/_data/volume_b
depends_on:
- db

Expand Down

0 comments on commit e9a74a0

Please sign in to comment.