/// <summary> /// Executes the specified context. /// </summary> /// <param name="context">The context.</param> public virtual void Execute(IJobExecutionContext context) { JobDataMap dataMap = context.JobDetail.JobDataMap; int expirationDays = dataMap.GetInt("ExpirationPeriod"); int delayMinutes = dataMap.GetInt("DelayPeriod"); var rockContext = new RockContext(); var qry = new CommunicationService(rockContext) .GetQueued(expirationDays, delayMinutes, false, false) .OrderBy(c => c.Id); var exceptionMsgs = new List <string>(); int communicationsSent = 0; foreach (var comm in qry.AsNoTracking().ToList()) { try { Rock.Model.Communication.Send(comm); communicationsSent++; } catch (Exception ex) { exceptionMsgs.Add(string.Format("Exception occurred sending communication ID:{0}:{1} {2}", comm.Id, Environment.NewLine, ex.Messages().AsDelimited(Environment.NewLine + " "))); ExceptionLogService.LogException(ex, System.Web.HttpContext.Current); } } if (communicationsSent > 0) { context.Result = string.Format("Sent {0} {1}", communicationsSent, "communication".PluralizeIf(communicationsSent > 1)); } else { context.Result = "No communications to send"; } if (exceptionMsgs.Any()) { throw new Exception("One or more exceptions occurred sending communications..." + Environment.NewLine + exceptionMsgs.AsDelimited(Environment.NewLine)); } // check for communications that have not been sent but are past the expire date. Mark them as failed and set a warning. var beginWindow = RockDateTime.Now.AddDays(0 - expirationDays); var qryExpiredRecipients = new CommunicationRecipientService(rockContext).Queryable() .Where(cr => cr.Communication.Status == CommunicationStatus.Approved && cr.Status == CommunicationRecipientStatus.Pending && ( (!cr.Communication.FutureSendDateTime.HasValue && cr.Communication.CreatedDateTime.HasValue && cr.Communication.CreatedDateTime < beginWindow) || (cr.Communication.FutureSendDateTime.HasValue && cr.Communication.FutureSendDateTime < beginWindow) )); var count = qryExpiredRecipients.Count(); rockContext.BulkUpdate(qryExpiredRecipients, c => new CommunicationRecipient { Status = CommunicationRecipientStatus.Failed, StatusNote = "Communication was not sent before the expire window (possibly due to delayed approval)." }); }
/// <summary> /// Executes the specified context. /// </summary> /// <param name="context">The context.</param> public virtual void Execute(IJobExecutionContext context) { JobDataMap dataMap = context.JobDetail.JobDataMap; var beginWindow = RockDateTime.Now.AddDays(0 - dataMap.GetInt("ExpirationPeriod")); var endWindow = RockDateTime.Now.AddMinutes(0 - dataMap.GetInt("DelayPeriod")); var rockContext = new RockContext(); var qryPendingRecipients = new CommunicationRecipientService(rockContext).Queryable().Where(a => a.Status == CommunicationRecipientStatus.Pending); var qry = new CommunicationService(rockContext).Queryable() .Where(c => c.Status == CommunicationStatus.Approved && qryPendingRecipients.Where(r => r.CommunicationId == c.Id).Any() && ( (!c.FutureSendDateTime.HasValue && c.CreatedDateTime.HasValue && c.CreatedDateTime.Value.CompareTo(beginWindow) >= 0 && c.CreatedDateTime.Value.CompareTo(endWindow) <= 0) || (c.FutureSendDateTime.HasValue && c.FutureSendDateTime.Value.CompareTo(beginWindow) >= 0 && c.FutureSendDateTime.Value.CompareTo(endWindow) <= 0) )); var exceptionMsgs = new List <string>(); foreach (var comm in qry.AsNoTracking().ToList()) { try { var medium = comm.Medium; if (medium != null) { medium.Send(comm); } } catch (Exception ex) { exceptionMsgs.Add(string.Format("Exception occurred sending communication ID:{0}:{1} {2}", comm.Id, Environment.NewLine, ex.Messages().AsDelimited(Environment.NewLine + " "))); ExceptionLogService.LogException(ex, System.Web.HttpContext.Current); } } if (exceptionMsgs.Any()) { throw new Exception("One or more exceptions occurred sending communications..." + Environment.NewLine + exceptionMsgs.AsDelimited(Environment.NewLine)); } }
/// <summary> /// Binds the grid. /// </summary> private void BindGrid() { // If configured for a person and person is null, return int personEntityTypeId = EntityTypeCache.Get <Person>().Id; if (ContextTypesRequired.Any(e => e.Id == personEntityTypeId) && _person == null) { return; } var rockContext = new RockContext(); var qryCommunications = new CommunicationService(rockContext).Queryable().Where(c => c.Status != CommunicationStatus.Transient); string subject = tbSubject.Text; if (!string.IsNullOrWhiteSpace(subject)) { qryCommunications = qryCommunications.Where(c => c.Subject.Contains(subject)); } var communicationType = ddlType.SelectedValueAsEnumOrNull <CommunicationType>(); if (communicationType != null) { qryCommunications = qryCommunications.Where(c => c.CommunicationType == communicationType); } var communicationStatus = ddlStatus.SelectedValue.ConvertToEnumOrNull <CommunicationStatus>(); if (communicationStatus.HasValue) { qryCommunications = qryCommunications.Where(c => c.Status == communicationStatus.Value); } // only communications for the selected recipient (_person) if (_person != null) { qryCommunications = qryCommunications .Where(c => c.Recipients.Any(a => a.PersonAlias.PersonId == _person.Id && (a.Status == CommunicationRecipientStatus.Delivered || a.Status == CommunicationRecipientStatus.Opened))); } if (drpDates.LowerValue.HasValue) { qryCommunications = qryCommunications.Where(a => a.CreatedDateTime >= drpDates.LowerValue.Value); } if (drpDates.UpperValue.HasValue) { DateTime upperDate = drpDates.UpperValue.Value.Date.AddDays(1); qryCommunications = qryCommunications.Where(a => a.CreatedDateTime < upperDate); } string content = tbContent.Text; if (!string.IsNullOrWhiteSpace(content)) { qryCommunications = qryCommunications.Where(c => c.Message.Contains(content) || c.SMSMessage.Contains(content) || c.PushMessage.Contains(content)); } var sortProperty = gCommunication.SortProperty; if (sortProperty != null) { qryCommunications = qryCommunications.Sort(sortProperty); } else { qryCommunications = qryCommunications.OrderByDescending(c => c.CreatedDateTime); } gCommunication.EntityTypeId = EntityTypeCache.Get <Rock.Model.Communication>().Id; gCommunication.SetLinqDataSource(qryCommunications.AsNoTracking()); gCommunication.DataBind(); }
/// <summary> /// Executes the specified context. /// </summary> /// <param name="context">The context.</param> public virtual void Execute(IJobExecutionContext context) { JobDataMap dataMap = context.JobDetail.JobDataMap; var beginWindow = RockDateTime.Now.AddDays(0 - dataMap.GetInt("ExpirationPeriod")); var endWindow = RockDateTime.Now.AddMinutes(0 - dataMap.GetInt("DelayPeriod")); var nowDate = RockDateTime.Now; var rockContext = new RockContext(); var qryPendingRecipients = new CommunicationRecipientService(rockContext).Queryable().Where(a => a.Status == CommunicationRecipientStatus.Pending); var qry = new CommunicationService(rockContext).Queryable() .Where(c => c.Status == CommunicationStatus.Approved && qryPendingRecipients.Where(r => r.CommunicationId == c.Id).Any() && ( (!c.FutureSendDateTime.HasValue && c.CreatedDateTime.HasValue && c.CreatedDateTime.Value.CompareTo(beginWindow) >= 0 && c.CreatedDateTime.Value.CompareTo(endWindow) <= 0) || (c.FutureSendDateTime.HasValue && c.FutureSendDateTime.Value.CompareTo(beginWindow) >= 0 && c.FutureSendDateTime.Value.CompareTo(nowDate) <= 0) )); var exceptionMsgs = new List <string>(); int communicationsSent = 0; foreach (var comm in qry.AsNoTracking().ToList()) { try { var medium = comm.Medium; if (medium != null && medium.IsActive) { medium.Send(comm); communicationsSent++; } } catch (Exception ex) { exceptionMsgs.Add(string.Format("Exception occurred sending communication ID:{0}:{1} {2}", comm.Id, Environment.NewLine, ex.Messages().AsDelimited(Environment.NewLine + " "))); ExceptionLogService.LogException(ex, System.Web.HttpContext.Current); } } if (communicationsSent > 0) { context.Result = string.Format("Sent {0} {1}", communicationsSent, "communication".PluralizeIf(communicationsSent > 1)); } else { context.Result = "No communications to send"; } if (exceptionMsgs.Any()) { throw new Exception("One or more exceptions occurred sending communications..." + Environment.NewLine + exceptionMsgs.AsDelimited(Environment.NewLine)); } // check for communications that have not been sent but are past the expire date. Mark them as failed and set a warning. var qryExpired = new CommunicationService(rockContext).Queryable() .Where(c => c.Status == CommunicationStatus.Approved && qryPendingRecipients.Where(r => r.CommunicationId == c.Id).Any() && ( (!c.FutureSendDateTime.HasValue && c.CreatedDateTime.HasValue && c.CreatedDateTime.Value.CompareTo(beginWindow) < 0) || (c.FutureSendDateTime.HasValue && c.FutureSendDateTime.Value.CompareTo(beginWindow) < 0) )); foreach (var comm in qryExpired.ToList()) { foreach (var recipient in comm.Recipients.Where(r => r.Status == CommunicationRecipientStatus.Pending)) { recipient.Status = CommunicationRecipientStatus.Failed; recipient.StatusNote = "Communication was not sent before the expire window (possibly due to delayed approval)."; rockContext.SaveChanges(); } } }