Пример #1
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;
            }
        }
Пример #2
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);
        }