From 818c40094b5a4d9d4cc668d1001531483842e057 Mon Sep 17 00:00:00 2001 From: Maksym Arutyunyan Date: Thu, 23 May 2024 08:18:45 +0000 Subject: [PATCH] add motoko timer example --- motoko/canister_logs/README.md | 51 +++++++++++++++++++++++++------- motoko/canister_logs/src/Main.mo | 16 ++++++++++ rust/canister_logs/README.md | 9 ------ 3 files changed, 56 insertions(+), 20 deletions(-) diff --git a/motoko/canister_logs/README.md b/motoko/canister_logs/README.md index 7c4d8f35f..ce9557ae0 100644 --- a/motoko/canister_logs/README.md +++ b/motoko/canister_logs/README.md @@ -38,13 +38,17 @@ $ cd examples/motoko/canister_logs $ dfx deploy ``` -### Step 3: Check canister has no logs yet: +### Step 3: Check canister logs: + +Expect to see logs from timer traps. ```shell # Terminal B $ dfx canister logs CanisterLogs - -# Expect to see no logs. +[0. 2024-05-23T08:14:40.60349175Z]: right before timer trap +[1. 2024-05-23T08:14:40.60349175Z]: [TRAP]: timer trap +[2. 2024-05-23T08:14:46.160555676Z]: right before timer trap +[3. 2024-05-23T08:14:46.160555676Z]: [TRAP]: timer trap ``` ### Step 4: Call `print` method and check the logs: @@ -56,7 +60,13 @@ $ dfx canister call CanisterLogs print hi # Expect to see new log entry. $ dfx canister logs CanisterLogs -[0. 2024-05-22T11:37:28.080234848Z]: hi +... +[14. 2024-05-23T08:15:15.829022842Z]: right before timer trap +[15. 2024-05-23T08:15:15.829022842Z]: [TRAP]: timer trap +[16. 2024-05-23T08:15:19.728382106Z]: hi +[17. 2024-05-23T08:15:21.079923035Z]: right before timer trap +[18. 2024-05-23T08:15:21.079923035Z]: [TRAP]: timer trap +... ``` ### Step 5: Start constantly polling logs: @@ -66,8 +76,13 @@ In order not to call `dfx canister logs CanisterLogs` after every canister call ```shell # Terminal C $ ./poll_logs.sh -[0. 2024-05-22T11:37:28.080234848Z]: hi - +... +[14. 2024-05-23T08:15:15.829022842Z]: right before timer trap +[15. 2024-05-23T08:15:15.829022842Z]: [TRAP]: timer trap +[16. 2024-05-23T08:15:19.728382106Z]: hi +[17. 2024-05-23T08:15:21.079923035Z]: right before timer trap +[18. 2024-05-23T08:15:21.079923035Z]: [TRAP]: timer trap +... ``` ### Step 6: Call `print`, `trap`, `panic` and other canister methods: @@ -94,10 +109,24 @@ Observe recorded logs that might look similar to this: ```shell # Terminal C -[0. 2024-05-22T12:21:52.333589617Z]: hi -[1. 2024-05-22T12:22:27.330332077Z]: hello! -[2. 2024-05-22T12:22:32.062734677Z]: yey! -[3. 2024-05-22T12:22:36.685104375Z]: right before trap -[4. 2024-05-22T12:22:36.685104375Z]: [TRAP]: oops! +... +[49. 2024-05-23T08:16:40.702368542Z]: right before timer trap +[50. 2024-05-23T08:16:40.702368542Z]: [TRAP]: timer trap +[51. 2024-05-23T08:16:42.785727368Z]: hi! +[52. 2024-05-23T08:16:45.69960063Z]: right before timer trap +[53. 2024-05-23T08:16:45.69960063Z]: [TRAP]: timer trap +[54. 2024-05-23T08:16:47.222911354Z]: hello! +[55. 2024-05-23T08:16:50.875445893Z]: right before timer trap +[56. 2024-05-23T08:16:50.875445893Z]: [TRAP]: timer trap +[57. 2024-05-23T08:16:51.570602735Z]: yey! +[58. 2024-05-23T08:16:55.783308056Z]: right before timer trap +[59. 2024-05-23T08:16:55.783308056Z]: [TRAP]: timer trap +[60. 2024-05-23T08:16:56.536701165Z]: right before trap +[61. 2024-05-23T08:16:56.536701165Z]: [TRAP]: oops! +[62. 2024-05-23T08:17:00.759041661Z]: right before timer trap +[63. 2024-05-23T08:17:00.759041661Z]: [TRAP]: timer trap +[64. 2024-05-23T08:17:05.657467481Z]: right before timer trap +[65. 2024-05-23T08:17:05.657467481Z]: [TRAP]: timer trap +... ``` diff --git a/motoko/canister_logs/src/Main.mo b/motoko/canister_logs/src/Main.mo index 3aeac1ea8..b228bdc72 100644 --- a/motoko/canister_logs/src/Main.mo +++ b/motoko/canister_logs/src/Main.mo @@ -1,7 +1,23 @@ import Debug "mo:base/Debug"; +import { abs } = "mo:base/Int"; +import { now } = "mo:base/Time"; +import { setTimer; recurringTimer } = "mo:base/Timer"; actor CanisterLogs { + let timerDelaySeconds = 5; + + private func execute_timer() : async () { + Debug.print("right before timer trap"); + Debug.trap("timer trap"); + }; + + ignore setTimer(#seconds (timerDelaySeconds - abs(now() / 1_000_000_000) % timerDelaySeconds), + func () : async () { + ignore recurringTimer(#seconds timerDelaySeconds, execute_timer); + await execute_timer(); + }); + public func print(text : Text) : async () { Debug.print(text); }; diff --git a/rust/canister_logs/README.md b/rust/canister_logs/README.md index 84f9e6386..4a1b9ab2f 100644 --- a/rust/canister_logs/README.md +++ b/rust/canister_logs/README.md @@ -62,15 +62,11 @@ $ dfx canister call canister_logs print hi # Expect to see new log entry. $ dfx canister logs canister_logs ... -[16. 2024-05-22T12:36:15.638667167Z]: right before timer trap -[17. 2024-05-22T12:36:15.638667167Z]: [TRAP]: timer trap [18. 2024-05-22T12:36:20.881326098Z]: right before timer trap [19. 2024-05-22T12:36:20.881326098Z]: [TRAP]: timer trap [20. 2024-05-22T12:36:26.305162772Z]: hi [21. 2024-05-22T12:36:27.185879186Z]: right before timer trap [22. 2024-05-22T12:36:27.185879186Z]: [TRAP]: timer trap -[23. 2024-05-22T12:36:33.486805581Z]: right before timer trap -[24. 2024-05-22T12:36:33.486805581Z]: [TRAP]: timer trap ``` ### Step 5: Start constantly polling logs: @@ -81,17 +77,12 @@ In order not to call `dfx canister logs canister_logs` after every canister call # Terminal C $ ./poll_logs.sh ... -[16. 2024-05-22T12:36:15.638667167Z]: right before timer trap -[17. 2024-05-22T12:36:15.638667167Z]: [TRAP]: timer trap [18. 2024-05-22T12:36:20.881326098Z]: right before timer trap [19. 2024-05-22T12:36:20.881326098Z]: [TRAP]: timer trap [20. 2024-05-22T12:36:26.305162772Z]: hi [21. 2024-05-22T12:36:27.185879186Z]: right before timer trap [22. 2024-05-22T12:36:27.185879186Z]: [TRAP]: timer trap -[23. 2024-05-22T12:36:33.486805581Z]: right before timer trap -[24. 2024-05-22T12:36:33.486805581Z]: [TRAP]: timer trap ... - ``` ### Step 6: Call `print`, `trap`, `panic` and other canister methods: