How to get the duration of the call inside of `ctx...
# ask-ai
m
How to get the duration of the call inside of
ctx.add_shutdown_callback(_shutdown_hook)
?
t
Thanks for reaching out, Marcus. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To get the duration of the call inside
ctx.add_shutdown_callback(_shutdown_hook)
, you need to store the call's start time when it begins, then calculate the duration in the shutdown callback by subtracting the start time from the current time. Example implementation:
Copy code
import time

async def entrypoint(ctx: JobContext):
    call_start_time = time.time()  # Store start time when call begins

    async def on_shutdown():
        duration = time.time() - call_start_time  # Calculate duration on shutdown
        <http://logger.info|logger.info>(f"Call duration: {duration:.2f} seconds")

    ctx.add_shutdown_callback(on_shutdown)
This logs the call duration when the shutdown callback triggers, ensuring you have the elapsed call time.