/// <summary>
        /// Create reminder
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task <int> CreateReminder(Reminder model)
        {
            try
            {
                await _context.Reminders.AddAsync(model);

                await _context.SaveChangesAsync();

                return(OK);
            }
            catch
            {
                return(SERVER_ERROR);
            }
        }
        public async Task ExecuteAsync(CancellationToken token)
        {
            DiscordChannel channel = Bot.Discord.Guilds[GuildId].GetChannel(ChannelId);

            DiscordEmbedBuilder builder = new DiscordEmbedBuilder()
                                          .WithAuthor("Reminder Hat")
                                          .WithTitle(Name)
                                          .WithDescription(Contents)
                                          .WithColor(DiscordColor.HotPink)
                                          .WithImageUrl("https://www.flaticon.com/svg/static/icons/svg/1792/1792931.svg");

            await channel.SendMessageAsync(embed : builder.Build());

            try
            {
                using (DevryDbContext database = new DevryDbContext())
                {
                    this.NextRunTime           = CrontabSchedule.Parse(Schedule).GetNextOccurrence(DateTime.Now);
                    database.Entry(this).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                    await database.SaveChangesAsync();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Unable to update Reminder: {Id} | {Name} | {Schedule}\n\t{ex.Message}");
            }
        }
Exemple #3
0
        private async Task ExecuteOnceAsync(CancellationToken token)
        {
            var taskFactory = new TaskFactory(TaskScheduler.Current);

            // Cached since this will be referenced quite frequently in this method
            var referenceTime = DateTime.Now;

            // Determine which tasks will never run again and remove them from the database
            var removeList = _scheduledTasks.Where(x => x.Value.WillNeverRunAgain).ToList();

            // We must ensure that our database is updated -- to remove tasks that will never run again
            using (DevryDbContext context = new DevryDbContext())
            {
                foreach (var pair in removeList)
                {
                    Models.Reminder reminder = await context.Reminders.FindAsync(pair.Key);

                    if (reminder == null)
                    {
                        _logger.LogWarning($"Unable to locate reminder with Id: {pair.Key}");
                        continue;
                    }
                    else
                    {
                        _logger.LogInformation($"Reminder: '{reminder.Name}' with Id '{reminder.Id}' is being cleaned up. --Determined to never run again");
                        context.Reminders.Remove(reminder);
                        _scheduledTasks.Remove(pair.Key);
                    }
                }

                // Save changes (if applicable)
                await context.SaveChangesAsync();
            }

            var tasksThatShouldRun = _scheduledTasks.Values.Where(x => x.ShouldRun(referenceTime));

            foreach (var task in tasksThatShouldRun)
            {
                task.Increment();

                await taskFactory.StartNew(
                    async() =>
                {
                    try
                    {
                        await task.Task.ExecuteAsync(token);
                    }
                    catch (Exception ex)
                    {
                        var args = new UnobservedTaskExceptionEventArgs(ex as AggregateException ?? new AggregateException(ex));
                        UnobservedException?.Invoke(this, args);

                        if (!args.Observed)
                        {
                            throw;
                        }
                    }
                },
                    token);
            }
        }