Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Azure Function Isolated] Cosmos change feed listener tries to start before CreateContainerIfNotExistsAsync #3150

Closed
andersson09 opened this issue Apr 20, 2022 · 6 comments

Comments

@andersson09
Copy link

andersson09 commented Apr 20, 2022

For isolated azure functions, I believe that the cosmos change feed tries to listen before containers have been created. Either that or I'm doing something wrong?

Thanks

Setup:

Program.cs

var host = new HostBuilder()
...
.Build()

var client = host.Services.GetRequiredService<CosmosClient>();
var databaseResponse = await client.CreateDatabaseIfNotExistsAsync(Configuration["DbName"]);
var database = databaseResponse.Database;

await database.CreateContainerIfNotExistsAsync("container", "/id");

host.Run();
Function.cs

[Function("DoSomething")]
public async Task Run(
    [CosmosDBTrigger("%DbName%", "container", 
    ConnectionStringSetting = "CosmosDbConnectionString", 
    LeaseCollectionName = "leases", 
    CreateLeaseCollectionIfNotExists = true)] IReadOnlyList<Entity> input)
{
} 

Error message on local machine:

The listener for function 'DoSomething' was unable to start. Microsoft.Azure.WebJobs.Extensions.CosmosDB: Either the source collection 'container' (in database 'db') or the lease collection 'leases' (in database 'db') does not exist. Both collections must exist before the listener starts. To automatically create the lease collection, set 'CreateLeaseCollectionIfNotExists' to 'true'

Versions

Microsoft.Azure.Functions.Worker.Sdk 1.3.0
Microsoft.Azure.Functions.Worker.Extensions.Http 3.0.13
Microsoft.Azure.Cosmos 3.26.1
Microsoft.Azure.Functions.Worker.Extensions.CosmosDB 3.0.9

@andersson09 andersson09 changed the title [Azure Function Isolated] Cosmos change feed listener starts before CreateContainerIfNotExistsAsync [Azure Function Isolated] Cosmos change feed listener tries to start before CreateContainerIfNotExistsAsync Apr 20, 2022
@ealsur
Copy link
Member

ealsur commented Apr 20, 2022

You might need to reach to Azure Functions. The Cosmos DB SDK does not dictate the order of how Azure Functions decides to load Functions or the Host and on which order.

@ealsur
Copy link
Member

ealsur commented Apr 20, 2022

Closing as out of the scope of this repo. https://github.com/Azure/azure-functions-host might be a good candidate to ask how you can execute code before the runtime loads the declared Functions.

@ealsur ealsur closed this as completed Apr 20, 2022
@andersson09
Copy link
Author

Closing as out of the scope of this repo. https://github.com/Azure/azure-functions-host might be a good candidate to ask how you can execute code before the runtime loads the declared Functions.

After some digging might be this repo: https://github.com/Azure/azure-webjobs-sdk-extensions?

@ealsur
Copy link
Member

ealsur commented Apr 20, 2022

@andersson09 that repo just contains the bindings and extension definitions but it does not govern the runtime initialization order. Extension only scope to what happens when the Extension initializes, but what you are trying to do is execute some code before they initialize. That would mean interacting with the Host lifecycle.

@andersson09
Copy link
Author

andersson09 commented Apr 20, 2022

@ealsur
Copy link
Member

ealsur commented Apr 20, 2022

@andersson09 - Correct, but that error message is just the Extension saying that it cannot find the container, it has nothing to do with your intent to create the container before the Function is initialized. The Extensions start when the Host/Runtime tell them to start, they do not have control over when that happens.

What is happening is that you have some code that you want to run before the Host signals the Extensions to start, but you are not able to put that code in a place where the Host is executing it before. The Host is starting the Extensions either at the same time or before your code.

In order to know where to put your code to achieve what you want, you need guidance or at least someone that knows how the Function Host works. This is not the repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants