Skip to content

Commit

Permalink
remove hot template reloading
Browse files Browse the repository at this point in the history
  • Loading branch information
syphar committed Jan 22, 2022
1 parent b4291d4 commit 1dd2aee
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 284 deletions.
240 changes: 32 additions & 208 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 0 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 2 additions & 9 deletions src/bin/cratesfyi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)?;
Expand Down
2 changes: 1 addition & 1 deletion src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
10 changes: 1 addition & 9 deletions src/web/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,17 +415,9 @@ pub struct Server {
}

impl Server {
pub fn start(
addr: Option<&str>,
reload_templates: bool,
context: &dyn Context,
) -> Result<Self, Error> {
pub fn start(addr: Option<&str>, context: &dyn Context) -> Result<Self, Error> {
// 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)
Expand Down
52 changes: 4 additions & 48 deletions src/web/page/templates.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -22,56 +13,21 @@ 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<Tera>,
pub templates: Tera,
}

impl TemplateData {
pub(crate) fn new(conn: &mut Client) -> Result<Self> {
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<TemplateData>, 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<String> {
Expand Down
1 change: 0 additions & 1 deletion src/web/page/web_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ pub trait WebPage: Serialize + Sized {
.get::<TemplateData>()
.expect("missing TemplateData from the request extensions")
.templates
.load()
.render(&self.template(), &ctx);

let rendered = if status.is_server_error() {
Expand Down

0 comments on commit 1dd2aee

Please sign in to comment.