Skip to content
This repository has been archived by the owner on Aug 16, 2023. It is now read-only.

Commit

Permalink
Make agent offline instead of delete him
Browse files Browse the repository at this point in the history
  • Loading branch information
andrey-skat committed Apr 28, 2022
1 parent 4e133a6 commit a3b78c3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
1 change: 1 addition & 0 deletions migrations/20220427161630_add_offline_agent_status.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TYPE agent_status ADD VALUE 'offline';
33 changes: 16 additions & 17 deletions src/app/endpoint/subscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,26 +76,22 @@ impl EventHandler for DeleteEventHandler {
// Delete agent from the DB.
let room_id = try_room_id(&payload.object)?;
Span::current().record("room_id", &display(room_id));
let mut conn = context.get_conn().await?;

let row_count = {
let query = agent::DeleteQuery::new(payload.subject.clone(), room_id);
let mut conn = context.get_conn().await?;

let row_count = context
.metrics()
.measure_query(QueryKey::AgentDeleteQuery, query.execute(&mut conn))
.await
.context("Failed to delete agent")
.error(AppErrorKind::DbQueryFailed)?;

row_count
};
let query =
agent::UpdateQuery::new(payload.subject.clone(), room_id).status(agent::Status::Offline);
let object = context
.metrics()
.measure_query(QueryKey::AgentUpdateQuery, query.execute(&mut conn))
.await
.context("Failed to put agent into 'offline' status")
.error(AppErrorKind::DbQueryFailed)?;

// Ignore missing agent.
if row_count != 1 {
if object.is_none() {
return Ok(Box::new(stream::empty()));
}
let mut conn = context.get_conn().await?;

let room = room::FindQuery::new(room_id)
.execute(&mut conn)
.await
Expand Down Expand Up @@ -183,7 +179,7 @@ fn try_room_id(object: &[String]) -> StdResult<Uuid, AppError> {
#[cfg(test)]
mod tests {
mod delete_event {
use crate::db::agent::ListQuery as AgentListQuery;
use crate::db::agent::{ListQuery as AgentListQuery, Status};
use crate::test_helpers::prelude::*;

use super::super::*;
Expand Down Expand Up @@ -237,7 +233,10 @@ mod tests {
.await
.expect("Failed to execute agent list query");

assert_eq!(db_agents.len(), 0);
match db_agents.first() {
None => assert!(false),
Some(agent) => assert_eq!(agent.status(), Status::Offline)
};
}

#[tokio::test]
Expand Down
8 changes: 8 additions & 0 deletions src/db/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ pub(crate) enum Status {
InProgress,
#[sqlx(rename = "ready")]
Ready,
#[sqlx(rename = "offline")]
Offline,
}

#[derive(Debug, Serialize, sqlx::FromRow)]
Expand All @@ -31,6 +33,12 @@ pub(crate) struct Object {
created_at: DateTime<Utc>,
}

impl Object {
pub fn status(&self) -> Status {
self.status
}
}

#[derive(Debug, Serialize, Deserialize, sqlx::FromRow)]
pub(crate) struct AgentWithBan {
#[serde(skip_serializing)]
Expand Down

0 comments on commit a3b78c3

Please sign in to comment.