Skip to content

client.purge_instance_history_by returns aiohttp.ContentTypeError #183

@hergott

Description

@hergott

I’m deleting orchestration histories, and this demo code wasn’t working for me in multiple ways. I reprogrammed that example, but it was failing with this error:

System.Private.CoreLib: Exception while executing function: Functions.regime_regression. System.Private.CoreLib: Result: Failure
Exception: ContentTypeError: 0, message='Attempt to decode JSON with unexpected mimetype: ', url=URL('http://127.0.0.1:17071/durabletask/instances/?createdTimeFrom=1938-07-03T22:59:05.650856Z&createdTimeTo=2020-08-21T22:59:05.650856Z&runtimeStatus=Completed,Terminated,Failed') 
Stack:   File "C:\...\npm\node_modules\azure-functions-core-tools\bin\workers\python\3.8/WINDOWS/X64\azure_functions_worker\dispatcher.py", line 338, in _handle__invocation_request
    call_result = await fi.func(**args)
  File "C:\...\__app__\regime_regression\__init__.py", line 87, in main
    await purge_history
  File "C:\...\__app__\.venv\lib\site-packages\azure\durable_functions\models\DurableOrchestrationClient.py", line 361, in purge_instance_history_by
    response = await self._delete_async_request(request_url)
  File "C:\...\__app__\.venv\lib\site-packages\azure\durable_functions\models\utils\http_utils.py", 
line 68, in delete_async_request
    data = await response.json()
  File "C:\...\__app__\.venv\lib\site-packages\aiohttp\client_reqrep.py", line 1026, in json        
    raise ContentTypeError(
.

For me, this error occurs when either (a) there are no past instances to delete, or (b) the Function App is run locally (regardless of whether there are instances eligible to be purged).

On my setup, client.purge_instance_history_by works as expected if there are instances that meet the delete criteria and I’m calling a Durable Function deployed to Azure.

This is what I wrote to handle the error; I don’t know whether people view this as long-term solution:

import azure.functions as func          
import azure.durable_functions as df    
from azure.durable_functions.models.OrchestrationRuntimeStatus import OrchestrationRuntimeStatus 

from datetime import datetime, timedelta
from aiohttp import ContentTypeError

...

    purge_past_instances = info.get('purge_past_instances', False)

    if purge_past_instances:
        keep_days = info.get('keep_days', 3)

        client = df.DurableOrchestrationClient(starter)

        created_time_from = datetime.utcnow() + timedelta(days=-30000)
        created_time_to = datetime.utcnow() + timedelta(days=-keep_days)

        runtime_statuses = [OrchestrationRuntimeStatus.Completed,
                            OrchestrationRuntimeStatus.Terminated, OrchestrationRuntimeStatus.Failed]

        try:
            purge_coroutine = client.purge_instance_history_by(
                created_time_from, created_time_to, runtime_statuses)

            purge_history = await purge_coroutine

        except ContentTypeError as e:
            purge_err = f'\nFailed to delete past instances: ContentTypeError.\n{repr(e)}\n'

            return func.HttpResponse(purge_err, status_code=500, mimetype='text/plain')

        else:
            purge_msg = f'Deleted past instances, without analyzing any submitted data. Purged {purge_history.instances_deleted} past instances that were created from {created_time_from} to {created_time_to}.'

            return func.HttpResponse(purge_msg, status_code=200, mimetype='text/plain')

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions