Skip to content

Commit

Permalink
Removed lazy static crate, refactored some code in user rate send (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
arkanoider authored Sep 5, 2023
1 parent 6731680 commit 460c082
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 32 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mostro"
version = "0.7.9"
version = "0.8.0"
edition = "2021"
license = "MIT"
authors = ["Francisco Calderón <[email protected]>"]
Expand Down Expand Up @@ -38,5 +38,4 @@ mostro-core = "0.2.7"
tokio-cron-scheduler = "*"
tracing = "0.1.37"
tracing-subscriber = "0.3.16"
lazy_static = "1.4.0"
config = "0.13.3"
20 changes: 18 additions & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@ use crate::app::cancel::cancel_action;
use crate::app::dispute::dispute_action;
use crate::app::fiat_sent::fiat_sent_action;
use crate::app::order::order_action;
use crate::app::rate_user::update_user_reputation_action;
use crate::app::rate_user::{send_user_rates, update_user_reputation_action};
use crate::app::release::release_action;
use crate::app::take_buy::take_buy_action;
use crate::app::take_sell::take_sell_action;
use crate::lightning::LndConnector;
use crate::CLEAR_USER_VEC;
use anyhow::Result;
use mostro_core::{Action, Message};
use nostr_sdk::prelude::*;
use sqlx::{Pool, Sqlite};
use std::sync::atomic::Ordering;

pub async fn run(
my_keys: Keys,
Expand All @@ -36,6 +38,15 @@ pub async fn run(
loop {
let mut notifications = client.notifications();

let mut rate_list: Vec<Event> = vec![];

// Check if we can send user rates updates
if CLEAR_USER_VEC.load(Ordering::Relaxed) {
send_user_rates(&rate_list, &client).await?;
CLEAR_USER_VEC.store(false, Ordering::Relaxed);
rate_list.clear();
}

while let Ok(notification) = notifications.recv().await {
if let RelayPoolNotification::Event(_, event) = notification {
if let Kind::EncryptedDirectMessage = event.kind {
Expand Down Expand Up @@ -85,7 +96,12 @@ pub async fn run(
Action::PayInvoice => todo!(),
Action::RateUser => {
update_user_reputation_action(
msg, &event, &my_keys, &client, &pool,
msg,
&event,
&my_keys,
&client,
&pool,
&mut rate_list,
)
.await?;
}
Expand Down
17 changes: 17 additions & 0 deletions src/app/rate_user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ pub async fn update_user_reputation_action(
my_keys: &Keys,
client: &Client,
pool: &Pool<Sqlite>,
rate_list: &mut Vec<Event>,
) -> Result<()> {
let order_id = msg.order_id.unwrap();
let order = match Order::by_id(pool, order_id).await? {
Expand Down Expand Up @@ -245,6 +246,7 @@ pub async fn update_user_reputation_action(
order.id,
my_keys,
pool,
rate_list,
)
.await?;

Expand All @@ -256,3 +258,18 @@ pub async fn update_user_reputation_action(

Ok(())
}

pub async fn send_user_rates(rate_list: &[Event], client: &Client) -> Result<()> {
for ev in rate_list.iter() {
// Send event to relay
match client.send_event(ev.clone()).await {
Ok(id) => {
info!("Updated rate event with id {:?}", id)
}
Err(e) => {
info!("Error on updating rate event {:?}", e.to_string())
}
}
}
Ok(())
}
10 changes: 2 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,12 @@ use nostr_sdk::prelude::*;
use scheduler::start_scheduler;
use settings::Settings;
use settings::{init_default_dir, init_global_settings};
use std::sync::atomic::AtomicBool;
use std::{env::args, path::PathBuf, sync::OnceLock};
use tokio::sync::Mutex;

static CLEAR_USER_VEC: AtomicBool = AtomicBool::new(false);
static MOSTRO_CONFIG: OnceLock<Settings> = OnceLock::new();

#[macro_use]
extern crate lazy_static;

lazy_static! {
static ref RATE_EVENT_LIST: Mutex<Vec<Event>> = Mutex::new(vec![]);
}

#[tokio::main]
async fn main() -> Result<()> {
pretty_env_logger::init();
Expand Down
23 changes: 5 additions & 18 deletions src/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use crate::db::*;
use crate::lightning::LndConnector;
use crate::settings::Settings;
use crate::util::update_order_event;
use crate::RATE_EVENT_LIST;
use crate::CLEAR_USER_VEC;
use std::sync::atomic::Ordering;

use anyhow::Result;
use mostro_core::Status;
Expand Down Expand Up @@ -165,24 +166,10 @@ pub async fn cron_scheduler(sched: &JobScheduler) -> Result<(), anyhow::Error> {

let job_update_rate_events = Job::new_async("0 0 * * * *", move |uuid, mut l| {
Box::pin(async move {
// Connect to relays
let client = crate::util::connect_nostr().await.unwrap();
info!("I run async every hour - update rate event of users");

info!("I run async every hour - update rate event of users",);

for ev in RATE_EVENT_LIST.lock().await.iter() {
// Send event to relay
match client.send_event(ev.clone()).await {
Ok(id) => {
info!("Updated rate event with id {:?}", id)
}
Err(e) => {
info!("Error on updating rate event {:?}", e.to_string())
}
}
}
// Clear list
RATE_EVENT_LIST.lock().await.clear();
// Clear list after sending
CLEAR_USER_VEC.store(true, Ordering::Relaxed);

let next_tick = l.next_tick_for_job(uuid).await;
match next_tick {
Expand Down
5 changes: 3 additions & 2 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::lightning::LndConnector;
use crate::messages;
use crate::models::Yadio;
use crate::settings::Settings;
use crate::{db, flow, RATE_EVENT_LIST};
use crate::{db, flow};

use anyhow::{Context, Result};
use log::{error, info};
Expand Down Expand Up @@ -188,6 +188,7 @@ pub async fn update_user_rating_event(
order_id: Uuid,
keys: &Keys,
pool: &SqlitePool,
rate_list: &mut Vec<Event>,
) -> Result<()> {
// let reputation = reput
// nip33 kind and d tag
Expand All @@ -204,7 +205,7 @@ pub async fn update_user_rating_event(
}

// Add event message to global list
RATE_EVENT_LIST.lock().await.push(event);
rate_list.push(event);

Ok(())
}
Expand Down

0 comments on commit 460c082

Please sign in to comment.