Beispiel #1
0
 /// <summary>
 ///     Helper method to start or update the recurring job.
 /// </summary>
 /// <param name="reminder"></param>
 private void StartOrUpdateJob(RecurringReminderMessage reminder)
 {
     using (var scope = _scopeFactory.CreateScope())
     {
         var reminderJob = scope.ServiceProvider.GetRequiredService <RecurringReminderMessageJob>();
         try
         {
             _jobService.ScheduleRecurringJob(() => reminderJob.Run(reminder),
                                              reminder.CronExpression,
                                              reminder.GetJobId());
         }
         catch (ArgumentException)
         {
             var reminderService = scope.ServiceProvider.GetRequiredService <IEntityService <RecurringReminderMessage> >();
             reminder.Fault = RecurringReminderMessage.FaultType.InvalidCron;
             reminderService.Update(reminder);
         }
     }
 }
Beispiel #2
0
        /// <summary>
        ///     Background job execution for reminder messages for the Remembot module.
        ///     ** NOTE - DO NOT make changes to this signature. If changes are needed, make a new method to use.
        ///        Changing this file's signature will cause errors with the existing job queues.
        /// </summary>
        /// <param name="reminder"></param>
        /// <returns></returns>
        public async Task Run(RecurringReminderMessage reminder)
        {
            using (var scope = _scopeFactory.CreateScope())
            {
                var channel = _bot.Client.GetChannel(reminder.ChannelId);
                if (channel is null)
                {
                    _logger.LogWarning("Attempt was made to send reminder to channel {ChannelId} that doesn't exist. Removing reminder and stopping job for guild {GuildId}.",
                                       reminder.ChannelId,
                                       reminder.GuildId);

                    await DisableJob();

                    return;
                }

                if (channel is SocketTextChannel stc)
                {
                    await stc.SendMessageAsync(reminder.Message);
                }
                else
                {
                    _logger.LogInformation("Attempt was made to send reminder to a non-text channel {ChannelId}. Disabling reminder for guild {GuildId}.",
                                           reminder.ChannelId,
                                           reminder.GuildId);

                    await DisableJob();
                }

                async Task DisableJob()
                {
                    var entityService = scope.ServiceProvider.GetRequiredService <IEntityService <RecurringReminderMessage> >();
                    var trackedEntity = await entityService.Find(reminder.Id);

                    trackedEntity.Enabled = false;
                    trackedEntity.Fault   = RecurringReminderMessage.FaultType.InvalidChannel;
                    await entityService.Update(trackedEntity);

                    _jobService.CancelRecurringJob(reminder.GetJobId());
                }
            }
        }
Beispiel #3
0
 /// <summary>
 ///     Helper method to stop the recurring job if it exists.
 /// </summary>
 /// <param name="reminder"></param>
 private void StopJobIfExists(RecurringReminderMessage reminder)
 {
     _jobService.CancelRecurringJob(reminder.GetJobId());
 }