1- use crate :: { db :: Pool , error:: Result } ;
1+ use crate :: error:: Result ;
22use anyhow:: Context ;
3- use arc_swap:: ArcSwap ;
43use chrono:: { DateTime , Utc } ;
5- use notify:: { watcher, RecursiveMode , Watcher } ;
64use path_slash:: PathExt ;
75use postgres:: Client ;
86use serde_json:: Value ;
9- use std:: {
10- collections:: HashMap ,
11- fmt,
12- path:: PathBuf ,
13- sync:: { mpsc:: channel, Arc } ,
14- thread,
15- time:: Duration ,
16- } ;
7+ use std:: { collections:: HashMap , fmt, path:: PathBuf } ;
178use tera:: { Result as TeraResult , Tera } ;
189use walkdir:: WalkDir ;
1910
@@ -22,56 +13,21 @@ const TEMPLATES_DIRECTORY: &str = "templates";
2213/// Holds all data relevant to templating
2314#[ derive( Debug ) ]
2415pub ( crate ) struct TemplateData {
25- /// The actual templates, stored in an `ArcSwap` so that they're hot-swappable
26- // TODO: Conditional compilation so it's not always wrapped, the `ArcSwap` is unneeded overhead for prod
27- pub templates : ArcSwap < Tera > ,
16+ pub templates : Tera ,
2817}
2918
3019impl TemplateData {
3120 pub ( crate ) fn new ( conn : & mut Client ) -> Result < Self > {
3221 log:: trace!( "Loading templates" ) ;
3322
3423 let data = Self {
35- templates : ArcSwap :: from_pointee ( load_templates ( conn) ?) ,
24+ templates : load_templates ( conn) ?,
3625 } ;
3726
3827 log:: trace!( "Finished loading templates" ) ;
3928
4029 Ok ( data)
4130 }
42-
43- pub ( crate ) fn start_template_reloading ( template_data : Arc < TemplateData > , pool : Pool ) {
44- let ( tx, rx) = channel ( ) ;
45- // Set a 2 second event debounce for the watcher
46- let mut watcher = watcher ( tx, Duration :: from_secs ( 2 ) ) . unwrap ( ) ;
47-
48- watcher
49- . watch ( TEMPLATES_DIRECTORY , RecursiveMode :: Recursive )
50- . unwrap ( ) ;
51-
52- thread:: spawn ( move || {
53- fn reload ( template_data : & TemplateData , pool : & Pool ) -> Result < ( ) > {
54- let mut conn = pool. get ( ) ?;
55- template_data
56- . templates
57- . swap ( Arc :: new ( load_templates ( & mut conn) ?) ) ;
58-
59- Ok ( ( ) )
60- }
61-
62- // The watcher needs to be moved into the thread so that it's not dropped (when dropped,
63- // all updates cease)
64- let _watcher = watcher;
65-
66- while rx. recv ( ) . is_ok ( ) {
67- if let Err ( err) = reload ( & template_data, & pool) {
68- log:: error!( "failed to reload templates: {}" , err) ;
69- } else {
70- log:: info!( "reloaded templates" ) ;
71- }
72- }
73- } ) ;
74- }
7531}
7632
7733fn load_rustc_resource_suffix ( conn : & mut Client ) -> Result < String > {
0 commit comments