bot: GC expired recent hooks at each call of the method
Signed-off-by: pep <pep@bouah.net>
This commit is contained in:
parent
0746afb443
commit
284aa7dfd1
1 changed files with 13 additions and 9 deletions
22
src/bot.rs
22
src/bot.rs
|
|
@ -16,7 +16,7 @@
|
||||||
use crate::Error;
|
use crate::Error;
|
||||||
use crate::hooks::{Hook, IssueAction, MergeRequestAction, format_hook};
|
use crate::hooks::{Hook, IssueAction, MergeRequestAction, format_hook};
|
||||||
|
|
||||||
use chrono::TimeDelta;
|
use chrono::{TimeDelta, Utc};
|
||||||
use log::debug;
|
use log::debug;
|
||||||
use tokio::{signal::ctrl_c, sync::mpsc};
|
use tokio::{signal::ctrl_c, sync::mpsc};
|
||||||
use xmpp::jid::{BareJid, Jid, ResourcePart};
|
use xmpp::jid::{BareJid, Jid, ResourcePart};
|
||||||
|
|
@ -137,6 +137,15 @@ impl XmppClient {
|
||||||
// Do we need to remove/update the hook?
|
// Do we need to remove/update the hook?
|
||||||
let mut removal: Option<usize> = None;
|
let mut removal: Option<usize> = None;
|
||||||
|
|
||||||
|
let datetime = Utc::now() - TimeDelta::minutes(5);
|
||||||
|
|
||||||
|
// Remove expired hooks (more than time delta)
|
||||||
|
self.recent_hooks.retain(|hook| match hook {
|
||||||
|
Hook::MergeRequest(hook) if hook.updated_at > datetime => false,
|
||||||
|
Hook::Issue(hook) if hook.updated_at > datetime => false,
|
||||||
|
_ => true,
|
||||||
|
});
|
||||||
|
|
||||||
for (i, old_hook) in self.recent_hooks.iter().enumerate() {
|
for (i, old_hook) in self.recent_hooks.iter().enumerate() {
|
||||||
match (old_hook, &new_hook) {
|
match (old_hook, &new_hook) {
|
||||||
(_, &Hook::MergeRequest(ref new))
|
(_, &Hook::MergeRequest(ref new))
|
||||||
|
|
@ -151,16 +160,13 @@ impl XmppClient {
|
||||||
// Action is MergeRequestAction::Update, otherwise it would have matched the other branch
|
// Action is MergeRequestAction::Update, otherwise it would have matched the other branch
|
||||||
// and the method would have returned.
|
// and the method would have returned.
|
||||||
if old.id == new.id {
|
if old.id == new.id {
|
||||||
if old.author.name == new.author.name
|
if old.author.name == new.author.name {
|
||||||
&& old.updated_at > (new.updated_at - TimeDelta::minutes(5))
|
|
||||||
{
|
|
||||||
// If everything matches and we're still within the time frame, let the old hook
|
// If everything matches and we're still within the time frame, let the old hook
|
||||||
// expire, don't update it.
|
// expire, don't update it.
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
// The old hook either doesn't match the new author (we want to announce messages
|
// The old hook either doesn't match the new author (we want to announce messages
|
||||||
// from different authors), or has expired, and in both cases can be replaced by the
|
// from different authors), let it be replaced by the new hook.
|
||||||
// new hook.
|
|
||||||
removal = Some(i);
|
removal = Some(i);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
@ -169,9 +175,7 @@ impl XmppClient {
|
||||||
(&Hook::Issue(ref old), &Hook::Issue(ref new)) => {
|
(&Hook::Issue(ref old), &Hook::Issue(ref new)) => {
|
||||||
// See the MergeRequest branch for comments
|
// See the MergeRequest branch for comments
|
||||||
if old.id == new.id {
|
if old.id == new.id {
|
||||||
if old.author.name == new.author.name
|
if old.author.name == new.author.name {
|
||||||
&& old.updated_at > (new.updated_at - TimeDelta::minutes(5))
|
|
||||||
{
|
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
removal = Some(i);
|
removal = Some(i);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue