Skip to content

Commit

Permalink
allow customize crawl interval
Browse files Browse the repository at this point in the history
  • Loading branch information
fanzeyi committed Aug 17, 2020
1 parent 107be55 commit e27237e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 37 deletions.
65 changes: 31 additions & 34 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,30 +215,37 @@ impl GroupCommand {
}
}

#[derive(Debug, StructOpt)]
pub struct ServerConfig {
#[structopt(short = "H", long = "host", default_value = "127.0.0.1")]
/// Specifies server host
host: String,

#[structopt(short = "p", long = "port", default_value = "4000")]
/// Specifies alternate port
port: u32,

#[structopt(short = "u", long = "username", requires = "password")]
/// Specifies authentication username
username: Option<String>,

#[structopt(short = "P", long = "password", requires = "username")]
/// Specifies authentication password
password: Option<String>,

#[structopt(short = "i", long = "interval", default_value = "30")]
/// Specifies crawl interval (unit: minutes)
interval: u32,
}

#[derive(Debug, StructOpt)]
pub enum SubCommand {
/// Manages feeds
Feed(FeedCommand),
/// Manages group
Group(GroupCommand),
/// Starts web server
Server {
#[structopt(short = "H", long = "host", default_value = "127.0.0.1")]
/// Specifies host of server
host: String,

#[structopt(short = "p", long = "port", default_value = "4000")]
/// Specifies port of server
port: u32,

#[structopt(short = "u", long = "username", requires = "password")]
/// Specifies username used in authentication
username: Option<String>,

#[structopt(short = "P", long = "password", requires = "username")]
/// Specifies password used in authentication
password: Option<String>,
},
Server(ServerConfig),
}

#[derive(StructOpt, Debug)]
Expand All @@ -257,23 +264,18 @@ pub struct Options {
}

impl Options {
async fn server(
mut state: State,
host: String,
port: u32,
username: Option<String>,
password: Option<String>,
) -> Result<()> {
if let Some(username) = username {
if let Some(password) = password {
async fn server(mut state: State, config: ServerConfig) -> Result<()> {
if let Some(username) = config.username {
if let Some(password) = config.password {
state = state.set_credential(username, password);
}
}

let app = crate::api::make_app(state.clone());
let crwaler = crate::crawler::Crawler::new(state);
let crawl_interval = ((config.interval) * 60) as u64;
let crwaler = crate::crawler::Crawler::new(state, crawl_interval);
let (web, crawl) = app
.listen(format!("{}:{}", host, port))
.listen(format!("{}:{}", config.host, config.port))
.join(crwaler.runloop())
.await;
(web?, crawl?);
Expand All @@ -287,12 +289,7 @@ impl Options {
match self.command {
SubCommand::Feed(cmd) => cmd.run(state).await,
SubCommand::Group(cmd) => cmd.run(state).await,
SubCommand::Server {
host,
port,
username,
password,
} => Self::server(state, host, port, username, password).await,
SubCommand::Server(config) => Self::server(state, config).await,
}
}
}
10 changes: 7 additions & 3 deletions src/crawler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ use std::time::Duration;

pub struct Crawler {
state: State,
interval_secs: u64,
}

impl Crawler {
pub fn new(state: State) -> Self {
Crawler { state }
pub fn new(state: State, interval_secs: u64) -> Self {
Crawler {
state,
interval_secs,
}
}

async fn crawl(&self) -> Result<()> {
Expand All @@ -31,7 +35,7 @@ impl Crawler {
}

pub async fn runloop(self) -> Result<()> {
let mut interval = stream::interval(Duration::from_secs(300));
let mut interval = stream::interval(Duration::from_secs(self.interval_secs));
while let Some(_) = interval.next().await {
match self.crawl().await {
Ok(_) => (),
Expand Down

0 comments on commit e27237e

Please sign in to comment.