Exemple #1
0
        /// <summary>
        /// Registers a Timer for the actor. A timer name is autogenerated by the runtime to keep track of it.
        /// </summary>
        /// <param name="timerName">Timer Name. If a timer name is not provided, a timer is autogenerated.</param>
        /// <param name="callback">
        /// The name of the method to be called when the timer fires.
        /// It has one parameter: the state object passed to RegisterTimer.
        /// It returns a <see cref="System.Threading.Tasks.Task"/> representing the asynchronous operation.
        /// </param>
        /// <param name="callbackParams">An object containing information to be used by the callback method, or null.</param>
        /// <param name="dueTime">The amount of time to delay before the async callback is first invoked.
        /// Specify negative one (-1) milliseconds to prevent the timer from starting.
        /// Specify zero (0) to start the timer immediately.
        /// </param>
        /// <param name="period">
        /// The time interval between invocations of the async callback.
        /// Specify negative one (-1) milliseconds to disable periodic signaling.</param>
        /// <returns>Returns IActorTimer object.</returns>
        public async Task <ActorTimer> RegisterTimerAsync(
            string timerName,
            string callback,
            byte[] callbackParams,
            TimeSpan dueTime,
            TimeSpan period)
        {
            EnsureInteractorInitialized();

            // Validate that the timer callback specified meets all the required criteria for a valid callback method
            this.ValidateTimerCallback(this.Host, callback);

            // create a timer name to register with Dapr runtime.
            if (string.IsNullOrEmpty(timerName))
            {
                timerName = $"{this.Id}_Timer_{Guid.NewGuid()}";
            }

            var timerInfo       = new TimerInfo(callback, callbackParams, dueTime, period);
            var actorTimer      = new ActorTimer(timerName, timerInfo);
            var serializedTimer = JsonSerializer.Serialize <TimerInfo>(timerInfo);

            await this.Host.DaprInteractor.RegisterTimerAsync(this.actorTypeName, this.Id.ToString(), timerName, serializedTimer);

            return(actorTimer);
        }
        public override async Task RegisterTimerAsync(ActorTimer timer)
        {
            if (timer == null)
            {
                throw new ArgumentNullException(nameof(timer));
            }

            #pragma warning disable 0618
            var timerInfo = new TimerInfo(timer.TimerCallback, timer.Data, timer.DueTime, timer.Period);
            #pragma warning restore 0618
            var data = JsonSerializer.Serialize(timerInfo);
            await this.interactor.RegisterTimerAsync(timer.ActorType, timer.ActorId.ToString(), timer.Name, data);
        }
Exemple #3
0
        /// <summary>
        /// Registers a Timer for the actor. If a timer name is not provided, a timer is autogenerated.
        /// </summary>
        /// <param name="timerName">Timer Name. If a timer name is not provided, a timer is autogenerated.</param>
        /// <param name="asyncCallback">
        /// A delegate that specifies a method to be called when the timer fires.
        /// It has one parameter: the state object passed to RegisterTimer.
        /// It returns a <see cref="System.Threading.Tasks.Task"/> representing the asynchronous operation.
        /// </param>
        /// <param name="state">An object containing information to be used by the callback method, or null.</param>
        /// <param name="dueTime">The amount of time to delay before the async callback is first invoked.
        /// Specify negative one (-1) milliseconds to prevent the timer from starting.
        /// Specify zero (0) to start the timer immediately.
        /// </param>
        /// <param name="period">
        /// The time interval between invocations of the async callback.
        /// Specify negative one (-1) milliseconds to disable periodic signaling.</param>
        /// <returns>Returns IActorTimer object.</returns>
        protected async Task <IActorTimer> RegisterTimerAsync(
            string timerName,
            Func <object, Task> asyncCallback,
            object state,
            TimeSpan dueTime,
            TimeSpan period)
        {
            // create a timer name to register with Dapr runtime.
            if (string.IsNullOrEmpty(timerName))
            {
                timerName = $"{this.Id}_Timer_{this.timers.Count + 1}";
            }

            var actorTimer = new ActorTimer(this, timerName, asyncCallback, state, dueTime, period);
            await ActorRuntime.DaprInteractor.RegisterTimerAsync(this.actorImplementaionTypeName, this.Id.ToString(), timerName, actorTimer.SerializeToJson());

            this.timers[timerName] = actorTimer;
            return(actorTimer);
        }
Exemple #4
0
        /// <summary>
        /// Registers a Timer for the actor. A timer name is autogenerated by the runtime to keep track of it.
        /// </summary>
        /// <param name="timerName">Timer Name. If a timer name is not provided, a timer is autogenerated.</param>
        /// <param name="callback">
        /// The name of the method to be called when the timer fires.
        /// It has one parameter: the state object passed to RegisterTimer.
        /// It returns a <see cref="System.Threading.Tasks.Task"/> representing the asynchronous operation.
        /// </param>
        /// <param name="callbackParams">An object containing information to be used by the callback method, or null.</param>
        /// <param name="dueTime">The amount of time to delay before the async callback is first invoked.
        /// Specify negative one (-1) milliseconds to prevent the timer from starting.
        /// Specify zero (0) to start the timer immediately.
        /// </param>
        /// <param name="period">
        /// The time interval between invocations of the async callback.
        /// Specify negative one (-1) milliseconds to disable periodic signaling.</param>
        /// <returns>Returns IActorTimer object.</returns>
        public async Task <ActorTimer> RegisterTimerAsync(
            string timerName,
            string callback,
            byte[] callbackParams,
            TimeSpan dueTime,
            TimeSpan period)
        {
            // Validate that the timer callback specified meets all the required criteria for a valid callback method
            this.ValidateTimerCallback(this.Host, callback);

            // create a timer name to register with Dapr runtime.
            if (string.IsNullOrEmpty(timerName))
            {
                timerName = $"{this.Id}_Timer_{Guid.NewGuid()}";
            }

            var actorTimer = new ActorTimer(this.actorTypeName, this.Id, timerName, callback, callbackParams, dueTime, period);

            await this.Host.TimerManager.RegisterTimerAsync(actorTimer);

            return(actorTimer);
        }
Exemple #5
0
 /// <summary>
 /// Unregisters a Timer previously set on this actor.
 /// </summary>
 /// <param name="timer">An IActorTimer representing timer that needs to be unregistered.</param>
 /// <returns>Task representing the Unregister timer operation.</returns>
 protected async Task UnregisterTimerAsync(ActorTimer timer)
 {
     await ActorRuntime.DaprInteractor.UnregisterTimerAsync(this.actorTypeName, this.Id.ToString(), timer.Name);
 }
 public override Task RegisterTimerAsync(ActorTimer timer)
 {
     throw new NotImplementedException(Message);
 }
Exemple #7
0
 /// <summary>
 /// Unregisters a Timer previously set on this actor.
 /// </summary>
 /// <param name="timer">An IActorTimer representing timer that needs to be unregistered.</param>
 /// <returns>Task representing the Unregister timer operation.</returns>
 protected async Task UnregisterTimerAsync(ActorTimer timer)
 {
     await this.Host.TimerManager.UnregisterTimerAsync(timer);
 }
Exemple #8
0
 /// <summary>
 /// Registers the provided timer with the runtime.
 /// </summary>
 /// <param name="timer">The <see cref="ActorTimer" /> to register.</param>
 /// <returns>A task which will complete when the operation completes.</returns>
 public abstract Task RegisterTimerAsync(ActorTimer timer);
Exemple #9
0
 /// <summary>
 /// Unregisters a Timer previously set on this actor.
 /// </summary>
 /// <param name="timer">An IActorTimer representing timer that needs to be unregistered.</param>
 /// <returns>Task representing the Unregister timer operation.</returns>
 protected async Task UnregisterTimerAsync(ActorTimer timer)
 {
     EnsureInteractorInitialized();
     await this.Host.DaprInteractor.UnregisterTimerAsync(this.actorTypeName, this.Id.ToString(), timer.Name);
 }