pulsync

Crates.iopulsync
lib.rspulsync
version1.0.1
created_at2025-05-27 11:49:22.682205+00
updated_at2025-05-27 12:07:54.99541+00
descriptionSchedule Async and Sync Task with ease.
homepagehttps://github.com/ZialeHub/pulsync
repositoryhttps://github.com/ZialeHub/pulsync
max_upload_size
id1690962
size84,632
(ZialeHub)

documentation

README

Pulsync

A Rust crate for managing synchronous and asynchronous task scheduling with powerful recurrence options.

Features

  • Create and manage a Scheduler to handle tasks.
  • Support for both synchronous and asynchronous tasks.
  • Flexible Recurrence API to control:
    • Timeout between executions.
    • Number of executions.
    • Execution time limits.
  • Feature flags:
    • sync: Enables synchronous task scheduling.
    • async: Enables asynchronous task scheduling.
    • Note: You must enable exactly one of the features above; they cannot be used together.
    • serde: Enables de/serialize TaskState<T> and add a restart method to scheduler.

Installation

Add this crate to your Cargo.toml:

[dependencies]
pulsync = { version = "0.1", features = ["async"] }
# or
pulsync = { version = "0.1", default-features = false, features = ["sync"] }

Usage

Synchronous Example

use chrono::NaiveDateTime;
use pulsync::prelude::*;

#[derive(Debug, Clone, Task, Salt)]
#[title("MySyncTask for {self.login}")]
#[salt("{self.state}")]
struct MySyncTask {
    login: TaskState<String>,
    state: TaskState<u8>,
}
impl SyncTaskHandler for MySyncTask {
    fn run(&self) {
        eprintln!(
            "Number of messages sent to {} = {}",
            self.login.read().unwrap(),
            self.state.read().unwrap()
        );
        *self.state.write().unwrap() += 1;
    }
}

fn main() {
    let mut scheduler = Scheduler::build();
    let task = MySyncTask::new(String::from("Pierre"), 0);

    // The task will run every 3 days until 2026-01-01 00:00:00
    let _id = scheduler
        .schedule(
            Box::new(task),
            every(3.days()).until_datetime(
                NaiveDateTime::parse_from_str("2026-01-01 00:00:00", "%Y-%m-%d %H:%M:%S").unwrap(),
            ),
        )
        .unwrap();

    // titles = ["[SyncTask({id})] MySyncTask for Pierre started at {task start datetime}"]
    let _titles = scheduler.get();
}

Asynchronous Example

#![cfg(all(feature = "async", not(feature = "sync")))]
use std::{future::Future, pin::Pin};

use pulsync::prelude::*;

#[derive(Debug, Clone, Task, Salt)]
#[title("MyAsyncTask for {self.login}")]
#[salt("{self.state}")]
struct MyAsyncTask {
    login: TaskState<String>,
    state: TaskState<u8>,
}
impl AsyncTaskHandler for MyAsyncTask {
    fn run(&self) -> Pin<Box<dyn Future<Output = ()> + Send + '_ + Sync>> {
        Box::pin(async move {
            eprintln!(
                "Number of messages sent to {} = {}",
                self.login.read().unwrap(),
                self.state.read().unwrap()
            );
            *self.state.write().unwrap() += 1;
        })
    }
}

#[tokio::main]
async fn main() {
    let mut scheduler = Scheduler::build();
    let task = MyAsyncTask::new(String::from("Pierre"), 0);

    // The task will run every 5 minutes and 30 seconds
    // After 5 times the task will be removed from the scheduler.
    let id = scheduler
        .schedule(
            Box::new(task),
            every(5.minutes()).and(30.seconds()).count(5),
        )
        .unwrap();
    let _ = scheduler.pause(id);
    let _ = scheduler.resume(id);
    let _ = scheduler.abort(id);
}

License

This project is licensed under the MIT License. See LICENSE for details.

Contributing

Contributions are welcome! Feel free to open issues or submit pull requests.

Links


Let me know if you want more details, usage examples, or specific documentation!

Commit count: 2

cargo fmt