IC Cron - let's schedule some tasks bois


Adding more text to make the forum engine happy.

This is great, but how do we consume it with Motoko? (Not a Rust dev.)

I believe there is no way for that rn.

1 Like

Thank you for responding. It looks like we’ll be getting a system function named heartbeat to implement cron jobs.

If there is more work than the interval between heartbeats ( ~2s), how will this work given that the underlying heartbeat function is sync (not async)?

Async is just a wrapper around request-response. All your requests will be sent during the heartbeat function. All your responses will be processed at the block they come back to your canister.

If you want to call an async function inside the heartbeat you could use ‘ic_cdk::block_on()’ (or something like this, I don’t remember the name).

1 Like

Just adding to your answer @senior.joinu. Here’s an ultra-basic async example that does automated inter-canister calls (just using heartbeat). It took me a little while to figure this out so hopefully it helps others :rocket:

use ic_cdk::export::Principal;
use ic_cdk::api::time;
use ic_cdk::api::call;

static mut LAST_JOB_TIME: u64 = 0;

#[export_name = "canister_heartbeat"]
fn canister_heartbeat() {
    let duration: u64  = 1_000_000_000 * 10; // 10 seconds
    let t = time();
    unsafe {
        if t - duration > LAST_JOB_TIME {
            LAST_JOB_TIME = t;

async fn test_api_call() {
    let principal = Principal::from_text("rrkah-fqaaa-aaaaa-aaaaq-cai").unwrap();
    let res: Result<(String,), _> = call::call(principal, "ping", ()).await;
    let res_string = format!("{:?}", res);
    ic_cdk::print(res_string); // -> Ok(("pong",))