/// <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 communicationQuery = new CommunicationService(new RockContext()).Queryable() .Where(c => c.Status == CommunicationStatus.Approved && c.Recipients.Any(r => r.Status == CommunicationRecipientStatus.Pending) && ( (!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 pendingCount = communicationQuery.Count(); var communications = communicationQuery.ToList(); int processedCount = 0; int sendCount = 0; string currentActivityDescription = "Initializing"; try { foreach (var comm in communications) { currentActivityDescription = string.Format("Sending Communication '{0}' [Id={1}]", comm.ToString(), comm.Id); var medium = comm.Medium; if (medium != null) { medium.Send(comm); sendCount++; } processedCount++; } pendingCount = pendingCount - processedCount; string resultDescription = string.Format("Send Communication processing completed.\n[{0} processed, {1} sent, {2} pending].", processedCount, sendCount, pendingCount); // Set the Job result summary. context.Result = RockJobResult.NewSuccessResult(resultDescription); } catch (Exception ex) { throw new Exception(string.Format("Job Execution failed at Processing Task \"{0}\".", currentActivityDescription), ex); } }
public virtual void Execute(IJobExecutionContext context) { // Check that we have at least one active Location Service. bool hasActiveServices = Rock.Address.VerificationContainer.Instance.Components.Any(x => x.Value.Value.IsActive); if (!hasActiveServices) { context.Result = RockJobResult.NewWarningResult("Address Verification canceled. There are no Active Location Services available to process the request."); return; } // Get the Job configuration settings. JobDataMap dataMap = context.JobDetail.JobDataMap; int maxRecords = Int32.Parse(dataMap.GetString("MaxRecordsPerRun")); int throttlePeriod = Int32.Parse(dataMap.GetString("ThrottlePeriod")); int retryPeriod = Int32.Parse(dataMap.GetString("RetryPeriod")); DateTime retryDate = DateTime.Now.Subtract(new TimeSpan(retryPeriod, 0, 0, 0)); using (var rockContext = new RockContext()) { var locationService = new LocationService(rockContext); // Get a set of Locations to process that: // 1. Are Active, Not Locked, and Not a Geofence. // 2. Have Not been previously geocoded or standardized, or are due for a retry. // 3. Are in order of modification date, so that the most recently updated addresses are given priority. var locationQuery = locationService.Queryable() .Where(l => ( (l.IsGeoPointLocked == null || l.IsGeoPointLocked == false) && l.IsActive && l.GeoFence == null && (l.GeocodedDateTime == null && (l.GeocodeAttemptedDateTime == null || l.GeocodeAttemptedDateTime < retryDate)) && (l.StandardizedDateTime == null && (l.StandardizeAttemptedDateTime == null || l.StandardizeAttemptedDateTime < retryDate)) )) .OrderByDescending(x => x.ModifiedDateTime); var pendingCount = locationQuery.Count(); var addresses = locationQuery.Take(maxRecords).ToList(); int processedCount = 0; int verifiedCount = 0; string currentActivityDescription = "Initializing"; try { foreach (var address in addresses) { currentActivityDescription = string.Format("Verifying Location '{0}' [Id={1}]", address, address.Id); DateTime?lastGeocoded = address.GeocodedDateTime; DateTime?lastStandardized = address.StandardizedDateTime; locationService.Verify(address, false); // currently not reverifying rockContext.SaveChanges(); if ((address.GeocodedDateTime.HasValue && address.GeocodedDateTime.Value.Subtract(lastGeocoded.GetValueOrDefault(DateTime.MinValue)).TotalSeconds > 0) || (address.StandardizedDateTime.HasValue && address.StandardizedDateTime.Value.Subtract(lastStandardized.GetValueOrDefault(DateTime.MinValue)).TotalSeconds > 0)) { verifiedCount++; } processedCount++; Thread.Sleep(throttlePeriod); } string resultDescription = string.Format("Job completed.\n[{0} processed, {1} verified, {2} failed, {3} pending].", processedCount, verifiedCount, processedCount - verifiedCount, pendingCount - processedCount); // Set the Job result summary. context.Result = RockJobResult.NewSuccessResult(resultDescription); } catch (Exception ex) { throw new Exception(string.Format("Job failed at Processing Task \"{0}\".", currentActivityDescription), ex); } } }