Skip to content

Commit

Permalink
chore: Optimize examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
BinChengZhao committed Mar 6, 2022
1 parent bd0b04c commit 85f8313
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 8 deletions.
13 changes: 12 additions & 1 deletion examples/demo_async_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,20 @@ use delay_timer::prelude::*;
use delay_timer::utils::convenience::functions::unblock_process_task_fn;
use smol::Timer;
use std::time::Duration;
use tracing::Level;
use tracing_subscriber::FmtSubscriber;

// You can replace the 62 line with the command you expect to execute.
#[async_std::main]
async fn main() -> Result<()> {
// a builder for `FmtSubscriber`.
FmtSubscriber::builder()
// all spans/events with a level higher than TRACE (e.g, debug, info, warn, etc.)
// will be written to stdout.
.with_max_level(Level::DEBUG)
// completes the builder.
.init();

// Build an DelayTimer that uses the default configuration of the Smol runtime internally.
let delay_timer = DelayTimerBuilder::default()
.smol_runtime_by_default()
Expand All @@ -22,6 +32,7 @@ async fn main() -> Result<()> {

// Get the running instance of task 1.
let task_instance = task_instance_chain.next_with_async_wait().await?;
Timer::after(Duration::from_secs(1)).await;

// Cancel running task instances.
task_instance.cancel_with_async_wait().await?;
Expand Down Expand Up @@ -53,7 +64,7 @@ fn build_task_async_print() -> Result<Task, TaskError> {

task_builder
.set_task_id(1)
.set_frequency_repeated_by_cron_str("*/6 * * * * * *")
.set_frequency_repeated_by_cron_str("* * * * * * *")
.set_maximum_parallel_runnable_num(2)
.spawn_async_routine(body)
}
Expand Down
12 changes: 11 additions & 1 deletion examples/demo_async_tokio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,20 @@ use delay_timer::utils::convenience::functions::unblock_process_task_fn;
use hyper::{Client, Uri};
use std::time::Duration;
use tokio::time::sleep;
use tracing::Level;
use tracing_subscriber::FmtSubscriber;

// You can replace the 66 line with the command you expect to execute.
#[tokio::main]
async fn main() -> Result<()> {
// a builder for `FmtSubscriber`.
FmtSubscriber::builder()
// all spans/events with a level higher than TRACE (e.g, debug, info, warn, etc.)
// will be written to stdout.
.with_max_level(Level::DEBUG)
// completes the builder.
.init();

// In addition to the mixed (smol & tokio) runtime
// You can also share a tokio runtime with delayTimer, please see api `DelayTimerBuilder::tokio_runtime` for details.

Expand Down Expand Up @@ -74,7 +84,7 @@ fn build_task_async_execute_process() -> Result<Task, TaskError> {

let body = move || {
#[allow(deprecated)]
unblock_process_task_fn("php /home/open/project/rust/repo/myself/delay_timer/examples/try_spawn.php >> ./try_spawn.txt".into(), task_id)
unblock_process_task_fn("/opt/homebrew/bin/php /Users/bincheng_paopao/project/repo/rust/myself/delay-timer/examples/try_spawn.php >> ./try_spawn.txt".into(), task_id)
};
task_builder
.set_frequency_repeated_by_seconds(1)
Expand Down
8 changes: 4 additions & 4 deletions examples/share_tokio_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ fn main() -> Result<(), Report> {
.tokio_runtime_shared_by_custom(rt)
.build();
let mut chain;
for cron_str in ["0 33 12 * * * *", "0 33 13 * * * *"] {
chain = delay_timer.insert_task(build_task_async_print(cron_str)?)?;
for (id, cron_str) in [(1, "0 1 6 * * * *"), (2, "0 10 6 * * * *")] {
chain = delay_timer.insert_task(build_task_async_print(id, cron_str)?)?;
chain.next_with_async_wait().await?;
}

Expand All @@ -30,15 +30,15 @@ fn main() -> Result<(), Report> {
Ok(read_config()?)
}

fn build_task_async_print(cron_str: &'static str) -> Result<Task, TaskError> {
fn build_task_async_print(id: u64, cron_str: &'static str) -> Result<Task, TaskError> {
let mut task_builder = TaskBuilder::default();

let body = move || async move {
info!("create_async_fn_body:i'success {}", cron_str);
};

task_builder
.set_task_id(1)
.set_task_id(id)
.set_frequency_repeated_by_cron_str(cron_str)
.set_schedule_iterator_time_zone(ScheduleIteratorTimeZone::Utc)
.set_maximum_parallel_runnable_num(2)
Expand Down
7 changes: 6 additions & 1 deletion src/timer/timer_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,12 @@ impl Timer {
}
}

trace!("timestamp: {}, task_ids: {:?}", current_timestamp, task_ids);
trace!(
"second_hand: {}, timestamp: {}, task_ids: {:?}",
second_hand,
current_timestamp,
task_ids
);

// Centralize task processing to avoid duplicate lock requests and releases.
// FIXME: https://github.com/BinChengZhao/delay-timer/issues/29
Expand Down
1 change: 0 additions & 1 deletion tests/simulation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ fn test_instance_state() -> anyhow::Result<()> {
// Get the first task instance.
let instance = task_instance_chain.next_with_wait()?;


// The task was still running when the instance was first obtained.
assert_eq!(instance.get_state(), instance::RUNNING);

Expand Down

0 comments on commit 85f8313

Please sign in to comment.