From 1dd2aeeadc6f95846c979d929b100e7d0581be5e Mon Sep 17 00:00:00 2001 From: Denis Cornehl Date: Sat, 22 Jan 2022 09:32:16 +0100 Subject: [PATCH] remove hot template reloading --- Cargo.lock | 240 +++++--------------------------------- Cargo.toml | 4 - README.md | 5 +- src/bin/cratesfyi.rs | 11 +- src/test/mod.rs | 2 +- src/utils/daemon.rs | 2 +- src/utils/html.rs | 2 +- src/web/mod.rs | 10 +- src/web/page/templates.rs | 52 +-------- src/web/page/web_page.rs | 1 - 10 files changed, 45 insertions(+), 284 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 89416efd6..129f80e46 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -53,7 +53,7 @@ version = "0.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2" dependencies = [ - "winapi 0.3.9", + "winapi", ] [[package]] @@ -65,12 +65,6 @@ dependencies = [ "backtrace", ] -[[package]] -name = "arc-swap" -version = "0.4.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dabe5a181f83789739c194cbe5a897dde195078fac08568d09221fd6137a7ba8" - [[package]] name = "assert-json-diff" version = "1.1.0" @@ -116,7 +110,7 @@ checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" dependencies = [ "hermit-abi", "libc", - "winapi 0.3.9", + "winapi", ] [[package]] @@ -317,7 +311,7 @@ dependencies = [ "num-traits", "serde", "time 0.1.43", - "winapi 0.3.9", + "winapi", ] [[package]] @@ -374,7 +368,7 @@ checksum = "b3616f750b84d8f0de8a58bda93e08e2a81ad3f523089b05f1dffecab48c6cbd" dependencies = [ "atty", "lazy_static", - "winapi 0.3.9", + "winapi", ] [[package]] @@ -776,7 +770,7 @@ checksum = "03d86534ed367a67548dc68113a0f5db55432fdfbb6e6f9d77704397d95d5780" dependencies = [ "libc", "redox_users", - "winapi 0.3.9", + "winapi", ] [[package]] @@ -787,7 +781,7 @@ checksum = "4ebda144c4fe02d1f7ea1a7d9641b6fc6b580adcfa024ae48797ecdeb6825b4d" dependencies = [ "libc", "redox_users", - "winapi 0.3.9", + "winapi", ] [[package]] @@ -801,7 +795,6 @@ name = "docs-rs" version = "0.6.0" dependencies = [ "anyhow", - "arc-swap", "backtrace", "base64 0.13.0", "bzip2", @@ -826,7 +819,6 @@ dependencies = [ "memmap2", "mime_guess 2.0.3", "mockito", - "notify", "once_cell", "path-slash", "postgres", @@ -1002,7 +994,7 @@ dependencies = [ "cfg-if 1.0.0", "libc", "redox_syscall", - "winapi 0.3.9", + "winapi", ] [[package]] @@ -1059,26 +1051,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9564fc758e15025b46aa6643b1b77d047d1a56a1aea6e01002ac0c7026876213" dependencies = [ "libc", - "winapi 0.3.9", -] - -[[package]] -name = "fsevent" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ab7d1bd1bd33cc98b0889831b72da23c0aa4df9cec7e0702f46ecea04b35db6" -dependencies = [ - "bitflags", - "fsevent-sys", -] - -[[package]] -name = "fsevent-sys" -version = "2.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f41b048a94555da0f42f1d632e2e19510084fb8e303b0daa2816e733fb3644a0" -dependencies = [ - "libc", + "winapi", ] [[package]] @@ -1087,22 +1060,6 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" -[[package]] -name = "fuchsia-zircon" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82" -dependencies = [ - "bitflags", - "fuchsia-zircon-sys", -] - -[[package]] -name = "fuchsia-zircon-sys" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7" - [[package]] name = "futf" version = "0.1.4" @@ -1395,7 +1352,7 @@ version = "0.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2456aef2e6b6a9784192ae780c0f15bc57df0e918585282325e8c8ac27737654" dependencies = [ - "winapi 0.3.9", + "winapi", ] [[package]] @@ -1406,7 +1363,7 @@ checksum = "3c731c3e10504cc8ed35cfe2f1db4c9274c3d35fa486e3b31df46f068ef3e867" dependencies = [ "libc", "match_cfg", - "winapi 0.3.9", + "winapi", ] [[package]] @@ -1578,26 +1535,6 @@ dependencies = [ "hashbrown 0.11.2", ] -[[package]] -name = "inotify" -version = "0.7.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4816c66d2c8ae673df83366c18341538f234a26d65a9ecea5c348b453ac1d02f" -dependencies = [ - "bitflags", - "inotify-sys", - "libc", -] - -[[package]] -name = "inotify-sys" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e05c02b5e89bff3b946cedeca278abc628fe811e604f027c45a8aa3cf793d0eb" -dependencies = [ - "libc", -] - [[package]] name = "instant" version = "0.1.12" @@ -1607,15 +1544,6 @@ dependencies = [ "cfg-if 1.0.0", ] -[[package]] -name = "iovec" -version = "0.1.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b2b3ea6ff95e175473f8ffe6a7eb7c00d054240321b84c57051175fe3c1e075e" -dependencies = [ - "libc", -] - [[package]] name = "ipnet" version = "2.3.1" @@ -1686,16 +1614,6 @@ dependencies = [ "wasm-bindgen", ] -[[package]] -name = "kernel32-sys" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" -dependencies = [ - "winapi 0.2.8", - "winapi-build", -] - [[package]] name = "kuchiki" version = "0.8.1" @@ -1974,25 +1892,6 @@ dependencies = [ "autocfg 1.0.1", ] -[[package]] -name = "mio" -version = "0.6.23" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4afd66f5b91bf2a3bc13fad0e21caedac168ca4c707504e75585648ae80e4cc4" -dependencies = [ - "cfg-if 0.1.10", - "fuchsia-zircon", - "fuchsia-zircon-sys", - "iovec", - "kernel32-sys", - "libc", - "log 0.4.14", - "miow 0.2.2", - "net2", - "slab", - "winapi 0.2.8", -] - [[package]] name = "mio" version = "0.7.14" @@ -2001,33 +1900,9 @@ checksum = "8067b404fe97c70829f082dec8bcf4f71225d7eaea1d8645349cb76fa06205cc" dependencies = [ "libc", "log 0.4.14", - "miow 0.3.7", + "miow", "ntapi", - "winapi 0.3.9", -] - -[[package]] -name = "mio-extras" -version = "2.0.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "52403fe290012ce777c4626790c8951324a2b9e3316b3143779c72b029742f19" -dependencies = [ - "lazycell", - "log 0.4.14", - "mio 0.6.23", - "slab", -] - -[[package]] -name = "miow" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ebd808424166322d4a38da87083bfddd3ac4c131334ed55856112eb06d46944d" -dependencies = [ - "kernel32-sys", - "net2", - "winapi 0.2.8", - "ws2_32-sys", + "winapi", ] [[package]] @@ -2036,7 +1911,7 @@ version = "0.3.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b9f1c5b025cda876f66ef43a113f91ebc9f4ccef34843000e0adf6ebbab84e21" dependencies = [ - "winapi 0.3.9", + "winapi", ] [[package]] @@ -2081,17 +1956,6 @@ dependencies = [ "tempfile", ] -[[package]] -name = "net2" -version = "0.2.37" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "391630d12b68002ae1e25e8f974306474966550ad82dac6886fb8910c19568ae" -dependencies = [ - "cfg-if 0.1.10", - "libc", - "winapi 0.3.9", -] - [[package]] name = "new_debug_unreachable" version = "1.0.4" @@ -2128,31 +1992,13 @@ dependencies = [ "version_check 0.9.4", ] -[[package]] -name = "notify" -version = "4.0.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ae03c8c853dba7bfd23e571ff0cff7bc9dceb40a4cd684cd1681824183f45257" -dependencies = [ - "bitflags", - "filetime", - "fsevent", - "fsevent-sys", - "inotify", - "libc", - "mio 0.6.23", - "mio-extras", - "walkdir", - "winapi 0.3.9", -] - [[package]] name = "ntapi" version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f6bb902e437b6d86e03cce10a7e2af662292c5dfef23b65899ea3ac9354ad44" dependencies = [ - "winapi 0.3.9", + "winapi", ] [[package]] @@ -2275,7 +2121,7 @@ dependencies = [ "libc", "redox_syscall", "smallvec 1.8.0", - "winapi 0.3.9", + "winapi", ] [[package]] @@ -2731,7 +2577,7 @@ dependencies = [ "rand_os", "rand_pcg 0.1.2", "rand_xorshift", - "winapi 0.3.9", + "winapi", ] [[package]] @@ -2867,7 +2713,7 @@ checksum = "1166d5c91dc97b88d1decc3285bb0a99ed84b05cfd0bc2341bdf2d43fc41e39b" dependencies = [ "libc", "rand_core 0.4.2", - "winapi 0.3.9", + "winapi", ] [[package]] @@ -2881,7 +2727,7 @@ dependencies = [ "libc", "rand_core 0.4.2", "rdrand", - "winapi 0.3.9", + "winapi", ] [[package]] @@ -2994,7 +2840,7 @@ version = "0.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7" dependencies = [ - "winapi 0.3.9", + "winapi", ] [[package]] @@ -3007,7 +2853,7 @@ dependencies = [ "log 0.4.14", "num_cpus", "rayon", - "winapi 0.3.9", + "winapi", ] [[package]] @@ -3204,7 +3050,7 @@ dependencies = [ "tokio-stream", "toml", "walkdir", - "winapi 0.3.9", + "winapi", ] [[package]] @@ -3257,7 +3103,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f05ba609c234e60bee0d547fe94a4c7e9da733d1c962cf6e59efa4cd9c8bc75" dependencies = [ "lazy_static", - "winapi 0.3.9", + "winapi", ] [[package]] @@ -3683,7 +3529,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0f82496b90c36d70af5fcd482edaa2e0bd16fade569de1330405fecbbdac736b" dependencies = [ "libc", - "winapi 0.3.9", + "winapi", ] [[package]] @@ -3883,7 +3729,7 @@ dependencies = [ "lazy_static", "libc", "nom", - "winapi 0.3.9", + "winapi", ] [[package]] @@ -3908,7 +3754,7 @@ dependencies = [ "libc", "redox_syscall", "remove_dir_all 0.5.3", - "winapi 0.3.9", + "winapi", ] [[package]] @@ -4017,7 +3863,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ca8a50ef2360fbd1eeb0ecd46795a87a19024eb4b53c5dc916ca1fd95fe62438" dependencies = [ "libc", - "winapi 0.3.9", + "winapi", ] [[package]] @@ -4032,7 +3878,7 @@ dependencies = [ "stdweb", "time-macros", "version_check 0.9.4", - "winapi 0.3.9", + "winapi", ] [[package]] @@ -4101,13 +3947,13 @@ dependencies = [ "bytes", "libc", "memchr", - "mio 0.7.14", + "mio", "num_cpus", "once_cell", "pin-project-lite", "signal-hook-registry", "tokio-macros", - "winapi 0.3.9", + "winapi", ] [[package]] @@ -4486,7 +4332,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "808cf2735cd4b6866113f648b791c6adc5714537bc222d9347bb203386ffda56" dependencies = [ "same-file", - "winapi 0.3.9", + "winapi", "winapi-util", ] @@ -4594,12 +4440,6 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f44b95f62d34113cf558c93511ac93027e03e9c29a60dd0fd70e6e025c7270a" -[[package]] -name = "winapi" -version = "0.2.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" - [[package]] name = "winapi" version = "0.3.9" @@ -4610,12 +4450,6 @@ dependencies = [ "winapi-x86_64-pc-windows-gnu", ] -[[package]] -name = "winapi-build" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc" - [[package]] name = "winapi-i686-pc-windows-gnu" version = "0.4.0" @@ -4628,7 +4462,7 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" dependencies = [ - "winapi 0.3.9", + "winapi", ] [[package]] @@ -4643,17 +4477,7 @@ version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0120db82e8a1e0b9fb3345a539c478767c0048d842860994d96113d5b667bd69" dependencies = [ - "winapi 0.3.9", -] - -[[package]] -name = "ws2_32-sys" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d59cefebd0c892fa2dd6de581e937301d8552cb44489cdff035c6187cb63fa5e" -dependencies = [ - "winapi 0.2.8", - "winapi-build", + "winapi", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index ab51921f3..6cb9a0f03 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -90,10 +90,6 @@ tempfile = "3.1.0" tera = { version = "1.5.0", features = ["builtins"] } walkdir = "2" -# Template hot-reloading -arc-swap = "0.4.6" -notify = "4.0.15" - # Date and Time utilities chrono = { version = "0.4.11", features = ["serde"] } time = "0.1" # TODO: Remove once `iron` is removed diff --git a/README.md b/README.md index a392e4d80..1f24af7a9 100644 --- a/README.md +++ b/README.md @@ -75,8 +75,9 @@ cargo run -- build add-essential-files # This starts the web server but does not build any crates. # It does not automatically run the migrations, so you need to do that manually (see above). cargo run -- start-web-server -# If you want the server to automatically reload templates if they are modified: -cargo run -- start-web-server --reload-templates +# If you want the server to automatically restart when code or templates change +# you can use `cargo-watch`: +cargo watch -x "run -- start-web-server" ``` If you need to store big files in the repository's directory it's recommended to diff --git a/src/bin/cratesfyi.rs b/src/bin/cratesfyi.rs index 13413120d..f6dc0b6fc 100644 --- a/src/bin/cratesfyi.rs +++ b/src/bin/cratesfyi.rs @@ -93,10 +93,6 @@ enum CommandLine { StartWebServer { #[structopt(name = "SOCKET_ADDR", default_value = "0.0.0.0:3000")] socket_addr: String, - - /// Reload templates when they're changed - #[structopt(long = "reload-templates")] - reload_templates: bool, }, /// Starts the daemon @@ -129,12 +125,9 @@ impl CommandLine { match self { Self::Build(build) => build.handle_args(ctx)?, - Self::StartWebServer { - socket_addr, - reload_templates, - } => { + Self::StartWebServer { socket_addr } => { // Blocks indefinitely - let _ = Server::start(Some(&socket_addr), reload_templates, &ctx)?; + let _ = Server::start(Some(&socket_addr), &ctx)?; } Self::Daemon { registry_watcher } => { docs_rs::utils::start_daemon(&ctx, registry_watcher == Toggle::Enabled)?; diff --git a/src/test/mod.rs b/src/test/mod.rs index f662f9663..218a664af 100644 --- a/src/test/mod.rs +++ b/src/test/mod.rs @@ -375,7 +375,7 @@ pub(crate) struct TestFrontend { impl TestFrontend { fn new(context: &dyn Context) -> Self { Self { - server: Server::start(Some("127.0.0.1:0"), false, context) + server: Server::start(Some("127.0.0.1:0"), context) .expect("failed to start the web server"), client: Client::new(), } diff --git a/src/utils/daemon.rs b/src/utils/daemon.rs index 8d147a8a1..f98413004 100644 --- a/src/utils/daemon.rs +++ b/src/utils/daemon.rs @@ -52,7 +52,7 @@ pub fn start_daemon(context: &dyn Context, enable_registry_watcher: bool) -> Res // Start the web server before doing anything more expensive // Please check with an administrator before changing this (see #1172 for context). info!("Starting web server"); - let server = crate::Server::start(None, false, context)?; + let server = crate::Server::start(None, context)?; let server_thread = thread::spawn(|| drop(server)); if enable_registry_watcher { diff --git a/src/utils/html.rs b/src/utils/html.rs index bc1597152..a6b364282 100644 --- a/src/utils/html.rs +++ b/src/utils/html.rs @@ -18,7 +18,7 @@ pub(crate) fn rewrite_lol( use lol_html::html_content::{ContentType, Element}; use lol_html::{HtmlRewriter, MemorySettings, Settings}; - let templates = templates.templates.load(); + let templates = &templates.templates; let tera_head = templates.render("rustdoc/head.html", &ctx).unwrap(); let tera_vendored_css = templates.render("rustdoc/vendored.html", &ctx).unwrap(); let tera_body = templates.render("rustdoc/body.html", &ctx).unwrap(); diff --git a/src/web/mod.rs b/src/web/mod.rs index 28c47879b..a810d800a 100644 --- a/src/web/mod.rs +++ b/src/web/mod.rs @@ -415,17 +415,9 @@ pub struct Server { } impl Server { - pub fn start( - addr: Option<&str>, - reload_templates: bool, - context: &dyn Context, - ) -> Result { + pub fn start(addr: Option<&str>, context: &dyn Context) -> Result { // Initialize templates let template_data = Arc::new(TemplateData::new(&mut *context.pool()?.get()?)?); - if reload_templates { - TemplateData::start_template_reloading(template_data.clone(), context.pool()?); - } - let server = Self::start_inner(addr.unwrap_or(DEFAULT_BIND), template_data, context)?; info!("Running docs.rs web server on http://{}", server.addr()); Ok(server) diff --git a/src/web/page/templates.rs b/src/web/page/templates.rs index be7a872ee..6f9192a18 100644 --- a/src/web/page/templates.rs +++ b/src/web/page/templates.rs @@ -1,19 +1,10 @@ -use crate::{db::Pool, error::Result}; +use crate::error::Result; use anyhow::Context; -use arc_swap::ArcSwap; use chrono::{DateTime, Utc}; -use notify::{watcher, RecursiveMode, Watcher}; use path_slash::PathExt; use postgres::Client; use serde_json::Value; -use std::{ - collections::HashMap, - fmt, - path::PathBuf, - sync::{mpsc::channel, Arc}, - thread, - time::Duration, -}; +use std::{collections::HashMap, fmt, path::PathBuf}; use tera::{Result as TeraResult, Tera}; use walkdir::WalkDir; @@ -22,9 +13,7 @@ const TEMPLATES_DIRECTORY: &str = "templates"; /// Holds all data relevant to templating #[derive(Debug)] pub(crate) struct TemplateData { - /// The actual templates, stored in an `ArcSwap` so that they're hot-swappable - // TODO: Conditional compilation so it's not always wrapped, the `ArcSwap` is unneeded overhead for prod - pub templates: ArcSwap, + pub templates: Tera, } impl TemplateData { @@ -32,46 +21,13 @@ impl TemplateData { log::trace!("Loading templates"); let data = Self { - templates: ArcSwap::from_pointee(load_templates(conn)?), + templates: load_templates(conn)?, }; log::trace!("Finished loading templates"); Ok(data) } - - pub(crate) fn start_template_reloading(template_data: Arc, pool: Pool) { - let (tx, rx) = channel(); - // Set a 2 second event debounce for the watcher - let mut watcher = watcher(tx, Duration::from_secs(2)).unwrap(); - - watcher - .watch(TEMPLATES_DIRECTORY, RecursiveMode::Recursive) - .unwrap(); - - thread::spawn(move || { - fn reload(template_data: &TemplateData, pool: &Pool) -> Result<()> { - let mut conn = pool.get()?; - template_data - .templates - .swap(Arc::new(load_templates(&mut conn)?)); - - Ok(()) - } - - // The watcher needs to be moved into the thread so that it's not dropped (when dropped, - // all updates cease) - let _watcher = watcher; - - while rx.recv().is_ok() { - if let Err(err) = reload(&template_data, &pool) { - log::error!("failed to reload templates: {}", err); - } else { - log::info!("reloaded templates"); - } - } - }); - } } fn load_rustc_resource_suffix(conn: &mut Client) -> Result { diff --git a/src/web/page/web_page.rs b/src/web/page/web_page.rs index 472471dd7..b34ba4f18 100644 --- a/src/web/page/web_page.rs +++ b/src/web/page/web_page.rs @@ -70,7 +70,6 @@ pub trait WebPage: Serialize + Sized { .get::() .expect("missing TemplateData from the request extensions") .templates - .load() .render(&self.template(), &ctx); let rendered = if status.is_server_error() {