microsoft / botframework-sdk

Bot Framework provides the most comprehensive experience for building conversation applications.
MIT License
7.5k stars 2.44k forks source link

Unable to set the TTL for CosmosDB userstate container #5890

Closed nmishr closed 4 years ago

nmishr commented 4 years ago

This is not for asking questions or filing bugs

To get help with an issue with your bot

To file an issue against a component please go to the components repo

Issue

When creating the CosmosDB container, there should be a way to set the TTL for the user state

Proposed change

Describe the proposed solution

Component Impact

Describe which components need to be updated

Customer Impact

Describe the impact on customers

Tracking Status

Dotnet SDK

Javascript SDK

Java SDK

Python SDK

Emulator

Samples

Docs

Tools

[dcr]

mdrichardson commented 4 years ago

@nmishr At one point, we decided not to support this in the Bot Framework SDK since it just adds additional complexity that very few customers are asking for. However, you can set TTL manually after the SDK creates the container.

EricDahlvang commented 4 years ago

If the goal is for the sdk to set ttl on the container, one option is to use an initializer to ensure the database container has the desired Time To Live, before creating the IStorage:

public static class CosmosDbStorageInitializer 
{
    public static async Task<IStorage> GetStorageAsync(CosmosDbPartitionedStorageOptions options, int timeToLiveInSeconds = -1)
    {
        using (var client = new CosmosClient(
            options.CosmosDbEndpoint,
            options.AuthKey,
            options.CosmosClientOptions ?? new CosmosClientOptions()))
        {
            var containerResponse = await client
                .GetDatabase(options.DatabaseId)
                .DefineContainer(options.ContainerId, "/id")
                .WithDefaultTimeToLive(timeToLiveInSeconds)
                .WithIndexingPolicy().WithAutomaticIndexing(false).Attach()
                .CreateIfNotExistsAsync(options.ContainerThroughput)
                .ConfigureAwait(false);
        }

        return new CosmosDbPartitionedStorage(options);
    }
} 
garypretty commented 4 years ago

I have received the suggestion about adding reference to TTL in the docs as part of the R10 docs pillar. Along with that, we have another open issue for ensuring we call out that the newer partitioned storage provider doesn't create the database for you. I think both items could be referenced in an update to the existing cosmos db docs, adding links to the Cosmos TTL docs and also referencing the option Eric specified above.

mdrichardson commented 4 years ago

I'm going to close this issue as there are now two workarounds and there's a new docs issue that will address this for the future.