Motoko Hearbeat frequency and example code

I noticed the addition of this to the docs website

Anyone with practical experience with the rust counterpart knows how frequently this is called? I noticed this in the interface spec

I’m trying to execute something every minute. What would the corresponding Motoko code look like?

You should be able to adapt this example to do what you want, by looking at the time the hearbeat is invoked.

Right, that was my question. On mainnet, should I expect the function to run once every second? That way i can set a check for n >= 60, and if the condition is met, set n to 0 and execute my logic, otherwise just increment n

2 Likes

You can’t rely on the hearbeat running every second.

I think it would be better to use Time.now() to record the time of the last hearbeat and see if this hearbeat needs to do work.

2 Likes

Is heartbeat live in motoko on main net now?

Is Time.now() in Motoko calling ic0.time? Not very familiar with Motoko

Otherwise wouldn’t Time.now() introduce local non -determinizm when used to make decisions in execution?

At least that is what I got from @akhilesh.singhania recent video cast on YouTube. @nomeata suggests here([quote=“nomeata, post:4, topic:9268”]
You can use ic0.time to get a suitable timestamp (but careful, don’t assume it to be_strictly_ monotonous – but that should be the same for a system clock with low resolution)
[/quote]
)

1 Like

It is the time of the block which the proposer sets and validators endorse.

1 Like

Yeah, Time.now() calls ic0.time.

2 Likes

@Icdev2dev, you seem to be repeatedly worried about non-determinism creeping in if canisters do the wrong thing (here, in the discussion of stable memory). Rest assured: Canisters run in a carefully constructed sandbox, and nothing they can do can endanger determinism. (Else the whole IC would fall apart, and it it hasn’t, yet, right, so Q.E.D.)

1 Like

Thanks, @nomeata for the reassurance! Yes, I WAS worried. Your words have put this to rest.