Esempio n. 1
0
        /// <summary>
        /// Removes a timer from the EventTimer database by its <paramref name="TimerTracker"/> token.
        /// </summary>
        /// <remarks>This function removes the timer given by its <paramref name="TimerTracker"/> from the database completely.</remarks>
        /// <param name="TimerTracker">The unique identifier of the target EventTimer.</param>

        public void RemoveTimer(string TimerTracker)
        {
            EventTimer Timer = EventTimersDB.EventTimers.Find(TimerTracker);

            if (Timer != null)
            {
                EventTimersDB.EventTimers.Remove(Timer);
            }

            EventTimersDB.SaveChanges();
        }
Esempio n. 2
0
        /// <summary>
        /// Loops through all events and checks if any of them are set to be triggered once (Expire) or periodically (Interval).
        /// </summary>
        /// <returns>A <c>Task</c> object, which can be awaited until this method completes successfully.</returns>

        public async Task LoopThroughEvents()
        {
            long CurrentTime = DateTimeOffset.UtcNow.ToUnixTimeSeconds();

            await EventTimersDB.EventTimers.AsQueryable().Where(Timer => Timer.ExpirationTime <= CurrentTime)
            .ForEachAsync(async(EventTimer Timer) => {
                switch (Timer.TimerType)
                {
                case TimerType.Expire:
                    EventTimersDB.EventTimers.Remove(Timer);
                    break;

                case TimerType.Interval:
                    Timer.ExpirationTime = Timer.ExpirationLength + DateTimeOffset.UtcNow.ToUnixTimeSeconds();
                    break;
                }

                EventTimersDB.SaveChanges();

                Dictionary <string, string> Parameters = JsonConvert.DeserializeObject <Dictionary <string, string> >(Timer.CallbackParameters);
                Type Class = Assembly.GetExecutingAssembly().GetTypes().Where(Type => Type.Name.Equals(Timer.CallbackClass)).FirstOrDefault();

                if (Class != null)
                {
                    if (Class.GetMethod(Timer.CallbackMethod) == null)
                    {
                        throw new NoNullAllowedException("The callback method specified for the admin confirmation is null! This could very well be due to the method being private.");
                    }

                    try {
                        await(Task) Class.GetMethod(Timer.CallbackMethod)
                        .Invoke(ServiceProvider.GetRequiredService(Class), new object[1] {
                            Parameters
                        });
                    } catch (Exception Exception) {
                        await LoggingService.LogMessageAsync(
                            new LogMessage(LogSeverity.Error, GetType().Name, Exception.Message, exception: Exception)
                            );
                    }
                }
                else if (Timer.TimerType == TimerType.Interval)
                {
                    EventTimersDB.EventTimers.Remove(Timer);
                }
            });
        }
Esempio n. 3
0
        /// <summary>
        /// Creates a new timer object and adds it to the corresponding database.
        /// </summary>
        /// <param name="JSON">JSON-formatted string-string Dictionary codifying the parameters to pass to <paramref name="ClassName"/>.<paramref name="MethodName"/>()</param>
        /// <param name="ClassName">The name of the class containing <paramref name="MethodName"/>.</param>
        /// <param name="MethodName">The name of the method to call when the event is triggered.</param>
        /// <param name="SecondsTillExpiration">Time until the event expires (Expire) or triggers (Interval).</param>
        /// <param name="TimerType">Whether the timer is a single-trigger timer (Expire) or triggers every set number of <paramref name="SecondsTillExpiration"/> (Interval).</param>
        /// <returns>A <c>Task</c> object, which can be awaited until this method completes successfully.</returns>

        public async Task <string> AddTimer(string JSON, string ClassName, string MethodName, int SecondsTillExpiration, TimerType TimerType)
        {
            string Token = CreateToken();

            EventTimer Timer = new () {
                Token              = Token,
                ExpirationLength   = SecondsTillExpiration,
                CallbackClass      = ClassName,
                CallbackMethod     = MethodName,
                CallbackParameters = JSON,
                ExpirationTime     = DateTimeOffset.UtcNow.ToUnixTimeSeconds() + (TimerType != TimerType.Interval ? SecondsTillExpiration : 0),
                TimerType          = TimerType
            };

            EventTimersDB.EventTimers.Add(Timer);

            await EventTimersDB.SaveChangesAsync();

            return(Timer.Token);
        }