BinChengZhao / delay-timer

Time-manager of delayed tasks. Like crontab, but synchronous asynchronous tasks are possible scheduling, and dynamic add/cancel/remove is supported.
Apache License 2.0
309 stars 23 forks source link

"set_frequency_once" always execute immediately. #38

Closed gamife closed 2 years ago

gamife commented 2 years ago

Describe the bug set_frequency_once_by_seconds(5) The function is executed immediately instead of being delayed for some time

To Reproduce

use std::time::Duration;

use delay_timer::prelude::{DelayTimerBuilder, ScheduleIteratorTimeZone, TaskBuilder};

#[tokio::main]
async fn main() {
    let body = || async {
        println!("running: {}", chrono::Local::now());
    };

    println!("start: {}", chrono::Local::now());
    let new_task = TaskBuilder::default()
        .set_task_id(1)
        .set_schedule_iterator_time_zone(ScheduleIteratorTimeZone::Local)
        .set_frequency_once_by_seconds(5)
        .set_maximum_parallel_runnable_num(1)
        .spawn_async_routine(body)
        .unwrap();

    let delay_timer = DelayTimerBuilder::default().build();
    delay_timer.add_task(new_task).unwrap();

    tokio::time::sleep(Duration::from_secs(15)).await;
    println!("end: {}", chrono::Local::now());
}
delay_timer = { version = "0.11.0", features = ["status-report"] }
chrono = "0.4.19"
tokio = { version = "1", features = ["full"] }

Result

start: 2022-04-22 10:56:01.732017356 +08:00
running: 2022-04-22 10:56:02.793417770 +08:00
end: 2022-04-22 10:56:16.796830865 +08:00

Expected behavior Expect a five-second delay.

Desktop (please complete the following information):

BinChengZhao commented 2 years ago

Expected behavior Expect a five-second delay.

Desktop (please complete the following information):

  • OS: Linux version 3.10.0-957.el7.x86_64 (gcc version 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) )
  • Rust: rustc 1.59.0 (9d1b2106e 2022-02-23)

Friend your scene I have reproduced,

The specific behavior is as follows:

First-time immediate execution, From the second time onwards, the execution is delayed at regular intervals.

I will find time to adjust the program behavior in the near future, thanks for your feedback!

BinChengZhao commented 2 years ago

@gamife Fix at V0.11.2, I look forward to your feedback on successful use.

:)

Xiaobaishushu25 commented 7 months ago

可以实现这种这种效果吗?public void schedule(TimerTask task, long delay, long period):创建完成后延迟delay执行一次,随后每period执行一次. 另外好像要么执行一次(set_frequency_once),要么指定执行次数,有无限执行的选择不?