MicrosoftDocs / azure-docs

Open source documentation of Microsoft Azure
https://docs.microsoft.com/azure
Creative Commons Attribution 4.0 International
10.21k stars 21.36k forks source link

update python sample code for Purge instance history #87913

Open yull1860 opened 2 years ago

yull1860 commented 2 years ago

The python sample code using purge_instance_history_by is not correct, wrong line commented and see bold words for update .

import azure.functions as func
import azure.durable_functions as df
from azure.durable_functions.models.DurableOrchestrationStatus import OrchestrationRuntimeStatus
from datetime import datetime, timedelta

async def main(req: func.HttpRequest, starter: str, instance_id: str) -> func.HttpResponse:
    client = df.DurableOrchestrationClient(starter)
    #created_time_from = datetime.min 
    created_time_from = **datetime.today() + timedelta(days = -60)**
    created_time_to = datetime.today() + timedelta(days = -30)
    runtime_statuses = [OrchestrationRuntimeStatus.Completed]
    #return client.purge_instance_history_by(created_time_from, created_time_to, runtime_statuses)
    return **await** client.purge_instance_history_by(created_time_from, created_time_to, runtime_statuses)

Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Karishma-Tiwari-MSFT commented 2 years ago

Thanks for the feedback! We are currently investigating and will update you shortly.

PramodValavala-MSFT commented 2 years ago

@yull1860 the first line is intended as such since the idea is to delete all instances older than 30 days. So, the oldest date possible to 30 days ago, are the instances we are deleting. Also, since the function is async and we don't really need to do anything with the result, returning the async operation should be OK I believe (unless my understanding of python async is wrong :))

yull1860 commented 2 years ago

@PramodValavala-MSFT see my comments below :

**>>> created_time_from = datetime.min

created_time_to = datetime.today() + timedelta(days = -1) a=f"testing w/ = '{created_time_from} - {created_time_to}'" a "testing w/ = '0001-01-01 00:00:00** - 2022-02-10 17:50:07.650003'"

use above parameter will result Error : Please provide value for 'createdTimeFrom' parameter.

/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/dispatcher.py:257: RuntimeWarning: coroutine 'DurableOrchestrationClient.purge_instance_history_by' was never awaited

MughundhanRaveendran-MSFT commented 2 years ago

@ggailey777 , @cgillum , Could you please look into this ?

cgillum commented 2 years ago

I think @davidmrdavid is the right guy to help with this one. :)

davidmrdavid commented 2 years ago

Hi all.

@MughundhanRaveendran-MSFT:

I think the await may be needed here. And looking into the docs, I think it's needed in quite a few places in other code snippets.

As for the use of datetime.min, I need to set up a reproducer for that.

@MughundhanRaveendran-MSFT - do you mind assigning me to this ticket as well so I can follow up on it? Thanks!

thiezn commented 2 years ago

Hi all,

I can confirm I'm running into issues using datetime.min for the created_time_from as well. Here is the stacktrace I get indicating there is a HTTP 400 status code message returned:

Result: Failure Exception: Exception: 
The operation failed with an unexpected status code 400 Stack: 

File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/dispatcher.py", line 403, in _handle__invocation_request
call_result = await self._run_async_func(

File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/dispatcher.py", line 655, in _run_async_func
return await ExtensionManager.get_async_invocation_wrapper(

File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/extension.py", line 147, in get_async_invocation_wrapper
result = await function(**args)

File "/home/site/wwwroot/orchestration_maintenance/main.py", line 27, in main
result = await client.purge_instance_history_by(created_time_from, created_time_to)

File "/home/site/wwwroot/.python_packages/lib/site-packages/azure/durable_functions/models/DurableOrchestrationClient.py", line 413, in purge_instance_history_by
return self._parse_purge_instance_history_response(response)

File "/home/site/wwwroot/.python_packages/lib/site-packages/azure/durable_functions/models/DurableOrchestrationClient.py", line 628, in _parse_purge_instance_history_response
raise Exception(result)

Since I want to remove every result before a given date, I managed to clear out things by removing the created_time_from al together. My code now looks something like this

async def main(maintenanceTimer: func.TimerRequest, starter: str):
    """Main entrypoint."""
    client = df.DurableOrchestrationClient(starter)
    created_time_to = datetime.today() + timedelta(days=-1)
    logger.info(f"Purging orchestration runs older than {created_time_to}")

    result = await client.purge_instance_history_by(created_time_to)

    if result:
        logger.info(f"{result.instances_deleted} instances deleted")
    else:
        logger.warning("Nothing returned from purge instance history.")
amatt13 commented 1 year ago

Is this still being worked on?

davidmrdavid commented 1 year ago

I just merged a documentation update to add the missing await statement, so that should be reflected in the documentation no later than tomorrow. Apologies for the delay on that, but it should be addressed now

Regarding the use of datetime.min: I still need to look into that. I managed to repro the issue a few months back, and it seemed to be a problem with Azurite in particular, but I would need to re-run some tests to confirm.