Skip to content

Commit d378745

Browse files
committed
introduce lock in command_executor_service
1 parent 31124f0 commit d378745

2 files changed

Lines changed: 20 additions & 20 deletions

File tree

backend/src/commands/command_executor_service.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ use regex::{Regex, RegexBuilder};
88
use serde::{Deserialize, Serialize};
99
use sqlx::Type;
1010
use std::collections::HashMap;
11+
use std::fs::read;
1112
use std::pin::Pin;
1213
use std::sync::Arc;
1314
use std::time::Instant;
15+
use tokio::sync::RwLock;
1416
// ChatMessage
1517

1618
#[allow(dead_code)]
@@ -105,18 +107,17 @@ static TEXT_COMMAND_CALLBACK: TriggerCallback = |app_state, trigger_id, _chat_me
105107
// we also can't just create an emtpy version of ourselves, and fill the rest in later, because then our users would try to modify non existing commands
106108
#[derive(Default)]
107109
pub struct CommandExecutorService {
108-
triggers: Vec<CommandTrigger>,
110+
triggers: RwLock<Vec<CommandTrigger>>,
109111
cooldown_service: CooldownService,
110112
}
111113

112114
impl CommandExecutorService {
113-
pub(crate) fn remove_command(&self, command_id: &TriggerId) {
114-
//TODO
115-
// self.triggers.retain(|c| c.id.as_ref() != command_id);
115+
pub(crate) async fn remove_command(&self, command_id: &TriggerId) {
116+
self.triggers.write().await.retain(|c| c.id.as_ref() != command_id);
116117
}
117118

118-
pub(crate) fn upsert_command(&self, command: &Command) -> Result<(), regex::Error> {
119-
self.remove_command(command.id.as_ref());
119+
pub(crate) async fn upsert_command(&self, command: &Command) -> Result<(), regex::Error> {
120+
self.remove_command(command.id.as_ref()).await;
120121
let global_cooldown = match command.global_cooldown_type {
121122
CooldownType::SECONDS => ChatCooldown::SECONDS(command.global_cooldown_amount),
122123
CooldownType::MESSAGES => ChatCooldown::MESSAGES(command.global_cooldown_amount)
@@ -125,19 +126,18 @@ impl CommandExecutorService {
125126
CooldownType::SECONDS => ChatCooldown::SECONDS(command.user_cooldown_amount),
126127
CooldownType::MESSAGES => ChatCooldown::MESSAGES(command.user_cooldown_amount)
127128
};
128-
//TODO
129-
// self.triggers.push(CommandTrigger {
130-
// id: command.id.clone().into_boxed_str(),
131-
// global_cooldown,
132-
// user_cooldown,
133-
// permission: command.permission,
134-
// patterns: Self::convert_patterns(command.patterns.as_ref())?,
135-
// callback: TEXT_COMMAND_CALLBACK
136-
// });
129+
self.triggers.write().await.push(CommandTrigger {
130+
id: command.id.clone().into_boxed_str(),
131+
global_cooldown,
132+
user_cooldown,
133+
permission: command.permission,
134+
patterns: Self::convert_patterns(command.patterns.as_ref())?,
135+
callback: TEXT_COMMAND_CALLBACK
136+
});
137137
Ok(())
138138
}
139139

140-
pub(crate) fn refresh_patterns(&self, prod_db: &ProdDB, trigger_id: &TriggerId) {
140+
pub(crate) async fn refresh_patterns(&self, prod_db: &ProdDB, trigger_id: &TriggerId) {
141141
todo!()
142142
}
143143

@@ -156,7 +156,7 @@ impl CommandExecutorService {
156156
}
157157

158158
pub async fn process_chat_message(&self, app_state: Arc<FullState>, message: ChatMessage) {
159-
for trigger in self.triggers.iter() {
159+
for trigger in self.triggers.read().await.iter() {
160160
self.execute_trigger_if_matching(app_state.clone(), trigger, message.clone()).await
161161
}
162162
}

backend/src/commands/command_repo.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ pub(crate) async fn set_enabled(state: AxumState, trigger_id: &TriggerId, enable
232232
return Ok(None)
233233
}
234234
if let Some(l2) = state.l2.get() {
235-
l2.command_executor_service.refresh_patterns(&state.l1.prod_db, trigger_id);
235+
l2.command_executor_service.refresh_patterns(&state.l1.prod_db, trigger_id).await;
236236
}
237237
transaction.commit().await?;
238238
Ok(Some(()))
@@ -263,7 +263,7 @@ pub(crate) async fn save(state: AxumState, command: &Command) -> Result<(), Save
263263

264264
//TODO we should have already parsed the regex here, the call to the command-executor should not fail for any reason from our request
265265
if let Some(l2) = state.l2.get() {
266-
l2.command_executor_service.upsert_command(&command)
266+
l2.command_executor_service.upsert_command(&command).await
267267
.map_err(|e| SaveCommandError::RegexError(e))?;
268268
}
269269
transaction.commit().await.context("failed to commit save command transaction")
@@ -321,7 +321,7 @@ pub(crate) async fn delete_by_id(state: AxumState, trigger_id: &TriggerId) -> an
321321
.context("failed to delete command trigger")?;
322322

323323
if let Some(l2) = state.l2.get() {
324-
l2.command_executor_service.remove_command(&trigger_id);
324+
l2.command_executor_service.remove_command(&trigger_id).await;
325325
}
326326
Ok(())
327327
}

0 commit comments

Comments
 (0)