Skip to content

Commit 1dd2aee

Browse files
committed
remove hot template reloading
1 parent b4291d4 commit 1dd2aee

File tree

10 files changed

+45
-284
lines changed

10 files changed

+45
-284
lines changed

Cargo.lock

+32-208
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

-4
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,6 @@ tempfile = "3.1.0"
9090
tera = { version = "1.5.0", features = ["builtins"] }
9191
walkdir = "2"
9292

93-
# Template hot-reloading
94-
arc-swap = "0.4.6"
95-
notify = "4.0.15"
96-
9793
# Date and Time utilities
9894
chrono = { version = "0.4.11", features = ["serde"] }
9995
time = "0.1" # TODO: Remove once `iron` is removed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,9 @@ cargo run -- build add-essential-files
7575
# This starts the web server but does not build any crates.
7676
# It does not automatically run the migrations, so you need to do that manually (see above).
7777
cargo run -- start-web-server
78-
# If you want the server to automatically reload templates if they are modified:
79-
cargo run -- start-web-server --reload-templates
78+
# If you want the server to automatically restart when code or templates change
79+
# you can use `cargo-watch`:
80+
cargo watch -x "run -- start-web-server"
8081
```
8182

8283
If you need to store big files in the repository's directory it's recommended to

src/bin/cratesfyi.rs

+2-9
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,6 @@ enum CommandLine {
9393
StartWebServer {
9494
#[structopt(name = "SOCKET_ADDR", default_value = "0.0.0.0:3000")]
9595
socket_addr: String,
96-
97-
/// Reload templates when they're changed
98-
#[structopt(long = "reload-templates")]
99-
reload_templates: bool,
10096
},
10197

10298
/// Starts the daemon
@@ -129,12 +125,9 @@ impl CommandLine {
129125

130126
match self {
131127
Self::Build(build) => build.handle_args(ctx)?,
132-
Self::StartWebServer {
133-
socket_addr,
134-
reload_templates,
135-
} => {
128+
Self::StartWebServer { socket_addr } => {
136129
// Blocks indefinitely
137-
let _ = Server::start(Some(&socket_addr), reload_templates, &ctx)?;
130+
let _ = Server::start(Some(&socket_addr), &ctx)?;
138131
}
139132
Self::Daemon { registry_watcher } => {
140133
docs_rs::utils::start_daemon(&ctx, registry_watcher == Toggle::Enabled)?;

src/test/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ pub(crate) struct TestFrontend {
375375
impl TestFrontend {
376376
fn new(context: &dyn Context) -> Self {
377377
Self {
378-
server: Server::start(Some("127.0.0.1:0"), false, context)
378+
server: Server::start(Some("127.0.0.1:0"), context)
379379
.expect("failed to start the web server"),
380380
client: Client::new(),
381381
}

src/utils/daemon.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub fn start_daemon(context: &dyn Context, enable_registry_watcher: bool) -> Res
5252
// Start the web server before doing anything more expensive
5353
// Please check with an administrator before changing this (see #1172 for context).
5454
info!("Starting web server");
55-
let server = crate::Server::start(None, false, context)?;
55+
let server = crate::Server::start(None, context)?;
5656
let server_thread = thread::spawn(|| drop(server));
5757

5858
if enable_registry_watcher {

src/utils/html.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub(crate) fn rewrite_lol(
1818
use lol_html::html_content::{ContentType, Element};
1919
use lol_html::{HtmlRewriter, MemorySettings, Settings};
2020

21-
let templates = templates.templates.load();
21+
let templates = &templates.templates;
2222
let tera_head = templates.render("rustdoc/head.html", &ctx).unwrap();
2323
let tera_vendored_css = templates.render("rustdoc/vendored.html", &ctx).unwrap();
2424
let tera_body = templates.render("rustdoc/body.html", &ctx).unwrap();

src/web/mod.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -415,17 +415,9 @@ pub struct Server {
415415
}
416416

417417
impl Server {
418-
pub fn start(
419-
addr: Option<&str>,
420-
reload_templates: bool,
421-
context: &dyn Context,
422-
) -> Result<Self, Error> {
418+
pub fn start(addr: Option<&str>, context: &dyn Context) -> Result<Self, Error> {
423419
// Initialize templates
424420
let template_data = Arc::new(TemplateData::new(&mut *context.pool()?.get()?)?);
425-
if reload_templates {
426-
TemplateData::start_template_reloading(template_data.clone(), context.pool()?);
427-
}
428-
429421
let server = Self::start_inner(addr.unwrap_or(DEFAULT_BIND), template_data, context)?;
430422
info!("Running docs.rs web server on http://{}", server.addr());
431423
Ok(server)

src/web/page/templates.rs

+4-48
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,10 @@
1-
use crate::{db::Pool, error::Result};
1+
use crate::error::Result;
22
use anyhow::Context;
3-
use arc_swap::ArcSwap;
43
use chrono::{DateTime, Utc};
5-
use notify::{watcher, RecursiveMode, Watcher};
64
use path_slash::PathExt;
75
use postgres::Client;
86
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};
178
use tera::{Result as TeraResult, Tera};
189
use walkdir::WalkDir;
1910

@@ -22,56 +13,21 @@ const TEMPLATES_DIRECTORY: &str = "templates";
2213
/// Holds all data relevant to templating
2314
#[derive(Debug)]
2415
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,
2817
}
2918

3019
impl 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

7733
fn load_rustc_resource_suffix(conn: &mut Client) -> Result<String> {

src/web/page/web_page.rs

-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ pub trait WebPage: Serialize + Sized {
7070
.get::<TemplateData>()
7171
.expect("missing TemplateData from the request extensions")
7272
.templates
73-
.load()
7473
.render(&self.template(), &ctx);
7574

7675
let rendered = if status.is_server_error() {

0 commit comments

Comments
 (0)