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