简体   繁体   English

如何使用“演员提醒”机制在服务结构中安排任务?

[英]How to schedule a task in service fabric using 'Ac­tor Remin­der' mechanism?

I want to generate invoices on daily, weekly and monthly basis and want to send invoices periodically.我想每天、每周和每月生成发票,并希望定期发送发票。 Each customer have different invoice settings.每个客户都有不同的发票设置。

Sometime ago I wondered how to do that myself.前一段时间我想知道如何自己做到这一点。 Based on the excellent documentation I came up with a sample app, see my repo .基于优秀的文档,我想出了一个示例应用程序,请参阅我的repo

Your actor should implement the IRemindable interface.您的演员应该实现IRemindable接口。 To create a reminder use this in an actor method:要创建提醒,请在 actor 方法中使用它:

await RegisterReminderAsync(
            "MyReminder", // name of the reminder
            Encoding.ASCII.GetBytes(message), // byte array with payload (message is a string in my case)
            dueTime, // When is the reminder first activated
            snoozeTime); // Interval between activations

In your case set the snoozeTime to a day or to a week to have the reminder activate every period.在您的情况下,将 snoozeTime 设置为一天或一周,以便在每个时期激活提醒。

When the due time is there the method ReceiveReminderAsync is called:当到期时间ReceiveReminderAsync时,方法ReceiveReminderAsync被调用:

public Task ReceiveReminderAsync(string reminderName, byte[] state, TimeSpan dueTime, TimeSpan period)
{
    ActorEventSource.Current.Message($"Actor recieved reminder {reminderName}.");

    ...
}

You can tell by the value of ReceiveReminderAsync which reminder it is about and you can use the content of state to act on the payload.您可以通过ReceiveReminderAsync的值来判断它是关于哪个提醒,并且您可以使用state的内容对有效负载进行操作。

To dismiss a reminder use the UnregisterReminderAsync method:要关闭提醒,请使用UnregisterReminderAsync方法:

await UnregisterReminderAsync(GetReminder("MyReminder"));  
internal class InvoiceGenerationActor : Actor, IInvoiceGenerationActor, IRemindable
{
    protected override async Task OnActivateAsync()
    {
        ////ActorEventSource.Current.ActorMessage(this, "Actor activated.");

        //// The StateManager is this actor's private state store.
        //// Data stored in the StateManager will be replicated for high-availability for actors that use volatile or persisted state storage.
        //// Any serializable object can be saved in the StateManager.
        //// For more information, see https://aka.ms/servicefabricactorsstateserialization

        //// return this.StateManager.TryAddStateAsync("count", 0);

        ////var schedulerDtos = GetSchedulerList();

        await base.OnActivateAsync();

        ActorEventSource.Current.ActorMessage(this, "Actor activated.");

        IActorReminder generationReminderRegistration = await this.RegisterReminderAsync(GenerationRemainder, BitConverter.GetBytes(100), TimeSpan.FromMilliseconds(0), TimeSpan.FromMinutes(10));

        ////IActorReminder mailReminderRegistration = await this.RegisterReminderAsync(generationRemainder, BitConverter.GetBytes(100), TimeSpan.FromMinutes(1), TimeSpan.FromHours(1));

        return;
    }

    public async Task ReceiveReminderAsync(string reminderName, byte[] context, TimeSpan dueTime, TimeSpan period)
    {
        if (reminderName.Equals(GenerationRemainder))
        {
            await GetAllInvoiceSettingAndRegisterNewRemiders();
        }
        else
        {
            int solutionPartnerId = BitConverter.ToInt32(context, 0);
            var data = await Get("SolutionOwnerInvoiceGeneration/AutomaticInvoiceGeneration/" + solutionPartnerId, "PartnerService");
            await UnregisterReminderAsync(GetReminder(reminderName));
        }
    }
}

For scheduled tasks, you can use an external mechanism like hangfire .对于计划任务,您可以使用诸如hangfire 之类的外部机制。

It does scheduled tasks apart from the web app and you can track tasks from its task dashboard.除了 Web 应用程序之外,它还执行计划任务,您可以从其任务仪表板跟踪任务。

You can register a job like:您可以注册以下职位:

RecurringJob.AddOrUpdate(() => SoccerDataFetcher_UpdatePlayersOfTeams(), Cron.Daily);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM