funscheduler

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 00c79dcc4c65928c29bd10126cddf375616ff4b9
Author: Jackson G. Kaindume <seestem@protonmail.com>
Date:   Wed, 14 Jul 2021 02:43:42 +0200

Initial commit

Diffstat:
A.gitignore | 3+++
ACargo.lock | 7+++++++
ACargo.toml | 9+++++++++
ALICENSE | 5+++++
AREADME | 29+++++++++++++++++++++++++++++
Asrc/lib.rs | 56++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/main.rs | 13+++++++++++++
7 files changed, 122 insertions(+), 0 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1,2 @@ +*~ +target +\ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "funscheduler" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "funscheduler" +authors = ["Jackson G. Kaindume <seestem@protonmail.com>"] +version = "0.1.0" +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/LICENSE b/LICENSE @@ -0,0 +1,4 @@ +.---------------------------------------------------------------. +| Written and placed in the public domain by | +| Jackson G. Kaindume<seestem@protonmail.com> ⧉ | +'----------------------------------------------------------[2021]+ +\ No newline at end of file diff --git a/README b/README @@ -0,0 +1,28 @@ + ┌─┐┬ ┬┌┐┌┌─┐┌─┐┬ ┬┌─┐┌┬┐┬ ┬┬ ┌─┐┬─┐ + ├┤ │ ││││└─┐│ ├─┤├┤ │││ ││ ├┤ ├┬┘ + └ └─┘┘└┘└─┘└─┘┴ ┴└─┘─┴┘└─┘┴─┘└─┘┴└─ + +---------------------------------------------------------------------- + Simple time based function execution scheduler +---------------------------------------------------------------------- + +use funscheduler::{FunScheduler, Timing}; + +fn main() { + // Execute job every second: + FunScheduler::interval(job, Timing::Seconds(1)); + + // The rest of the API: + // FunScheduler::interval(job, Timing::Minutes(1)); + // FunScheduler::interval(job, Timing::Hours(1)); + // FunScheduler::interval(job, Timing::Days(1)); + + // Execute function once after a specified amount of time: + // FunScheduler::after(job, Timing::Seconds(1)); +} + +fn job() { + println!("Hello, world"); +} + +---------------------------------------------------------------------- +\ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs @@ -0,0 +1,56 @@ +// .---------------------------------------------------------------. +// | Written and placed in the public domain by | +// | Jackson G. Kaindume<seestem@protonmail.com> ⧉ | +// '----------------------------------------------------------[2021]+ +//! # Time based function execution scheduler + +use std::time::Duration; + +pub enum Timing { + Seconds(u64), + Minutes(u64), + Hours(u64), + Days(u64), +} + +pub struct FunScheduler; + +impl FunScheduler { + /// Execute a function in specified time intervals, starting now. + pub fn interval(job: fn(), timing: Timing) { + let time_control = calc_time(timing); + + loop { + job(); + std::thread::sleep(time_control); + } + } + + /// Execute function once after a specified amount of time + pub fn after(job: fn(), timing: Timing) { + let time_control = calc_time(timing); + std::thread::sleep(time_control); + job(); + } +} + +pub fn calc_time(timing: Timing) -> Duration { + let final_seconds; + match timing { + Timing::Seconds(seconds) => final_seconds = Duration::from_secs(seconds), + Timing::Minutes(minutes) => { + let seconds = minutes * 60; + final_seconds = Duration::from_secs(seconds); + } + Timing::Hours(hours) => { + let seconds = hours * 3600; + final_seconds = Duration::from_secs(seconds); + } + Timing::Days(days) => { + let seconds = days * 86_400; + final_seconds = Duration::from_secs(seconds); + } + } + + final_seconds +} diff --git a/src/main.rs b/src/main.rs @@ -0,0 +1,13 @@ +use funscheduler::{FunScheduler, Timing}; + +fn main() { + // Execute job every second + //FunScheduler::interval(job, Timing::Seconds(1)); + + // Execute job once after 5 seconds + FunScheduler::after(job, Timing::Seconds(5)); +} + +fn job() { + println!("Hello, world"); +}