Pocket IC - testing jobs / scheduled jobs

I have a job setup in a canister. The code looks similar to the below code but I’ve consolidated the code to make it easier to understand the parts involved.

pub fn run_interval(interval: Duration, func: fn()) {
    ic_cdk_timers::set_timer_interval(interval, func);
}

pub fn start_job() {
    run_now_then_interval(Duration::from_millis(WEEK_IN_MS), run);
}

pub fn run() {
    ic_cdk::spawn(mc_test())
}

pub async fn mc_test() {
    debug!("lets go")
}

#[init]
fn init(args: Args) {
    jobs::start_job()
}

Question : I never see the log when i get the logs during the test. Is this the correct way to setup a job?

Thanks :slight_smile:

You might need to execute rounds on the PocketIC instance, using pic.tick() to see some output.

Also tried that. Also tried adding like 30 pick() calls :frowning:

Also if the timer only runs in a week, you might need to advance time pic.advance_time(...) and then tick (pic.tick()) - potentially a few times (since timers need not run every round).

Also tried to advance_time to the exactly 1 week, 1 hour after 1 week, 2 hours after 1 week, 2 weeks, 1 month. Still doesn’t work for me.

What is kind of weird is that I have a seperate timer function that does work every day, it looks like this.

pub fn run_now_then_interval(interval: Duration, func: fn()) {
    ic_cdk_timers::set_timer_interval(interval, func);
    ic_cdk_timers::set_timer(Duration::ZERO, func);
}

essentially the above code runs as soon as the canister boots up and then on the interval.

A few more ideas:

  • init doesn’t run if a canister is upgraded, so your timers won’t be set up after an upgrade
  • could you please try invoking run() right after jobs::start_job() to figure out if your issue is caused by timers or something else

All good. It was something else entirely. Appreciate the fast reponses from you guys though <3 seriously great to see you guys active on the forums so much :slight_smile: