有没有办法在Azure持久功能的持久实体中安排“提醒”?

问题描述

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

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

考虑为我系统中的每个设备创建的持久实体。我希望每个演员都按计划运行功能,并自行计划。 (我想避免具有需要为每个设备安排任务的编排功能,而只是让实体自己运行任务的实体。)

这是允许,可能或预见的事情吗?

希望这个问题很清楚,但很高兴在需要时提供更多背景信息。

解决方法

来自the docs

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

但是,您想从“持久实体”中进行调度。通过使用Entity.Current.SignalEntitydocs)也可以做到这一点。此方法作为重载,接受一个DateTime,该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来由实体本身安排工作。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...