1
- use crate :: { db :: Pool , error:: Result } ;
1
+ use crate :: error:: Result ;
2
2
use anyhow:: Context ;
3
- use arc_swap:: ArcSwap ;
4
3
use chrono:: { DateTime , Utc } ;
5
- use notify:: { watcher, RecursiveMode , Watcher } ;
6
4
use path_slash:: PathExt ;
7
5
use postgres:: Client ;
8
6
use 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 } ;
17
8
use tera:: { Result as TeraResult , Tera } ;
18
9
use walkdir:: WalkDir ;
19
10
@@ -22,56 +13,21 @@ const TEMPLATES_DIRECTORY: &str = "templates";
22
13
/// Holds all data relevant to templating
23
14
#[ derive( Debug ) ]
24
15
pub ( 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 ,
28
17
}
29
18
30
19
impl TemplateData {
31
20
pub ( crate ) fn new ( conn : & mut Client ) -> Result < Self > {
32
21
log:: trace!( "Loading templates" ) ;
33
22
34
23
let data = Self {
35
- templates : ArcSwap :: from_pointee ( load_templates ( conn) ?) ,
24
+ templates : load_templates ( conn) ?,
36
25
} ;
37
26
38
27
log:: trace!( "Finished loading templates" ) ;
39
28
40
29
Ok ( data)
41
30
}
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
- }
75
31
}
76
32
77
33
fn load_rustc_resource_suffix ( conn : & mut Client ) -> Result < String > {
0 commit comments