public ActionConfirmation Delete(int itemId) { var emailToDelete = _queueRepository.Get(itemId); if (emailToDelete != null) { try { _queueRepository.Delete(emailToDelete); _queueRepository.DbContext.CommitChanges(); return(ActionConfirmation.CreateSuccess("The email was successfully deleted.")); } catch { return (ActionConfirmation.CreateFailure( "A problem was encountered preventing the email from being deleted. " + "Another item likely depends on this email")); } } return (ActionConfirmation.CreateFailure( "The email could not be found to deletion. It may already have been deleted")); }
/// <summary> /// 在队列中对发送失败的邮件更新下次发送时间及发送次数,并且删除超过最大发送次数的邮件 /// </summary> /// <param name="ids">发送失败的邮件Id集合</param> /// <param name="retryInterval">失败的Email过多少分钟再次重新发送</param> /// <param name="maxNumberOfTries">尝试发送的最大次数,超过该次数将设置为失败状态</param> public void SendFailed(IEnumerable <int> ids, int retryInterval, int maxNumberOfTries) { //根据当前的TryTime,若maxNumberOfTries <= TryTime+1 则:设置邮件状态IsFailed为:True // 若maxNumberOfTries > TryTime+1 则:设置邮件NumberOfTries,根据retryInterval,修改NextTryTime //操作步骤: //1 根据ids获取所有的实体 //2 逐个进行判断,然后,编辑修改 //由于发送失败属于异常情况,暂不考虑性能问题 if (ids == null) { return; } foreach (int id in ids) { EmailQueueEntry emailQueueEntry = emailQueueRepository.Get(id); if (emailQueueEntry == null) { continue; } else { //尝试次数大于等于最多尝试次数,设置为发送失败 if ((emailQueueEntry.NumberOfTries + 1) >= maxNumberOfTries) { emailQueueEntry.IsFailed = true; } else//修改尝试时间和下次尝试时间 { emailQueueEntry.NumberOfTries = emailQueueEntry.NumberOfTries + 1; emailQueueEntry.NextTryTime = DateTime.UtcNow.AddMinutes(retryInterval); } //更新到数据库 emailQueueRepository.Update(emailQueueEntry); } } }