cdk-rs
cdk-rs copied to clipboard
question: `Timer` Is there any way to know if execution is continuing?
I want to reliably perform the same task again if the timer execution stops. There is one issue that cannot be resolved. If the canister is stopped, I don't think I can determine how to restart the timer. There are two ways to stop the canister: manually by executing stop or when cycles are exhausted. If manually stopped, timer continues; if cycles are exhausted, timer is stopped.
The global timer is also deactivated upon changes to the canister's Wasm module (calling install_code, install_chunked_code, uninstall_code methods of the management canister or if the canister runs out of cycles). https://internetcomputer.org/docs/current/references/ic-interface-spec#global-timer
If it keeps running, TimerId can be saved in heap memory, and in case of upgrade, it is enough to set the timer again (by post_upgrade hook), so there should be no problem except for the above case.
If stable memory is available, TimerId can be stored continuously, so that in any case one timer can always be maintained by stopping past timers and setting a new timer.
https://github.com/dfinity/cdk-rs/issues/392
If this is not the place to inquire, please let us know.
I am writing code like this now, but have not been able to implement it to keep one timer on at all times when the canister stops
thread_local! {
...
static TIMER_ID: RefCell<Option<TimerId>> = RefCell::new(None);
}
/// Reset timer
/// NOTE: Duplicate timer run if canister is stopped/restarted normally
#[update]
#[candid_method(update)]
async fn reset_timer() {
if let Some(timer_id) = _timer_id() {
ic_cdk_timers::clear_timer(timer_id);
_reset_timer_id();
}
let timer_id = ic_cdk_timers::set_timer_interval(...);
_set_timer_id(timer_id);
}
#[post_upgrade]
fn post_upgrade() {
let timer_id = ic_cdk_timers::set_timer_interval(...);
_set_timer_id(timer_id);
}
@adamspofford-dfinity could you shine some light on this issue?