Best way to handle status check using activity

I am trying to orchestrate some k8s object using temporal.

I want to implement status check on k8s resource, how can I do it ?

A few approaches I thought of were:

  1. Let the activity to check status be short-lived, and continuously calling that health check activity in a loop. Problem with this approach is it will make the workflow execution history very large, the lifecycle of those k8s object can be for many days.

  2. Another approach could be putting loop inside acitivty and calling that health check activity once, it will keep the workflow execution history small but activity will live long. The problem what if workflow is terminated, activity will still be running?

What is the best approach to solve this ?

Thanks @maxim , I tried the approach you mention in another post.

But looks like my heartbeat is not being sent. I am using python SDK.

this is my activity definition

@activity.defn()
async def check_status(name: str) -> None:
    while True:
        print(f"Checking status for {name}.")
        print(
            f"Activity ID: {activity.info().activity_id}\tActivity Attempt: {activity.info().attempt}"
        )
        activity.heartbeat("Running")
        time.sleep(1)

This is how I am executing the activity.

        await workflow.execute_activity(
            check_status,
            name,
            start_to_close_timeout=timedelta(days=30),
            heartbeat_timeout=timedelta(seconds=30),
            retry_policy=RetryPolicy(maximum_attempts=2),
        )

As per the logs, my worker is running in first attempt,

Checking status for demo1.
Activity ID: 2	Activity Attempt: 1
Checking status for demo1.
Activity ID: 2	Activity Attempt: 1
Checking status for demo1.
Activity ID: 2	Activity Attempt: 1
Checking status for demo1.
Activity ID: 2	Activity Attempt: 1
Checking status for demo1.
Activity ID: 2	Activity Attempt: 1
Checking status for demo1.
Activity ID: 2	Activity Attempt: 1
Checking status for demo1.
Activity ID: 2	Activity Attempt: 1
Checking status for demo1.
Activity ID: 2	Activity Attempt: 1
Checking status for demo1.
Activity ID: 2	Activity Attempt: 1
Checking status for demo1.
Activity ID: 2	Activity Attempt: 1
Checking status for demo1.
Activity ID: 2	Activity Attempt: 1
Checking status for demo1.
Activity ID: 2	Activity Attempt: 1

But in the UI, I can see it says activity timeout, the last heartbeat was 11 mins ago.

Am I missing something while sending heartbeat?

You should never block a thread in an async def call. This will cause every asyncio function in the system to block and cause issues with the entire system. Use asyncio.sleep

Thanks @Chad_Retz it’s working now.