/// <summary> /// Get's the given <see cref="ScheduledMessageInfo"/> and adds it to internal list of scheduled messages. /// </summary> /// <param name="msg">Scheduled message description to be added to the internal list of scheduled messages.</param> private void ScheduleMessage(ScheduledMessageInfo msg) { lock (scheduledMessagesLock) { ScheduledMessages.Add(msg); } }
/// <summary> /// Schedule periodic <paramref name="message"/> to be sent to <paramref name="recipient"/> each <paramref name="period"/>. /// Creates the <see cref="ScheduledMessageInfo"/> instance based on the parameters and let it add to the internal list of the scheduled messages /// </summary> /// <param name="sender">Optional information about the sender, usually used by <paramref name="recipient"/> to respond to the <paramref name="message"/></param> /// <param name="recipient">Recipient of the message</param> /// <param name="period">Period after which the message is enqueued to the <paramref name="recipient"/> queue</param> /// <param name="message">Message to be periodically sent to the <paramref name="recipient"/></param> /// <returns>Unique ID of scheduled message</returns> internal string ScheduleMessage(IActorRef sender, IActorRef recipient, TimeSpan period, object message) { var msg = new ScheduledMessageInfo(sender, recipient, period, message); ScheduleMessage(msg); return(msg.Id); }
/// <summary> /// Schedule one-time <paramref name="message"/> to be sent to <paramref name="recipient"/> at given <paramref name="nextFire">date and time</paramref> /// Creates the <see cref="ScheduledMessageInfo"/> instance based on the parameters and let it add to the internal list of the scheduled messages /// </summary> /// <param name="sender">Optional information about the sender, usually used by <paramref name="recipient"/> to respond to the <paramref name="message"/></param> /// <param name="recipient">Recipient of the message</param> /// <param name="nextFire">Date and time after which the message is enqueued to the <paramref name="recipient"/> queue</param> /// <param name="message">Message to be sent to the <paramref name="recipient"/></param> /// <returns>Unique ID of scheduled message</returns> internal string ScheduleMessage(IActorRef sender, IActorRef recipient, DateTime nextFire, object message) { var msg = new ScheduledMessageInfo(sender, recipient, nextFire, message); ScheduleMessage(msg); return(msg.Id); }
/// <summary> /// CTOR for the new instance of scheduled message definition for existing periodic scheduled message /// Used when creating the definition of each new run of the periodic schedule. Copies the information from current definition and preservers the ID /// </summary> /// <param name="existingPeriodicScheduledMessageInfo"></param> /// <returns>Scheduled message definition instance while preserving the ID of scheduled message</returns> internal ScheduledMessageInfo(ScheduledMessageInfo existingPeriodicScheduledMessageInfo) { if (existingPeriodicScheduledMessageInfo == null) { throw new ArgumentNullException(nameof(existingPeriodicScheduledMessageInfo)); } Id = existingPeriodicScheduledMessageInfo.Id; Sender = existingPeriodicScheduledMessageInfo.Sender; Recipient = existingPeriodicScheduledMessageInfo.Recipient; Period = existingPeriodicScheduledMessageInfo.Period ?? throw new ArgumentOutOfRangeException(nameof(Period)); Message = existingPeriodicScheduledMessageInfo.Message; NextFire = DateTime.Now + Period.Value; }
/// <summary> /// Gets the list of scheduled messages that should be enqueued (are due) /// When the scheduled message is periodic, the next fire time is calculated and a new instance of scheduled message is added to to schedule /// </summary> /// <returns>The list of scheduled messages that to be enqueued</returns> private IEnumerable <ScheduledMessageInfo> GetScheduledMessagesDue() { List <ScheduledMessageInfo> retVal; var refDateTime = DateTime.Now; lock (scheduledMessagesLock) { retVal = ScheduledMessages.FindAll(i => i.NextFire <= refDateTime); foreach (var scheduledMessage in retVal) { ScheduledMessages.Remove(scheduledMessage); if (scheduledMessage.Period == null) { continue; } var msg = new ScheduledMessageInfo(scheduledMessage); ScheduledMessages.Add(msg); } } return(retVal); }