Skip to content

Commit bc713db

Browse files
committed
Add lock and unlock subcommands to build
1 parent d3dd76c commit bc713db

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

src/bin/cratesfyi.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,13 @@ pub fn main() {
9191
.required(true)
9292
.help("Version of crate")))
9393
.subcommand(SubCommand::with_name("add-essential-files")
94-
.about("Adds essential files for rustc")))
94+
.about("Adds essential files for rustc"))
95+
.subcommand(SubCommand::with_name("lock")
96+
.about("Locks cratesfyi daemon to stop \
97+
building new crates"))
98+
.subcommand(SubCommand::with_name("unlock")
99+
.about("Unlocks cratesfyi daemon to continue \
100+
building new crates")))
95101
.subcommand(SubCommand::with_name("start-web-server")
96102
.about("Starts web server")
97103
.arg(Arg::with_name("SOCKET_ADDR")
@@ -169,19 +175,24 @@ pub fn main() {
169175

170176
let mut docbuilder = DocBuilder::new(docbuilder_opts);
171177

172-
docbuilder.load_cache().expect("Failed to load cache");
173-
174178
if let Some(_) = matches.subcommand_matches("world") {
179+
docbuilder.load_cache().expect("Failed to load cache");
175180
docbuilder.build_world().expect("Failed to build world");
181+
docbuilder.save_cache().expect("Failed to save cache");
176182
} else if let Some(matches) = matches.subcommand_matches("crate") {
183+
docbuilder.load_cache().expect("Failed to load cache");
177184
docbuilder.build_package(matches.value_of("CRATE_NAME").unwrap(),
178185
matches.value_of("CRATE_VERSION").unwrap())
179186
.expect("Building documentation failed");
187+
docbuilder.save_cache().expect("Failed to save cache");
180188
} else if let Some(_) = matches.subcommand_matches("add-essential-files") {
181189
docbuilder.add_essential_files().expect("Failed to add essential files");
190+
} else if let Some(_) = matches.subcommand_matches("lock") {
191+
docbuilder.lock().expect("Failed to lock");
192+
} else if let Some(_) = matches.subcommand_matches("unlock") {
193+
docbuilder.unlock().expect("Failed to unlock");
182194
}
183195

184-
docbuilder.save_cache().expect("Failed to save cache");
185196
} else if let Some(matches) = matches.subcommand_matches("database") {
186197
if let Some(_) = matches.subcommand_matches("init") {
187198
use std::io::Write;

src/docbuilder/mod.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,27 @@ impl DocBuilder {
8181
}
8282
Ok(())
8383
}
84+
85+
86+
fn lock_path(&self) -> PathBuf {
87+
self.options.prefix.join("cratesfyi.lock")
88+
}
89+
90+
/// Creates a lock file. Daemon will check this lock file and stop operating if its exists.
91+
pub fn lock(&self) -> Result<()> {
92+
let path = self.lock_path();
93+
if !path.exists() {
94+
try!(fs::OpenOptions::new().write(true).create(true).open(path));
95+
}
96+
Ok(())
97+
}
98+
99+
/// Removes lock file.
100+
pub fn unlock(&self) -> Result<()> {
101+
let path = self.lock_path();
102+
if path.exists() {
103+
try!(fs::remove_file(path));
104+
}
105+
Ok(())
106+
}
84107
}

0 commit comments

Comments
 (0)