pub mod schedule;
pub mod parsers;
pub mod errors;
mod thread;
mod uuid;
pub use crate::uuid::uuid::IDMode;
pub mod timer {
use std::sync::{Mutex,Arc};
use std::time;
use crate::schedule::schedule;
use crate::schedule::schedule::{TaskAction, ClosuresAction};
use crate::errors::errors::{TResult, TError, TErrorKind};
use crate::IDMode;
use lazy_static::*;
use crate::thread::threads::{TaskPool};
use crate::uuid::uuid::{set_seed, next_timestamp_id, next_big_id};
use simple_log;
use simple_log::LogConfigBuilder;
use std::future::Future;
#[derive(Clone)]
struct InterData {
config:Arc<Mutex<Config>>,
thread_pool:Arc<Mutex<TaskPool>>,
}
#[derive(Clone)]
pub struct Config {
pub debug:bool,
pub debug_log:String,
pub thread_count:i32,
pub id_seed:i64, pub id_type:IDMode,
}
lazy_static! {
static ref Inter:InterData = InterData{
config:Arc::new(Mutex::new(Config{
debug:false,
debug_log:String::new(),
thread_count:4,
id_seed:1, id_type:IDMode::SequenceId,
})),
thread_pool:Arc::new(Mutex::new(TaskPool::new(time::Duration::from_secs(1),4)))
};
}
fn next_uuid() -> u64 {
match Inter.config.lock().unwrap().id_type {
IDMode::SequenceId => { next_big_id() as u64 }
IDMode::TimestampId => { next_timestamp_id() as u64 }
}
}
pub fn init_schedule(conf:Config) -> TResult<()> {
let mut l_config = Inter.config.lock().unwrap();
l_config.thread_count = conf.thread_count;
l_config.id_seed = conf.id_seed;
l_config.debug = conf.debug;
l_config.id_type = conf.id_type;
let config = LogConfigBuilder::builder()
.path(conf.debug_log)
.size(1 * 100)
.roll_count(10)
.level("debug")
.output_file()
.output_console()
.build();
let r = simple_log::new(config);
match r {
Err(e) => {
return Err(TError::new(TErrorKind::Other(e)));
}
Ok(v) => {}
}
Inter.thread_pool.lock().unwrap().rebuild(conf.thread_count,conf.debug);
set_seed(l_config.id_seed);
Ok(())
}
pub fn spawn_ticker(tick:time::Duration, loopCount:i32, f: impl Fn(u64) + Send+Sync + 'static) -> TResult<u64> {
let task_action = ClosuresAction::new("", next_uuid(), loopCount, tick, f);
let r = Inter.thread_pool.lock();
match r {
Err(e) => { Err(TError::new(TErrorKind::Other(e.to_string()))) },
Ok(mut v) => {
let task_id = task_action.id();
v.spawn(Arc::new(task_action));
Ok(task_id)
}
}
}
pub fn spawn_trait(ft:Arc<dyn TaskAction>) -> TResult<u64> {
let r = Inter.thread_pool.lock();
match r {
Err(e) => { Err(TError::new(TErrorKind::Other(e.to_string()))) },
Ok(mut v) => {
let taskId = ft.id();
v.spawn(ft);
Ok(taskId)
}
}
}
pub fn spawn_date(dateformate:&str, loopCount:i32, f: impl Fn(u64) + Send+Sync + 'static) -> TResult<u64> {
let task_action = ClosuresAction::new(dateformate, next_uuid(), loopCount, time::Duration::from_secs(0), f);
let r = Inter.thread_pool.lock();
match r {
Err(e) => { Err(TError::new(TErrorKind::Other(e.to_string()))) },
Ok(mut v) => {
let task_id = task_action.id();
v.spawn(Arc::new(task_action));
Ok(task_id)
}
}
}
pub fn block_on_rt<F>(future: F) -> TResult<()>
where
F: Future,
F::Output: Send + 'static,
{
let r =Inter.thread_pool.lock();
match r {
Err(e) => { Err(TError::new(TErrorKind::Other(e.to_string()))) },
Ok(mut v) => {
v.block_on(future);
Ok(())
}
}
}
pub fn spawn_rt<F>(future: F) -> TResult<()>
where
F: Future + Send + 'static,
F::Output: Send + 'static,
{
let r =Inter.thread_pool.lock();
match r {
Err(e) => { Err(TError::new(TErrorKind::Other(e.to_string()))) },
Ok(mut v) => {
v.spawn_rt(future);
Ok(())
}
}
}
pub fn stop_ticker(id:u64) -> TResult<()> {
let r = Inter.thread_pool.lock();
match r {
Err(e) => { Err(TError::new(TErrorKind::Other(e.to_string()))) },
Ok(mut v) => {
let r = v.stop_task(id)?;
Ok(r)
}
}
}
pub fn wait_forever() {
loop {
std::thread::sleep(time::Duration::from_secs(1));
}
}
}