mirror of
http://shenjack.top:5100/shenjack/icalingua-python-bot.git
synced 2025-04-20 05:39:54 +08:00
写了个 schduler
This commit is contained in:
parent
1f92a62e45
commit
56469f6fbb
|
@ -8,14 +8,14 @@ use ed25519_dalek::{Signature, Signer, SigningKey};
|
||||||
use rust_socketio::Payload;
|
use rust_socketio::Payload;
|
||||||
use rust_socketio::asynchronous::Client;
|
use rust_socketio::asynchronous::Client;
|
||||||
use serde_json::{Value, json};
|
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 {
|
pub async fn send_message(client: &Client, message: &SendMessage) -> bool {
|
||||||
let value = message.as_value();
|
let value = message.as_value();
|
||||||
match client.emit("sendMessage", value).await {
|
match client.emit("sendMessage", value).await {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
debug!("send_message {}", format!("{:#?}", message).cyan());
|
event!(Level::INFO, "send_message {}", format!("{:#?}", message).cyan());
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
|
@ -30,11 +30,11 @@ pub async fn delete_message(client: &Client, message: &DeleteMessage) -> bool {
|
||||||
let value = message.as_value();
|
let value = message.as_value();
|
||||||
match client.emit("deleteMessage", value).await {
|
match client.emit("deleteMessage", value).await {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
debug!("delete_message {}", format!("{:#?}", message).yellow());
|
event!(Level::DEBUG, "delete_message {}", format!("{:#?}", message).yellow());
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
warn!("delete_message faild:{}", format!("{:#?}", e).red());
|
event!(Level::WARN, "delete_message faild:{}", format!("{:#?}", e).red());
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,6 +56,7 @@ impl ConfigDataPy {
|
||||||
fn rs_api_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
|
fn rs_api_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
|
||||||
m.add_class::<ConfigDataPy>()?;
|
m.add_class::<ConfigDataPy>()?;
|
||||||
m.add_class::<config::ConfigStoragePy>()?;
|
m.add_class::<config::ConfigStoragePy>()?;
|
||||||
|
m.add_class::<schdule::SchedulerPy>()?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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};
|
use tracing::{Level, event};
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
|
@ -12,6 +14,10 @@ pub struct SchedulerPy {
|
||||||
///
|
///
|
||||||
/// 你最好不要把他清理掉
|
/// 你最好不要把他清理掉
|
||||||
pub callback: Py<PyFunction>,
|
pub callback: Py<PyFunction>,
|
||||||
|
/// 预计等待时间
|
||||||
|
pub schdule_time: Duration,
|
||||||
|
// /// 是否正在运行
|
||||||
|
// pub running: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[pymethods]
|
#[pymethods]
|
||||||
|
@ -20,4 +26,35 @@ impl SchedulerPy {
|
||||||
visit.call(&self.callback)?;
|
visit.call(&self.callback)?;
|
||||||
Ok(())
|
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);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user