有没有办法在 Azure 持久函数的持久实体中计划"Reminders"?



我在一个研究项目中使用Azure耐用功能,我想验证在耐用实体中实现计时器/提醒器概念的最佳方法。

在Service Fabric中,Actor可以调度一个"持久计时器",以便框架在Actor本身上调用一个方法。我正在寻找一个类似的概念。

考虑为我的系统中的每个设备创建的耐用实体。我希望每个参与者都按计划运行功能,并自行安排。(我想避免使用需要为每个设备安排任务的编排功能,而只让实体自己运行任务(。

这是允许的、可能的还是可以预见的?

希望这个问题足够清楚,但很乐意在需要的地方提供更多的背景。

来自文档:

要调用实体上的操作,请指定[….]计划时间,这是一个用于指定操作交付时间的可选参数。例如,一个操作可以可靠地安排在未来几天内运行。因此,可以选择调度实体的调用

但是,您希望从耐用实体中进行调度。这也可以通过使用Entity.Current.SignalEntity(文档(来实现。此方法是一个重载,它接受指定开始操作的时间的DateTime。

下面的代码将使用http触发的函数开始递增计数器。在azure函数初始启动后,实体将为自己每5秒安排一次操作。

using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Azure.WebJobs.Extensions.DurableTask;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
namespace FunctionApp3
{
public static class Function1
{
[FunctionName("Function1")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
[DurableClient] IDurableEntityClient client,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
// Tell the Durable Entity to increase a counter
await client.SignalEntityAsync<ICounter>("aKey", e => e.IncreaseCount(1));
return new OkResult();
}
}
public interface ICounter
{
void IncreaseCount(int amount);
}
[JsonObject(MemberSerialization.OptIn)]
public class Counter : ICounter
{
private readonly ILogger _log;
public Counter(ILogger<Counter> log)
{
_log = log;
}
[JsonProperty("value")]
public int CurrentValue { get; set; }
public void IncreaseCount(int amount)
{
this.CurrentValue += amount;
if(this.CurrentValue) > 10
return;
// Schedule a call to this entity to IncreaseCount after 5 seconds. 
// Once the method is called, schedule it again to create the effect of a timer
Entity.Current.SignalEntity<ICounter>(Entity.Current.EntityId, DateTime.Now.AddSeconds(5), e => e.IncreaseCount(1));
_log.LogInformation(this.CurrentValue.ToString());
}
[FunctionName(nameof(Counter))]
public static Task Run([EntityTrigger] IDurableEntityContext ctx)
{
return ctx.DispatchAsync<Counter>();
}
}
}

这只是一个粗略的示例,但要点是使用Entity.Current.SignalEntity来安排实体本身的工作。

最新更新