forked from stumpapp/stump
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🎨 Session cleanup as queueable job (stumpapp#201)
In an effort to reduce the liklihood of multiple DB writers at a given time, I made the session cleanup task a job. This way, it has to be queued before it can do its thing
- Loading branch information
1 parent
4b9e667
commit 2b2a7ad
Showing
6 changed files
with
95 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use prisma_client_rust::chrono::Utc; | ||
use stump_core::{ | ||
job::{Job, JobError, JobTrait, WorkerCtx}, | ||
prisma::session, | ||
}; | ||
|
||
pub const SESSION_CLEANUP_JOB_NAME: &str = "session_cleanup"; | ||
|
||
pub struct SessionCleanupJob; | ||
|
||
impl SessionCleanupJob { | ||
pub fn new() -> Box<Job<SessionCleanupJob>> { | ||
Job::new(Self) | ||
} | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl JobTrait for SessionCleanupJob { | ||
fn name(&self) -> &'static str { | ||
SESSION_CLEANUP_JOB_NAME | ||
} | ||
|
||
fn description(&self) -> Option<Box<&str>> { | ||
None | ||
} | ||
|
||
async fn run(&mut self, ctx: WorkerCtx) -> Result<u64, JobError> { | ||
tracing::trace!("Deleting expired sessions"); | ||
|
||
let client = ctx.core_ctx.db.clone(); | ||
let affected_rows = client | ||
.session() | ||
.delete_many(vec![session::expires_at::lt(Utc::now().into())]) | ||
.exec() | ||
.await?; | ||
|
||
tracing::trace!(affected_rows = ?affected_rows, "Deleted expired sessions"); | ||
|
||
Ok(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters