Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

question: Timer Is there any way to know if execution is continuing? #462

Open
linnefromice opened this issue Feb 11, 2024 · 2 comments
Open
Assignees

Comments

@linnefromice
Copy link

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.

#392

If this is not the place to inquire, please let us know.

@linnefromice
Copy link
Author

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);
}

@lwshang
Copy link
Contributor

lwshang commented Apr 12, 2024

@adamspofford-dfinity could you shine some light on this issue?

@adamspofford-dfinity adamspofford-dfinity self-assigned this Apr 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants