Skip to content

Commit 85f8313

Browse files
committed
chore: Optimize examples.
1 parent bd0b04c commit 85f8313

File tree

5 files changed

+33
-8
lines changed

5 files changed

+33
-8
lines changed

examples/demo_async_std.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,20 @@ use delay_timer::prelude::*;
55
use delay_timer::utils::convenience::functions::unblock_process_task_fn;
66
use smol::Timer;
77
use std::time::Duration;
8+
use tracing::Level;
9+
use tracing_subscriber::FmtSubscriber;
810

911
// You can replace the 62 line with the command you expect to execute.
1012
#[async_std::main]
1113
async fn main() -> Result<()> {
14+
// a builder for `FmtSubscriber`.
15+
FmtSubscriber::builder()
16+
// all spans/events with a level higher than TRACE (e.g, debug, info, warn, etc.)
17+
// will be written to stdout.
18+
.with_max_level(Level::DEBUG)
19+
// completes the builder.
20+
.init();
21+
1222
// Build an DelayTimer that uses the default configuration of the Smol runtime internally.
1323
let delay_timer = DelayTimerBuilder::default()
1424
.smol_runtime_by_default()
@@ -22,6 +32,7 @@ async fn main() -> Result<()> {
2232

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

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

5465
task_builder
5566
.set_task_id(1)
56-
.set_frequency_repeated_by_cron_str("*/6 * * * * * *")
67+
.set_frequency_repeated_by_cron_str("* * * * * * *")
5768
.set_maximum_parallel_runnable_num(2)
5869
.spawn_async_routine(body)
5970
}

examples/demo_async_tokio.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,20 @@ use delay_timer::utils::convenience::functions::unblock_process_task_fn;
55
use hyper::{Client, Uri};
66
use std::time::Duration;
77
use tokio::time::sleep;
8+
use tracing::Level;
9+
use tracing_subscriber::FmtSubscriber;
810

911
// You can replace the 66 line with the command you expect to execute.
1012
#[tokio::main]
1113
async fn main() -> Result<()> {
14+
// a builder for `FmtSubscriber`.
15+
FmtSubscriber::builder()
16+
// all spans/events with a level higher than TRACE (e.g, debug, info, warn, etc.)
17+
// will be written to stdout.
18+
.with_max_level(Level::DEBUG)
19+
// completes the builder.
20+
.init();
21+
1222
// In addition to the mixed (smol & tokio) runtime
1323
// You can also share a tokio runtime with delayTimer, please see api `DelayTimerBuilder::tokio_runtime` for details.
1424

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

7585
let body = move || {
7686
#[allow(deprecated)]
77-
unblock_process_task_fn("php /home/open/project/rust/repo/myself/delay_timer/examples/try_spawn.php >> ./try_spawn.txt".into(), task_id)
87+
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)
7888
};
7989
task_builder
8090
.set_frequency_repeated_by_seconds(1)

examples/share_tokio_runtime.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ fn main() -> Result<(), Report> {
1818
.tokio_runtime_shared_by_custom(rt)
1919
.build();
2020
let mut chain;
21-
for cron_str in ["0 33 12 * * * *", "0 33 13 * * * *"] {
22-
chain = delay_timer.insert_task(build_task_async_print(cron_str)?)?;
21+
for (id, cron_str) in [(1, "0 1 6 * * * *"), (2, "0 10 6 * * * *")] {
22+
chain = delay_timer.insert_task(build_task_async_print(id, cron_str)?)?;
2323
chain.next_with_async_wait().await?;
2424
}
2525

@@ -30,15 +30,15 @@ fn main() -> Result<(), Report> {
3030
Ok(read_config()?)
3131
}
3232

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

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

4040
task_builder
41-
.set_task_id(1)
41+
.set_task_id(id)
4242
.set_frequency_repeated_by_cron_str(cron_str)
4343
.set_schedule_iterator_time_zone(ScheduleIteratorTimeZone::Utc)
4444
.set_maximum_parallel_runnable_num(2)

src/timer/timer_core.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,12 @@ impl Timer {
223223
}
224224
}
225225

226-
trace!("timestamp: {}, task_ids: {:?}", current_timestamp, task_ids);
226+
trace!(
227+
"second_hand: {}, timestamp: {}, task_ids: {:?}",
228+
second_hand,
229+
current_timestamp,
230+
task_ids
231+
);
227232

228233
// Centralize task processing to avoid duplicate lock requests and releases.
229234
// FIXME: https://github.com/BinChengZhao/delay-timer/issues/29

tests/simulation.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ fn test_instance_state() -> anyhow::Result<()> {
3232
// Get the first task instance.
3333
let instance = task_instance_chain.next_with_wait()?;
3434

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

0 commit comments

Comments
 (0)