1 unstable release

0.1.0 Oct 24, 2019

#47 in #task-runner

MIT license

16KB
318 lines

heng_rs

A schedule task runner on tokio-runtime with one way message pushing.

Requirement:

rustc 1.39.0-beta.6 (224f0bc90 2019-10-15)


lib.rs:

A simple schedule task runner on tokio runtime that support one way message pushing.

example:

use std::time::Duration;
use heng_rs::{Scheduler, Time, ToDuration};

struct TestTask(u32);

impl Scheduler for TestTask {
    type Message = u32;
}

#[tokio::main]
async fn main() -> std::io::Result<()> {
    let task = TestTask(0);
    let time = Time::new()
        .every(1.d())
        .plus(2.h())
        .plus(3.m())
        .plus(4.s());
    // run task with a 1 day, 2 hours, 3 minutes and 4 seconds interval.
    let addr = task.start(time, |task, ctx| {
        if let Some(msg) = ctx.get_msg_front() {
            /* do something with message */
        }

        // do something with task.
        task.0 += 1;

        // run a future.
        async {
            Ok::<(),()>(())
        }
    });

    // use address to push message to task's context;
    addr.send(1u32).await;
    Ok(())
}

Dependencies

~1.5MB
~27K SLoC