Skip to content

Commit

Permalink
impl record & get last polling time
Browse files Browse the repository at this point in the history
  • Loading branch information
Pugma committed Oct 3, 2024
1 parent 1ae0007 commit 3d75385
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
14 changes: 11 additions & 3 deletions server/app/src/traq.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use anyhow::{Ok, Result};
use log::info;
use anyhow::Result;
use chrono::{SecondsFormat, Utc};
use log::{error, info};
use std::{env, sync::LazyLock};
use tokio::{time, time::Duration};
use traq::apis::configuration::Configuration;
Expand All @@ -22,11 +23,18 @@ pub async fn start_polling(repo: Repository) -> Result<()> {
let mut interval = time::interval(Duration::new(180, 0));
interval.tick().await;

let mut last_checkpoint = if let Ok(point) = repo.get_time().await {
point
} else {
error!("Couldn't get last checkpoint!");
Utc::now().to_rfc3339_opts(SecondsFormat::Nanos, true)
};

loop {
interval.tick().await;

info!("start polling ...");
let _ = message::collect(&repo, &CONFIG).await;
let _ = message::collect(&repo, &CONFIG, &mut last_checkpoint).await;
}
})
.await?;
Expand Down
10 changes: 8 additions & 2 deletions server/app/src/traq/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ use crate::repo::Repository;

use super::MESSAGE_LIMIT;

pub(super) async fn collect(repo: &Repository, config: &Configuration) -> Result<()> {
pub(super) async fn collect(
repo: &Repository,
config: &Configuration,
checkpoint: &mut String,
) -> Result<()> {
if let Some(token) = config.bearer_access_token.clone() {
debug!("bot_access_token is Some object");
if token == *"" {
Expand Down Expand Up @@ -53,11 +57,13 @@ pub(super) async fn collect(repo: &Repository, config: &Configuration) -> Result

// check whether all messages are retrieved
if MESSAGE_LIMIT * (page + 1) >= result.total_hits as i32 {
*checkpoint = hit_messages.last().unwrap().created_at.clone();
info!("Updated last_checkpoint = {}", *checkpoint);
break;
}
}

repo.record_time(now).await?;
repo.record_time(checkpoint.clone()).await?;

Ok(())
}

0 comments on commit 3d75385

Please sign in to comment.