From 56469f6fbb7a84bb5b116620b564cf8e9f52c7a7 Mon Sep 17 00:00:00 2001 From: shenjack-5600u <3695888@qq.com> Date: Fri, 11 Apr 2025 00:36:58 +0800 Subject: [PATCH] =?UTF-8?q?=E5=86=99=E4=BA=86=E4=B8=AA=20schduler?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ica-rs/src/ica/client.rs | 8 +++---- ica-rs/src/py/class.rs | 1 + ica-rs/src/py/class/schdule.rs | 39 +++++++++++++++++++++++++++++++++- 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/ica-rs/src/ica/client.rs b/ica-rs/src/ica/client.rs index 3a8774a..269bbe3 100644 --- a/ica-rs/src/ica/client.rs +++ b/ica-rs/src/ica/client.rs @@ -8,14 +8,14 @@ use ed25519_dalek::{Signature, Signer, SigningKey}; use rust_socketio::Payload; use rust_socketio::asynchronous::Client; use serde_json::{Value, json}; -use tracing::{Level, debug, event, span, warn}; +use tracing::{Level, event, span, warn}; /// "安全" 的 发送一条消息 pub async fn send_message(client: &Client, message: &SendMessage) -> bool { let value = message.as_value(); match client.emit("sendMessage", value).await { Ok(_) => { - debug!("send_message {}", format!("{:#?}", message).cyan()); + event!(Level::INFO, "send_message {}", format!("{:#?}", message).cyan()); true } Err(e) => { @@ -30,11 +30,11 @@ pub async fn delete_message(client: &Client, message: &DeleteMessage) -> bool { let value = message.as_value(); match client.emit("deleteMessage", value).await { Ok(_) => { - debug!("delete_message {}", format!("{:#?}", message).yellow()); + event!(Level::DEBUG, "delete_message {}", format!("{:#?}", message).yellow()); true } Err(e) => { - warn!("delete_message faild:{}", format!("{:#?}", e).red()); + event!(Level::WARN, "delete_message faild:{}", format!("{:#?}", e).red()); false } } diff --git a/ica-rs/src/py/class.rs b/ica-rs/src/py/class.rs index 81cf579..0345f5f 100644 --- a/ica-rs/src/py/class.rs +++ b/ica-rs/src/py/class.rs @@ -56,6 +56,7 @@ impl ConfigDataPy { fn rs_api_module(m: &Bound<'_, PyModule>) -> PyResult<()> { m.add_class::()?; m.add_class::()?; + m.add_class::()?; Ok(()) } diff --git a/ica-rs/src/py/class/schdule.rs b/ica-rs/src/py/class/schdule.rs index 3d8632a..379512b 100644 --- a/ica-rs/src/py/class/schdule.rs +++ b/ica-rs/src/py/class/schdule.rs @@ -1,4 +1,6 @@ -use pyo3::{Bound, Py, PyAny, PyTraverseError, PyVisit, pyclass, pymethods, types::PyFunction}; +use std::time::Duration; + +use pyo3::{Bound, Py, PyTraverseError, PyVisit, Python, pyclass, pymethods, types::PyFunction}; use tracing::{Level, event}; #[derive(Clone, Debug)] @@ -12,6 +14,10 @@ pub struct SchedulerPy { /// /// 你最好不要把他清理掉 pub callback: Py, + /// 预计等待时间 + pub schdule_time: Duration, + // /// 是否正在运行 + // pub running: bool, } #[pymethods] @@ -20,4 +26,35 @@ impl SchedulerPy { visit.call(&self.callback)?; Ok(()) } + + #[new] + pub fn new(func: Bound<'_, PyFunction>, schdule_time: Duration) -> Self { + Self { + callback: func.unbind(), + schdule_time, + // running: false, + } + } + + /// 开始 + pub fn start(&self, py: Python<'_>) { + let wait = self.schdule_time; + let cb = self.callback.clone_ref(py); + tokio::spawn(async move { + let second = Duration::from_secs(1); + if wait > second { + let big_sleep = wait.checked_sub(second).unwrap(); + tokio::time::sleep(big_sleep).await; + tokio::time::sleep(second).await; + } else { + tokio::time::sleep(wait).await; + } + Python::with_gil(|py| { + event!(Level::INFO, "正在调用计划 {:?}", wait); + if let Err(e) = cb.call0(py) { + event!(Level::WARN, "调用时出现错误 {}", e); + } + }); + }); + } }