Azure functions 是否需要Azure持久功能活动触发器?

Azure functions 是否需要Azure持久功能活动触发器?,azure-functions,Azure Functions,我有一个简单的持久功能,我的目标是执行一些后台工作(在本例中,启动Azure数据库) 下面的代码正在运行,但是我想知道需要ActivityTrigger函数的目的是什么 如果我不包括它,并尝试在CreateDB本身中执行工作,则持久功能将永远无法正确完成并保持运行 public static class CreateDBFn { private const string CreateDBConstant = "CreateDB"; private const string Cre

我有一个简单的持久功能,我的目标是执行一些后台工作(在本例中,启动Azure数据库)

下面的代码正在运行,但是我想知道需要ActivityTrigger函数的目的是什么

如果我不包括它,并尝试在
CreateDB
本身中执行工作,则持久功能将永远无法正确完成并保持运行

public static class CreateDBFn
{
    private const string CreateDBConstant = "CreateDB";
    private const string CreateDBTaskConstant = CreateDBConstant + "Task";

    [FunctionName(CreateDBConstant + "Start")]
    public static async Task<HttpResponseMessage> Run(
   [HttpTrigger(AuthorizationLevel.Function, methods: "post", Route = CreateDBConstant + "/{Name}")] HttpRequestMessage req,
   [OrchestrationClient] DurableOrchestrationClientBase starter,
   string Name,
   ILogger log)
    {
        string instanceId = await starter.StartNewAsync(CreateDBConstant, Name);

        log.LogInformation($"Started orchestration with ID = '{instanceId}'.");

        var res = starter.CreateCheckStatusResponse(req, instanceId);
        res.Headers.RetryAfter = new RetryConditionHeaderValue(TimeSpan.FromSeconds(10));
        return res;
    }

    [FunctionName(CreateDBConstant)]
    public static async Task<bool> CreateDB([OrchestrationTrigger] DurableOrchestrationContextBase context)
    {
        var Name = context.GetInput<string>();

        // I'd like to just do work here
        await Task.Delay(30000);

        // Why is it required to call out to an Activity?
        var success = await context.CallActivityAsync<bool>(CreateDBTaskConstant, Name);

        return true;
    }

    [FunctionName(CreateDBTaskConstant)]
    public static async Task<bool> CreateDBTask([ActivityTrigger] string Name)
    {
        // preform work
        await Task.Delay(30000);

        return true;
    }
}
公共静态类CreateDBFn
{
私有常量字符串CreateDBConstant=“CreateDB”;
私有常量字符串CreateDBTaskConstant=CreateDBConstant+“任务”;
[函数名(CreateDBConstant+“开始”)]
公共静态异步任务运行(
[HttpTrigger(AuthorizationLevel.Function,methods:“post”,Route=CreateDBConstant+“/{Name}”)]HttpRequestMessage请求,
[OrchestrationClient]DurableOrchestrationClientBase启动器,
字符串名,
ILogger日志)
{
字符串instanceId=await starter.StartNewAsync(CreateDBConstant,Name);
LogInformation($“启动了ID为“{instanceId}”的业务流程”;
var res=starter.CreateCheckStatusResponse(请求,实例ID);
res.Headers.RetryAfter=新的RetryConditionHeaderValue(TimeSpan.FromSeconds(10));
返回res;
}
[函数名(CreateDBConstant)]
公共静态异步任务CreateDB([OrchestrationTrigger]DurableOrchestrationContextBase上下文)
{
var Name=context.GetInput();
//我只想在这里工作
等待任务。延迟(30000);
//为什么需要号召参加某项活动?
var success=await context.CallActivityAsync(CreateDBTaskConstant,Name);
返回true;
}
[函数名(CreateDBTaskConstant)]
公共静态异步任务CreateDBTask([ActivityTrigger]字符串名称)
{
//预制件工作
等待任务。延迟(30000);
返回true;
}
}

不鼓励在orchestrator代码中启动异步操作,唯一的例外是由持久业务流程上下文启动的代码,例如
CallActivityAsync()
。这是由于orchestrator函数的检查点重播行为造成的

有关摘录自

除非使用DurableOrchestrationContext API,否则Orchestrator代码不得启动任何异步操作。例如,没有Task.Run、Task.Delay或HttpClient.SendAsync。持久任务框架在单个线程上执行orchestrator代码,并且不能与其他异步API可以调度的任何其他线程交互

可以在orchestrator函数中安全等待的任务有时称为持久任务。这些任务由持久任务框架创建和管理。例如CallActivityAsync、WaitForExternalEvent和CreateTimer返回的任务


为了确认这是调用上述代码的正确方法?我基本上需要3种方法来执行一个可以立即返回挂起结果的异步任务?是的,您需要orchestrator客户端(已触发)、orchestrator函数和实际执行工作的活动。尽管如此,我还是不明白为什么要使用编排器来执行不严格要求编排的逻辑。出于您的目的,仅仅有一个异步启动数据库的触发函数难道还不够吗?但是我正在从asp.net端点开始创建数据库。在创建数据库时,我无法可靠地运行后台任务或让线程等待。db可能需要几分钟才能创建,有意义吗?在我看来,我的目标是启动一个函数,并让它返回一个端点,在那里我可以检查进度。我唯一能做到这一点的方法是使用上面的持久函数流,我想这是肯定的,在这种情况下,编曲器是有意义的。请注意,由于该过程可能需要几分钟,因此如果您在消费计划中运行函数,则默认的最大执行超时为5分钟(可以扩展为10分钟),之后将终止活动函数。