项目作者: NickDarvey

项目描述 :
Extensions for Service Fabric's actor reminders
高级语言: C#
项目地址: git://github.com/NickDarvey/Prompter.git
创建时间: 2017-04-24T11:52:22Z
项目社区:https://github.com/NickDarvey/Prompter

开源协议:MIT License

下载


Prompter

A lil’ helper library for Service Fabric actor’s reminders.

It adds two things we found ourselves implementing over and over again:

  • Safe unregistering of reminders

    No more try/catch blocks! Huzzah!

  • Single-use reminders

    You’re meant to unregister reminders, even if you’re using the fire-once approach (dueTime: TimeSpan.FromMilliseconds(-1)).
    This makes that process automatic with PromptOnce.
    This is currently listed as a functional bug, but this will get you around it.

  • Correlation IDs

    We use found it super useful to have an ID we could track across all of our services. This let’s you track a GUID across reminders.

Usage

  1. internal sealed class OrderActor : PromptableActor, IOrderActor
  2. {
  3. public async Task PlaceOrder(OrderRequest req)
  4. {
  5. // Some internal logic, whatever
  6. var token = await SetOrderState(req);
  7. // Register a prompt to come back in seven days
  8. await PromptOnce(token, TimeSpan.FromDays(7));
  9. }
  10. protected override async Task OnPrompt(string name, byte[] context, TimeSpan due, TimeSpan period, Guid cid)
  11. {
  12. var token = name;
  13. var order = await GetOrderState(name);
  14. await SendEmailReminder(order);
  15. }
  16. }

Setup

Install it from NuGet (Install-Package Prompter) and use one of the two options for integration.

A. Inheritance

  1. internal sealed class MyActor : PromptableActor, IMyActor { }

B. Pretending you have traits

Setup a callback:

  1. private Task OnPrompt(string name, byte[] context, TimeSpan due, TimeSpan period, Guid cid)
  2. {
  3. // Do stuff
  4. }

then add it to your actor’s constructor:

  1. private readonly Prompt _prompt;
  2. public ActorFixture(ActorService svc, ActorId id)
  3. : base(svc, id)
  4. {
  5. _prompt = new Prompt(
  6. OnPrompt,
  7. GetReminder,
  8. RegisterReminderAsync,
  9. UnregisterReminderAsync,
  10. this);
  11. }

and link the reminder callback to Prompter:

  1. public Task ReceiveReminderAsync(string reminderName, byte[] state, TimeSpan dueTime, TimeSpan period) =>
  2. _prompt.ReceivePrompt(reminderName, state, dueTime, period);

It’s awful, isn’t it?
I couldn’t figure out a nicer way layer it on but I’m open to suggestions.
(We have a bunch of these ‘traits’ we wanted to use and didn’t like the idea of bundling them all into one base class.)

Logging

You should be logging everything like crazy, so I chucked in logs here too.
That’s why you (should) pass in the current actor when constructing Prompt.
(I don’t do anything else with it, I swear!)

The provider is Prompter-Prompt and it logs registers, unregisters and receives.