Better handling of events.
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
use crate::json_util::get_json_string;
|
||||
use std::collections::HashSet;
|
||||
use tokio::sync::mpsc;
|
||||
|
||||
use super::github_endpoint_watcher::GithubEndpointWatcher;
|
||||
@@ -75,9 +77,10 @@ impl<'a> GithubCtl<'a> {
|
||||
let event_stream = self.event_writer.clone();
|
||||
|
||||
tokio::spawn(async move {
|
||||
let mut already_seen_event_ids = HashSet::new();
|
||||
let mut endpoint_watcher = GithubEndpointWatcher::new(username, token, url)
|
||||
.expect("Failed to create endpoint watcher.");
|
||||
endpoint_watcher.skip_results_before_now().await.expect("Failed to fetch initial historical results.");
|
||||
let mut should_notify: bool = false; // Skip the first page of results because theses events already occurred in the past
|
||||
loop {
|
||||
let api_result = match endpoint_watcher.get_results().await {
|
||||
Ok(result) => result,
|
||||
@@ -88,9 +91,17 @@ impl<'a> GithubCtl<'a> {
|
||||
};
|
||||
if let Some(serde_json::Value::Array(events)) = api_result {
|
||||
for event in events {
|
||||
if let Err(_) = event_stream.send(event).await {
|
||||
error!("Receiver dropped.");
|
||||
return;
|
||||
let event_id = event
|
||||
.get("id")
|
||||
.map(get_json_string)
|
||||
.expect("Ran into an event without a id.")
|
||||
.to_owned();
|
||||
|
||||
if should_notify && already_seen_event_ids.insert(event_id) {
|
||||
if let Err(_) = event_stream.send(event).await {
|
||||
error!("Receiver dropped.");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if let None = api_result {
|
||||
@@ -98,6 +109,7 @@ impl<'a> GithubCtl<'a> {
|
||||
} else {
|
||||
error!("Unsupported JSON type.");
|
||||
}
|
||||
should_notify = true;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user