Calculate repo count on start and increment when a new repo is cloned
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Valentin Brandl 2019-06-12 21:51:54 +02:00
parent eb718990ec
commit 8d4de48c11
No known key found for this signature in database
GPG Key ID: 30D341DD34118D7D
3 changed files with 33 additions and 0 deletions

24
src/count.rs Normal file
View File

@ -0,0 +1,24 @@
use crate::error::Result;
use std::{fs::read_dir, path::Path, result::Result as StdResult};
pub(crate) fn count_repositories<P>(repo_path: P) -> Result<usize>
where
P: AsRef<Path>,
{
Ok(read_dir(repo_path)?
.filter_map(StdResult::ok)
.filter(|entry| entry.file_type().map(|ft| ft.is_dir()).unwrap_or(false))
.map(|entry| read_dir(entry.path()))
.filter_map(StdResult::ok)
.flat_map(|dir| {
dir.filter_map(StdResult::ok)
.filter(|entry| entry.file_type().map(|ft| ft.is_dir()).unwrap_or(false))
})
.map(|entry| read_dir(entry.path()))
.filter_map(StdResult::ok)
.flat_map(|dir| {
dir.filter_map(StdResult::ok)
.filter(|entry| entry.file_type().map(|ft| ft.is_dir()).unwrap_or(false))
})
.count())
}

View File

@ -1,3 +1,5 @@
#![type_length_limit = "2257138"]
#[macro_use]
extern crate actix_web;
#[macro_use]
@ -9,6 +11,7 @@ extern crate serde_derive;
mod cache;
mod config;
mod count;
mod error;
mod service;
mod statics;
@ -34,6 +37,7 @@ use std::{
fs::create_dir_all,
path::Path,
process::Command,
sync::atomic::Ordering,
sync::Arc,
time::{Duration, SystemTime},
};
@ -169,6 +173,7 @@ fn hoc_request<T: Service>(
let repo = Repository::init_bare(file)?;
repo.remote_add_fetch("origin", "refs/heads/*:refs/heads/*")?;
repo.remote_set_url("origin", &url)?;
REPO_COUNT.fetch_add(1, Ordering::Relaxed);
}
pull(&path)?;
let (hoc, head) = hoc(&service_path, &state.repos, &state.cache)?;

View File

@ -1,3 +1,5 @@
use crate::{config::Opt, count::count_repositories};
use std::sync::atomic::AtomicUsize;
use structopt::StructOpt;
pub struct VersionInfo<'a> {
@ -15,4 +17,6 @@ pub(crate) const FAVICON: &[u8] = include_bytes!("../static/favicon32.png");
lazy_static! {
pub(crate) static ref CLIENT: reqwest::Client = reqwest::Client::new();
pub(crate) static ref OPT: Opt = Opt::from_args();
pub(crate) static ref REPO_COUNT: AtomicUsize =
AtomicUsize::new(count_repositories(&OPT.outdir).unwrap());
}