5
5
Glob,
6
6
TOML
7
7
8
+ using Serialization
9
+
8
10
using DataFrames
9
11
using OrderedCollections
10
12
using Memoization
@@ -38,18 +40,38 @@ function get_regions()
38
40
return regions
39
41
end
40
42
43
+ """
44
+ setup_regional_data(config::Dict)
45
+
46
+ Load regional data to act as an in-memory cache.
47
+
48
+ # Arguments
49
+ - `config` : Configuration settings, typically loaded from a TOML file.
50
+ - `reef_data_path` : Path to pre-prepared reef data
51
+
52
+ # Returns
53
+ OrderedDict of `RegionalCriteria` for each region.
54
+ """
41
55
function setup_regional_data (config:: Dict )
42
- return setup_regional_data (config[" prepped_data" ][" PREPPED_DATA_DIR" ])
43
- end
44
- function setup_regional_data (reef_data_path:: String )
45
56
if @isdefined (REGIONAL_DATA)
46
57
@debug " Using previously generated regional data store."
47
58
sleep (1 ) # Pause so message is noticeably visible
48
59
return REGIONAL_DATA
49
60
end
50
61
62
+ # Check disk-based store
63
+ reg_cache_dir = config[" server_config" ][" REGIONAL_CACHE_DIR" ]
64
+ reg_cache_fn = joinpath (reg_cache_dir, " regional_cache.dat" )
65
+ if isfile (reg_cache_fn)
66
+ @debug " Loading regional data cache from disk"
67
+ @eval const REGIONAL_DATA = deserialize ($ (reg_cache_fn))
68
+ return REGIONAL_DATA
69
+ end
70
+
51
71
@debug " Setting up regional data store..."
52
72
73
+ reef_data_path = config[" prepped_data" ][" PREPPED_DATA_DIR" ]
74
+
53
75
regional_assessment_data = OrderedDict {String, RegionalCriteria} ()
54
76
for reg in get_regions ()
55
77
data_paths = String[]
@@ -75,7 +97,8 @@ function setup_regional_data(reef_data_path::String)
75
97
elseif occursin (" flat" , string (dp))
76
98
flat_table = GeoParquet. read (parq_file)
77
99
else
78
- error (" Unknown lookup found: $(parq_file) " )
100
+ msg = " Unknown lookup found: $(parq_file) . Must be 'slope' or 'flat'"
101
+ throw (ArgumentError (msg))
79
102
end
80
103
end
81
104
end
@@ -97,19 +120,28 @@ function setup_regional_data(reef_data_path::String)
97
120
)
98
121
end
99
122
123
+ # Store cache on disk to avoid excessive cold startup times
124
+ @debug " Saving regional data cache to disk"
125
+ serialize (reg_cache_fn, regional_assessment_data)
126
+
100
127
# Remember, `@eval` runs in global scope.
101
128
@eval const REGIONAL_DATA = $ (regional_assessment_data)
102
129
103
130
return REGIONAL_DATA
104
131
end
105
132
106
- function _cache_location (config)
133
+ """
134
+ _cache_location(config::Dict)::String
135
+
136
+ Retrieve cache location for geotiffs.
137
+ """
138
+ function _cache_location (config:: Dict ):: String
107
139
cache_loc = try
108
140
in_debug = " DEBUG_MODE" in config[" server_config" ]
109
141
if in_debug && lowercase (config[" server_config" ][" DEBUG_MODE" ]) == " true"
110
142
mktempdir ()
111
143
else
112
- config[" server_config" ][" CACHE_DIR " ]
144
+ config[" server_config" ][" TIFF_CACHE_DIR " ]
113
145
end
114
146
catch
115
147
mktempdir ()
@@ -118,7 +150,12 @@ function _cache_location(config)
118
150
return cache_loc
119
151
end
120
152
121
- function n_gdal_threads (config):: String
153
+ """
154
+ n_gdal_threads(config::Dict)::String
155
+
156
+ Retrieve the configured number of threads to use when writing COGs with GDAL.
157
+ """
158
+ function n_gdal_threads (config:: Dict ):: String
122
159
n_cog_threads = try
123
160
config[" server_config" ][" COG_THREADS" ]
124
161
catch
@@ -128,7 +165,12 @@ function n_gdal_threads(config)::String
128
165
return n_cog_threads
129
166
end
130
167
131
- function tile_size (config):: Tuple
168
+ """
169
+ tile_size(config::Dict)::Tuple
170
+
171
+ Retrieve the configured size of map tiles in pixels (width and height / lon and lat).
172
+ """
173
+ function tile_size (config:: Dict ):: Tuple
132
174
tile_dims = try
133
175
res = parse (Int, config[" server_config" ][" TILE_SIZE" ])
134
176
(res, res)
@@ -145,8 +187,19 @@ function get_auth_middleware(config :: Dict)
145
187
return [auth]
146
188
end
147
189
190
+ """
191
+ warmup_cache(config_path::String)
192
+
193
+ Invokes warm up of regional data cache to reduce later spin up times.
194
+ """
195
+ function warmup_cache (config_path:: String )
196
+ config = TOML. parsefile (config_path)
197
+ setup_regional_data (config)
198
+ end
199
+
200
+
148
201
function start_server (config_path)
149
- @info " Launching server...please wait"
202
+ @info " Launching server... please wait"
150
203
151
204
@info " Parsing configuration from $(config_path) ..."
152
205
config = TOML. parsefile (config_path)
@@ -163,6 +216,13 @@ function start_server(config_path)
163
216
164
217
@info " Setting up tile routes..."
165
218
setup_tile_routes (auth)
219
+
220
+ @info " Setting up region routes..."
221
+ setup_region_routes (config)
222
+ @info " Completed region routes setup."
223
+
224
+ @info " Setting up tile routes..."
225
+ setup_tile_routes ()
166
226
@info " Completed tile routes setup."
167
227
168
228
@info " Initialisation complete, starting server on port 8000."
0 commit comments