-
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.
chore(main): reorganize background jobs
- Loading branch information
1 parent
108c011
commit 339d6f4
Showing
6 changed files
with
93 additions
and
92 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 |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod clear_sessions; | ||
pub mod convert_goals; |
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,26 @@ | ||
use crate::{models::user::User, mongo_client}; | ||
use bson::doc; | ||
use chrono::Utc; | ||
use tracing::{error, info}; | ||
|
||
pub async fn clear_sessions() { | ||
info!("clearing old sessions at {}", Utc::now()); | ||
let mongo = mongo_client().await.unwrap(); | ||
|
||
let update_result = mongo | ||
.default_database() | ||
.unwrap() | ||
.collection::<User>("users") | ||
.update_many( | ||
doc! {}, | ||
doc! {"$pull": doc! {"sessions": doc! {"expiration": doc! { "$lte": Utc::now()}}}}, | ||
) | ||
.await; | ||
|
||
match update_result { | ||
Ok(_) => {} | ||
Err(err) => { | ||
error!("{}", err); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod dates; | ||
pub mod tera; |
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,46 @@ | ||
use std::{collections::HashMap, time::SystemTime}; | ||
use tera::{Filter, Value}; | ||
use tracing::debug; | ||
|
||
pub fn extract_id() -> impl Filter { | ||
return |value: &Value, args: &HashMap<String, tera::Value>| -> tera::Result<tera::Value> { | ||
debug!("{}", value); | ||
debug!("{:#?}", args); | ||
|
||
let id = value.get("_id"); | ||
|
||
match id { | ||
None => Err(tera::Error::msg("could not find id field".to_string())), | ||
Some(id) => Ok(tera::Value::String( | ||
id["$oid"].to_string().replace("\"", ""), | ||
)), | ||
} | ||
}; | ||
} | ||
|
||
pub fn digest_asset() -> impl tera::Function { | ||
let key = SystemTime::now(); | ||
let key = key | ||
.duration_since(SystemTime::UNIX_EPOCH) | ||
.expect("could not generate asset timestamp"); | ||
let key = key.as_secs().to_string(); | ||
|
||
return move |args: &HashMap<String, tera::Value>| -> tera::Result<tera::Value> { | ||
match args.get("file") { | ||
Some(file) => { | ||
let mut path = "/assets/".to_string(); | ||
|
||
let Some(file) = file.as_str() else { | ||
return Err("".to_string().into()); | ||
}; | ||
|
||
path.push_str(file); | ||
path.push_str("?v="); | ||
path.push_str(&key); | ||
|
||
Ok(path.into()) | ||
} | ||
None => Err("".to_string().into()), | ||
} | ||
}; | ||
} |