Example #1
0
        private Task RegisterOrUpdateReminderAsync(ActorId actorId, IActorReminderState reminderState,
                                                   bool saveState = true)
        {
            var reminder = new ActorReminder(actorId, this, reminderState);

            return(this.RegisterOrUpdateReminderAsync(reminder, reminderState.RemainingDueTime, saveState));
        }
Example #2
0
        private async Task RegisterOrUpdateReminderAsync(ActorReminder actorReminder, TimeSpan remainingDueTime,
                                                         bool saveState = true)
        {
            this.ThrowIfClosed();

            ActorTrace.Source.WriteInfoWithId(
                TraceType,
                this.traceId,
                "Registering reminder for actor: ({0}), reminderName: ({1}), remainingDueTime: ({2}),  saveState {3}",
                actorReminder.OwnerActorId,
                actorReminder.Name,
                remainingDueTime,
                saveState);

            var reminderDictionary = this.remindersByActorId.GetOrAdd(actorReminder.OwnerActorId,
                                                                      k => new ConcurrentDictionary <string, ActorReminder>());

            reminderDictionary.AddOrUpdate(
                actorReminder.Name,
                actorReminder,
                (k, v) =>
            {
                v.CancelTimer();
                return(actorReminder);
            });

            try
            {
                if (saveState)
                {
                    await this.StateProvider.SaveReminderAsync(actorReminder.OwnerActorId, actorReminder);
                }

                actorReminder.ArmTimer(remainingDueTime);
            }
            catch (Exception ex)
            {
                actorReminder.CancelTimer();
                reminderDictionary.TryRemove(actorReminder.Name, out actorReminder);

                if (!(ex is FabricNotPrimaryException))
                {
                    ActorTrace.Source.WriteWarningWithId(
                        TraceType,
                        this.traceId,
                        "Failed to register reminder for actor {0}, reminderName {1}, saveState {2}. Error={3}.",
                        actorReminder.OwnerActorId,
                        actorReminder.Name,
                        saveState,
                        ex.ToString());
                }

                throw;
            }
        }
Example #3
0
        public async Task <IActorReminder> RegisterOrUpdateReminderAsync(
            ActorId actorId,
            string reminderName,
            byte[] state,
            TimeSpan dueTime,
            TimeSpan period,
            bool saveState = true)
        {
            var reminder = new ActorReminder(actorId, this, reminderName, state, dueTime, period);

            await this.RegisterOrUpdateReminderAsync(reminder, dueTime, saveState);

            return(reminder);
        }
Example #4
0
 private async Task UpdateReminderLastCompletedTimeAsync(ActorReminder reminder)
 {
     try
     {
         await this.StateProvider.ReminderCallbackCompletedAsync(reminder.OwnerActorId, reminder, CancellationToken.None);
     }
     catch (Exception ex)
     {
         ActorTrace.Source.WriteWarningWithId(
             TraceType,
             this.traceId,
             "Failed to update last completed time for ActorId: ({0}), ReminderName: ({1}). Exception: {2}",
             reminder.OwnerActorId,
             reminder.Name,
             ex.ToString());
     }
 }
Example #5
0
 private async Task UnregisterOneTimeReminderAsync(ActorReminder reminder)
 {
     try
     {
         await this.UnregisterReminderAsync(reminder.Name, reminder.OwnerActorId, true);
     }
     catch (ReminderNotFoundException)
     {
         // User already unregistered the reminder.
     }
     catch (Exception ex)
     {
         ActorTrace.Source.WriteWarningWithId(
             TraceType,
             this.traceId,
             "UnregisterOneTimeReminderAsync(): Failed to unregister reminder for ActorId:[{0}], ReminderName:[{1}]. Exception: {2}",
             reminder.OwnerActorId,
             reminder.Name,
             ex.ToString());
     }
 }
Example #6
0
        public async Task <IActorReminder> RegisterOrUpdateReminderAsync(
            ActorId actorId,
            string reminderName,
            byte[] state,
            TimeSpan dueTime,
            TimeSpan period,
            bool saveState = true)
        {
            var reminderDictionary = this.remindersByActorId.GetOrAdd(
                actorId,
                k => new ConcurrentDictionary <string, ActorReminder>());

            var reminder = new ActorReminder(actorId, this, reminderName, state, dueTime, period);


            reminderDictionary.AddOrUpdate(
                reminderName,
                reminder,
                (k, v) =>
            {
                v.CancelTimer();
                return(reminder);
            });
            try
            {
                if (saveState)
                {
                    await this.StateProvider.SaveReminderAsync(actorId, reminder);
                }
            }
            catch
            {
                reminder.CancelTimer();
                reminderDictionary.TryRemove(reminderName, out reminder);

                throw;
            }

            return(reminder);
        }
Example #7
0
        public async void FireReminder(ActorReminder reminder)
        {
            var rearmTimer = true;

            try
            {
                using (var actorScope = this.GetActor(reminder.OwnerActorId, true, false))
                {
                    var actorBase = actorScope.Actor;

                    // if Actor is deleted, reminder should not be fired or armed again.
                    // Its an optimization so that we don't fire the reminder if the actor
                    // is marked for deletion.
                    if (actorBase.MarkedForDeletion)
                    {
                        rearmTimer = false;
                        return;
                    }

                    if (this.actorService.ActorTypeInformation.IsRemindable)
                    {
                        var actor = (IRemindable)actorBase;

                        await this.DispatchToActorAsync(
                            reminder.OwnerActorId,
                            this.reminderMethodContext,
                            false,
                            (async(a, cancellationTkn) =>
                        {
                            await actor.ReceiveReminderAsync(reminder.Name, reminder.State, reminder.DueTime, reminder.Period);

                            return(null);
                        }),
                            Guid.NewGuid().ToString(),
                            false,
                            CancellationToken.None);
                    }
                }
            }
            catch (ActorDeletedException)
            {
                rearmTimer = false;
            }
            catch (Exception e)
            {
                ActorTrace.Source.WriteWarningWithId(
                    TraceType,
                    this.traceId,
                    "Firing reminder {0} for actor {1} caused exception: {2}",
                    reminder.Name,
                    reminder.OwnerActorId,
                    e.ToString());
            }

            // User may delete or update reminder during ReceiveReminderAsync() call.
            // Rearm only if it is still valid.
            if (reminder.IsValid() && rearmTimer)
            {
                if (this.ActorService.Settings.ReminderSettings.AutoDeleteOneTimeReminders && this.IsOneTimeReminder(reminder))
                {
                    await this.UnregisterOneTimeReminderAsync(reminder);
                }
                else
                {
                    await this.UpdateReminderLastCompletedTimeAsync(reminder);

                    reminder.ArmTimer(reminder.Period);
                }
            }
        }
Example #8
0
 public void FireReminder(ActorReminder reminder)
 {
     // no-op.
     // Reminders don't fire in mock version.
 }