{"id":13587062,"url":"https://github.com/BinChengZhao/delay-timer","last_synced_at":"2025-04-07T19:30:37.015Z","repository":{"id":40424417,"uuid":"268475598","full_name":"BinChengZhao/delay-timer","owner":"BinChengZhao","description":"Time-manager of delayed tasks. Like crontab, but synchronous asynchronous tasks are possible scheduling, and dynamic add/cancel/remove is supported.","archived":false,"fork":false,"pushed_at":"2024-05-20T04:46:18.000Z","size":623,"stargazers_count":323,"open_issues_count":7,"forks_count":25,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-04-01T17:49:46.769Z","etag":null,"topics":["cron","crontab","delay-timer","delayed-tasks","schedule","scheduling","scheduling-engines","smol","tokio"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/BinChengZhao.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE-APACHE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-06-01T09:13:05.000Z","updated_at":"2025-03-31T03:32:20.000Z","dependencies_parsed_at":"2024-11-06T05:33:46.110Z","dependency_job_id":"f2c5bf79-08d0-4d71-8887-9341ae8f31a2","html_url":"https://github.com/BinChengZhao/delay-timer","commit_stats":{"total_commits":271,"total_committers":7,"mean_commits":"38.714285714285715","dds":"0.029520295202952074","last_synced_commit":"69a3867c0981428810bace164159ff4619bcf755"},"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BinChengZhao%2Fdelay-timer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BinChengZhao%2Fdelay-timer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BinChengZhao%2Fdelay-timer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BinChengZhao%2Fdelay-timer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/BinChengZhao","download_url":"https://codeload.github.com/BinChengZhao/delay-timer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247716060,"owners_count":20984164,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cron","crontab","delay-timer","delayed-tasks","schedule","scheduling","scheduling-engines","smol","tokio"],"created_at":"2024-08-01T15:05:59.818Z","updated_at":"2025-04-07T19:30:36.617Z","avatar_url":"https://github.com/BinChengZhao.png","language":"Rust","funding_links":[],"categories":["Rust","Libraries","库 Libraries"],"sub_categories":["Task scheduling","任务调度 Task scheduling"],"readme":"# delay-timer  \n[![Build](https://github.com/BinChengZhao/delay-timer/workflows/Build%20and%20test/badge.svg)](\nhttps://github.com/BinChengZhao/delay-timer/actions)\n[![License](https://img.shields.io/badge/license-MIT%2FApache--2.0-blue.svg)](\nhttps://github.com/BinChengZhao/delay-timer)\n[![Cargo](https://img.shields.io/crates/v/delay_timer.svg)](\nhttps://crates.io/crates/delay_timer)\n[![Documentation](https://docs.rs/delay_timer/badge.svg)](\nhttps://docs.rs/delay_timer)\n\nTime-manager of delayed tasks. Like crontab, but synchronous asynchronous tasks are possible, and dynamic add/cancel/remove is supported.\n\ndelay-timer is a task manager based on a time wheel algorithm, which makes it easy to manage timed tasks, or to periodically execute arbitrary tasks such as closures.\n\nThe underlying runtime is based on the optional smol and tokio, and you can build your application with either one.\n\nThe minimum-supported version of `rustc` is **1.56**.\n\nExcept for the simple execution in a few seconds, you can also specify a specific date, \nsuch as Sunday at 4am to execute a backup task.\n\n##### Supports configuration of the maximum number of parallelism of tasks.\n##### Dynamically cancel a running task instance by means of a handle.\n\n![image](https://github.com/BinChengZhao/delay-timer/blob/master/structural_drawing/DelayTImer.png)\n\n### If you're looking for a distributed task scheduling platform, check out the [delicate](https://github.com/BinChengZhao/delicate)\n\n\n## Examples\n\n\n ```rust\n\nuse anyhow::Result;\nuse delay_timer::prelude::*;\n\nfn main() -\u003e Result\u003c()\u003e {\n    // Build an DelayTimer that uses the default configuration of the Smol runtime internally.\n    let delay_timer = DelayTimerBuilder::default().build();\n\n    // Develop a print job that runs in an asynchronous cycle.\n    // A chain of task instances.\n    let task_instance_chain = delay_timer.insert_task(build_task_async_print()?)?;\n\n    // Get the running instance of task 1.\n    let task_instance = task_instance_chain.next_with_wait()?;\n\n    // Cancel running task instances.\n    task_instance.cancel_with_wait()?;\n\n    // Remove task which id is 1.\n    delay_timer.remove_task(1)?;\n\n    // No new tasks are accepted; running tasks are not affected.\n    delay_timer.stop_delay_timer()?;\n\n    Ok(())\n}\n\nfn build_task_async_print() -\u003e Result\u003cTask, TaskError\u003e {\n    let mut task_builder = TaskBuilder::default();\n\n    let body = || async {\n        println!(\"create_async_fn_body!\");\n\n        Timer::after(Duration::from_secs(3)).await;\n\n        println!(\"create_async_fn_body:i'success\");\n    };\n\n    task_builder\n        .set_task_id(1)\n        .set_frequency_repeated_by_cron_str(\"@secondly\")\n        .set_maximum_parallel_runnable_num(2)\n        .spawn_async_routine(body)\n}\n\n ```\n\nUse in asynchronous contexts.\n ``` rust\n\nuse delay_timer::prelude::*;\n\nuse anyhow::Result;\n\nuse smol::Timer;\nuse std::time::Duration;\n\n#[tokio::main]\nasync fn main() -\u003e Result\u003c()\u003e {\n    // In addition to the mixed (smol \u0026 tokio) runtime\n    // You can also share a tokio runtime with delayTimer, please see api `DelayTimerBuilder::tokio_runtime` for details.\n\n    // Build an DelayTimer that uses the default configuration of the Smol runtime internally.\n    let delay_timer = DelayTimerBuilder::default().build();\n\n    // Develop a print job that runs in an asynchronous cycle.\n    let task_instance_chain = delay_timer.insert_task(build_task_async_print()?)?;\n\n    // Get the running instance of task 1.\n    let task_instance = task_instance_chain.next_with_async_wait().await?;\n\n    // Cancel running task instances.\n    task_instance.cancel_with_async_wait().await?;\n\n\n    // Remove task which id is 1.\n    delay_timer.remove_task(1)?;\n\n    // No new tasks are accepted; running tasks are not affected.\n    delay_timer.stop_delay_timer()\n}\n\nfn build_task_async_print() -\u003e Result\u003cTask, TaskError\u003e {\n    let mut task_builder = TaskBuilder::default();\n\n    let body = || async {\n        println!(\"create_async_fn_body!\");\n\n        Timer::after(Duration::from_secs(3)).await;\n\n        println!(\"create_async_fn_body:i'success\");\n    };\n\n    task_builder\n        .set_task_id(1)\n        .set_frequency_repeated_by_cron_str(\"@secondly\")\n        .set_maximum_parallel_runnable_num(2)\n        .spawn_async_routine(body)\n}\n\n ```\n\n\n Capture the specified environment information and build the closure \u0026 task:\n\n ``` rust\n #[macro_use]\n use delay_timer::prelude::*;\n\n use std::sync::atomic::{\n     AtomicUsize,\n     Ordering::{Acquire, Release},\n };\n use std::sync::Arc;\n\n\n let delay_timer = DelayTimer::new();\n let share_num = Arc::new(AtomicUsize::new(0));\n let share_num_bunshin = share_num.clone();\n \n let body = move || {\n     share_num_bunshin.fetch_add(1, Release);\n };\n \n let task = TaskBuilder::default()\n     .set_frequency_count_down_by_cron_str(expression, 3)\n     .set_task_id(1)\n     .spawn_routine(body)?;\n\n delay_timer.add_task(task)?;\n\n ```\n\n\n\n Building customized-dynamic future tasks:\n ``` rust\n #[macro_use]\n use delay_timer::prelude::*;\n use hyper::{Client, Uri};\n\nfn build_task_customized_async_task() -\u003e Result\u003cTask, TaskError\u003e {\n    let id = 1;\n    let name = String::from(\"someting\");\n    let mut task_builder = TaskBuilder::default();\n\n    let body = move || {\n        let name_ref = name.clone();\n        async move {\n            async_template(id, name_ref).await.expect(\"Request failed.\");\n\n            sleep(Duration::from_secs(3)).await;\n\n            println!(\"create_async_fn_body:i'success\");\n        }\n    };\n\n    task_builder\n        .set_frequency_repeated_by_cron_str(\"0,10,15,25,50 0/1 * * Jan-Dec * 2020-2100\")\n        .set_task_id(5)\n        .set_maximum_running_time(5)\n        .spawn_async_routine(body)\n}\n\n\npub async fn async_template(id: i32, name: String) -\u003e Result\u003c()\u003e {\n    let url = format!(\"https://httpbin.org/get?id={}\u0026name={}\", id, name);\n    let mut res = surf::get(url).await?;\n    dbg!(res.body_string().await?);\n\n    Ok(())\n}\n\n ```\n \nThere's a lot more in the [examples] directory.\n\n\n## License\n\nLicensed under either of\n\n * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)\n\n\n## To Do List\n- [x] Support tokio Ecology.\n- [x] Disable unwrap related methods that will panic.\n- [ ] Thread and running task quit when delayTimer drop.\n- [ ] neaten todo in code, replenish tests and benchmark.\n- [ ] batch-opration.\n- [x] report-for-server.\n- [ ] Future upgrade of delay_timer to multi-wheel mode, different excutor handling different wheels e.g. subtract laps for one wheel, run task for one wheel.\n\n#### Contribution\n\nUnless you explicitly state otherwise, any contribution intentionally submitted\nfor inclusion in the work by you, as defined in the Apache-2.0 license, shall be\ndual licensed as above, without any additional terms or conditions.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FBinChengZhao%2Fdelay-timer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FBinChengZhao%2Fdelay-timer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FBinChengZhao%2Fdelay-timer/lists"}