Exemple #1
0
        private static int ProcessTransaction(CacheInvalidationRow row)
        {
            var count = 0;

            switch (row.TransactionType.ToLowerInvariant())
            {
            case "all":
                count += InvalidateAll();
                break;

            case "nagsall":
                // no action needed on common db apart from
                // updating IsCommonCacheInvalidated
                break;

            case "politicianimage":
                // no action needed on common db apart from
                // updating IsCommonCacheInvalidated
                break;

            default:
                throw new VoteException(
                          "Unidentified invalidation transaction type" + row.TransactionType);
            }
            CacheInvalidation.UpdateIsCommonCacheInvalidatedById(true, row.Id);
            return(count);
        }
Exemple #2
0
        // This method is only used by the CommonCacheCleanup which is run via the
        // Windows task scheduler
        public static void CleanUpCacheInvalidation()
        {
            string message;

            try
            {
                VotePage.LogInfo("CleanUpCacheInvalidation", "Started");

                // Get the number of days to retain, default to 7
                var daysString =
                    ConfigurationManager.AppSettings["VoteCommonCacheInvalidationRetentionDays"];
                if (!int.TryParse(daysString, out var days))
                {
                    days = 7;
                }

                // Convert to a past DateTime
                var expiration = DateTime.UtcNow - new TimeSpan(days, 0, 0, 0);

                // Do it
                var deleted = CacheInvalidation.DeleteExpiredTransactions(expiration);

                message =
                    $"{deleted} CacheInvalidation rows deleted";
            }
            catch (Exception ex)
            {
                VotePage.LogException("CleanUpCacheInvalidation", ex);
                message = $"Exception: {ex.Message} [see exception log for details]";
            }

            VotePage.LogInfo("CleanUpCacheInvalidation", message);
        }
Exemple #3
0
        private void InvalidateCache(List <OrganizationServiceCachePluginMessage> batchedPluginMessage)
        {
            //Set Properties
            var cacheMessage = new OrganizationServiceCacheBatchedPluginMessage();

            cacheMessage.MessageName          = batchedPluginMessage[0].MessageName;
            cacheMessage.RelatedEntities      = batchedPluginMessage[0].RelatedEntities;
            cacheMessage.Relationship         = batchedPluginMessage[0].Relationship;
            cacheMessage.Target               = batchedPluginMessage[0].Target;
            cacheMessage.BatchedPluginMessage = batchedPluginMessage;

            var messageName = batchedPluginMessage[0].Target != null
                                ? batchedPluginMessage[0].Target.LogicalName
                                : batchedPluginMessage[0].MessageName;

            ADXTrace.Instance.TraceInfo(TraceCategory.Application, string.Format("Posting Batch Cache Invalidation Request for {0}  with Count {1} ", messageName, batchedPluginMessage.Count));

            retryPolicy.Value.ExecuteAction(() => CacheInvalidation.ProcessMessage(cacheMessage));
        }
Exemple #4
0
        // This method is only used by the CommonCacheInvalidation which is run via the
        // Windows task scheduler
        public static void ProcessPendingTransactions()
        {
            string message;

            try
            {
                VotePage.LogInfo("CommonCacheInvalidation", "Started");

                var table = CacheInvalidation.GetUnprocessedData();
                var count = table.Sum(ProcessTransaction);

                message =
                    $"{table.Count()} CacheInvalidation rows processed, {count} Page rows deleted";
            }
            catch (Exception ex)
            {
                VotePage.LogException("CommonCacheInvalidation", ex);
                message = $"Exception: {ex.Message} [see exception log for details]";
            }
            VotePage.LogInfo("CommonCacheInvalidation", message);
        }
Exemple #5
0
        private void ProcessNotification(CancellationToken cancellationToken, PluginMessageRequest request)
        {
            try
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    return;
                }

                if (!WebNotificationCryptography.ValidateRequest(request.Authorization))
                {
                    WebNotificationEventSource.Log.AuthorizationValidationFailed();

                    return;
                }

                var message = this.GetMessage(request);

                if (message == null)
                {
                    WebNotificationEventSource.Log.MessageInvalid();

                    return;
                }

                CacheInvalidation.ProcessMessage(message);

                if (SearchIndexApplicableMessages.Contains(message.MessageName, MessageComparer))
                {
                    var serviceContext = CrmConfigurationManager.CreateContext();
                    SearchIndexBuildRequest.ProcessMessage(message, serviceContext: serviceContext);
                }
            }
            catch (TaskCanceledException e)
            {
                ADXTrace.Instance.TraceWarning(TraceCategory.Application, e.Message);
            }
        }
Exemple #6
0
 protected override IEnumerable <CacheInvalidationItem> GetCacheInvalidationItems()
 {
     return(CacheInvalidation.GetOrganisationInvalidationItems(_organisationId, _email).Union(
                CacheInvalidation.GetUserInvalidationItems(_organisationId, _email)));
 }
 protected override IEnumerable <CacheInvalidationItem> GetCacheInvalidationItems()
 {
     return(CacheInvalidation.GetOrganisationInvalidationItems(_organisationId));
 }
Exemple #8
0
 public static void ScheduleInvalidation(string transactionType, string key)
 {
     CacheInvalidation.Insert(
         transactionType: transactionType, cacheKey: key,
         cacheTimeStamp: DateTime.UtcNow, isCommonCacheInvalidated: false);
 }
Exemple #9
0
        public EditUserResponse Invoke(EditUserRequest request)
        {
            Trace("Starting...");

            var userId                 = User.GetId(request.UserId);
            var existingUser           = Load <User>(userId);
            var cacheInvalidationItems = new List <CacheInvalidationItem>();

            if (existingUser == null)
            {
                return(new EditUserResponse
                {
                    Status = EditUserStatus.UserNotFound
                });
            }

            var userMapping = Session.MasterRaven.Query <UserOrganisationMapping>().First(u => u.EmailAddress == existingUser.Email);

            if (userMapping == null)
            {
                return(new EditUserResponse
                {
                    Status = EditUserStatus.UserNotFound
                });
            }

            _authorisationManager.Authorise(existingUser, request.CurrentUser);

            var existingEmail = Session.Raven.Query <User, Indexing.Users>().FirstOrDefault(u => u.Email == request.Email && u.Id != userId);

            if (existingEmail != null)
            {
                return(new EditUserResponse
                {
                    Status = EditUserStatus.EmailExists
                });
            }

            if (existingUser.Email != request.Email)
            {
                userMapping.EmailAddress = request.Email;
                Session.SynchroniseIndexes <UserOrganisationMappings>(true);
            }

            var ravenInstances = _getRavenInstancesQuery.Invoke(new GetRavenInstancesRequest()).RavenInstances;

            //find the users accounts in their organisations and sync the user
            foreach (var organisationId in userMapping.Organisations)
            {
                var organisation = MasterLoad <Organisation>(organisationId);
                if (organisation == null)
                {
                    userMapping.Organisations.Remove(organisationId);
                }
                else
                {
                    organisation.RavenInstance = ravenInstances.First(r => r.Id == organisation.RavenInstanceId);

                    using (Session.SwitchOrg(organisation))
                    {
                        var user = Session.Raven.Query <User, Indexing.Users>().FirstOrDefault(u => u.Email == request.Email);

                        if (user == null)
                        {
                            userMapping.Organisations.Remove(organisationId);
                        }
                        else
                        {
                            user.Email     = request.Email;
                            user.FirstName = request.FirstName;
                            user.LastName  = request.LastName;

                            if (organisation.Id == request.CurrentUser.ActiveOrganisation.Id)
                            {
                                if (request.Administrator.HasValue && existingUser.Role != UserRole.SuperUser)
                                {
                                    user.Role = request.Administrator.Value ? UserRole.Administrator : UserRole.User;
                                }

                                if (request.GroupIds != null)
                                {
                                    user.GroupIds = request.GroupIds.Select(Group.GetId).ToList();
                                }
                            }

                            cacheInvalidationItems.AddRange(CacheInvalidation.GetUserInvalidationItems(organisation.Id, existingUser.Email));
                        }
                    }
                }
            }

            Session.SynchroniseIndexes <Indexing.Users, Indexing.Groups>();

            return(new EditUserResponse(cacheInvalidationItems)
            {
                Status = EditUserStatus.Ok
            });
        }
        //private static int InvalidatePoliticianIssueAll()
        //{
        //  var count = CachePoliticianIssuePages.CountTable(0);
        //  CachePoliticianIssuePages.TruncateTable();
        //  return count;
        //}

        //private static int InvalidatePoliticianIssueByIssuekey(string issueKey)
        //{
        //  return CachePoliticianIssuePages.DeleteByIssueKey(issueKey);
        //}

        //private static int InvalidatePoliticianIssueByPoliticianKey(
        //  string politicianKey)
        //{
        //  return CachePoliticianIssuePages.DeleteByPoliticianKey(politicianKey);
        //}

        //private static int InvalidatePoliticianIssueByPoliticianKeyIssueKey(
        //  string politicianKey, string issueKey)
        //{
        //  return CachePoliticianIssuePages.DeleteByPoliticianKeyIssueKey(
        //    politicianKey, issueKey);
        //}

        //private static int InvalidateReferendumAll()
        //{
        //  var count = CacheReferendumPages.CountTable(0);
        //  CacheReferendumPages.TruncateTable();
        //  return count;
        //}

        //private static int InvalidateReferendumByElectionkey(string electionKey)
        //{
        //  return CacheReferendumPages.DeleteByElectionKey(electionKey);
        //}

        private static int ProcessTransaction(CacheInvalidationRow row)
        {
            var count = 0;

            switch (row.TransactionType.ToLowerInvariant())
            {
            case "all":
                count += InvalidateAll();
                break;

            //case "ballotall":
            //  count += InvalidateBallotAll();
            //  break;

            //case "ballotbydomaindesigncodeelectionkey":
            //  {
            //    var keys = row.CacheKey.Split('|');
            //    if (keys.Length == 2)
            //      count += InvalidateBallotByDomainDesignCodeElectionKey(keys[0], keys[1]);
            //  }
            //  break;

            //case "ballotbyelectionkey":
            //  count += InvalidateBallotByElectionKey(row.CacheKey);
            //  break;

            //case "electedall":
            //  count += InvalidateElectedAll();
            //  break;

            //case "electedbystatecode":
            //  count += InvalidateElectedByStateCode(row.CacheKey);
            //  break;

            //case "electionall":
            //  count += InvalidateElectionAll();
            //  break;

            //case "electionbyelectionkey":
            //  count += InvalidateElectionByElectionKey(row.CacheKey);
            //  break;

            //case "introall":
            //  count += InvalidateIntroAll();
            //  break;

            //case "introbypoliticiankey":
            //  count += InvalidateIntroByPoliticianKey(row.CacheKey);
            //  break;

            //case "issueall":
            //  count += InvalidateIssueAll();
            //  break;

            //case "issuebyissuekey":
            //  count += InvalidateIssueByIssueKey(row.CacheKey);
            //  break;

            //case "issuebyofficekey":
            //  count += InvalidateIssueByOfficeKey(row.CacheKey);
            //  break;

            //case "issuebyelectionkeyofficekey":
            //  {
            //    var keys = row.CacheKey.Split('|');
            //    if (keys.Length == 2)
            //      count += InvalidateIssueByElectionKeyOfficeKey(keys[0], keys[1]);
            //  }
            //  break;

            //case "issuebyofficekeyissuekey":
            //  {
            //    var keys = row.CacheKey.Split('|');
            //    if (keys.Length == 2)
            //      count += InvalidateIssueByOfficeKeyIssueKey(keys[0], keys[1]);
            //  }
            //  break;

            //case "issuebyelectionkeyofficekeyissuekey":
            //  {
            //    var keys = row.CacheKey.Split('|');
            //    if (keys.Length == 3)
            //      count += InvalidateIssueByElectionKeyOfficeKeyIssueKey(
            //        keys[0], keys[1], keys[2]);
            //  }
            //  break;

            case "nagsall":
                // no action needed on common db apart from
                // updating IsCommonCacheInvalidated
                break;

            //case "officialsall":
            //  count += InvalidateOfficialsAll();
            //  break;

            //case "officialsbystatecode":
            //  count += InvalidateOfficialsByStateCode(row.CacheKey);
            //  break;

            //case "officialsbystatecodecountycode":
            //  {
            //    var keys = row.CacheKey.Split('|');
            //    if (keys.Length == 2)
            //      count += InvalidateOfficialsByStateCodeCountyCode(keys[0], keys[1]);
            //  }
            //  break;

            //case "officialsbystatecodecountycodelocalcode":
            //  {
            //    var keys = row.CacheKey.Split('|');
            //    if (keys.Length == 3)
            //      count += InvalidateOfficialsByStateCodeCountyCodeLocalCode(
            //        keys[0], keys[1], keys[2]);
            //  }
            //  break;

            case "politicianimage":
                // no action needed on common db apart from
                // updating IsCommonCacheInvalidated
                break;

            //case "politicianissueall":
            //  count += InvalidatePoliticianIssueAll();
            //  break;

            //case "politicianissuebyissuekey":
            //  count += InvalidatePoliticianIssueByIssuekey(row.CacheKey);
            //  break;

            //case "politicianissuebypoliticiankey":
            //  count += InvalidatePoliticianIssueByPoliticianKey(row.CacheKey);
            //  break;

            //case "politicianissuebypoliticiankeyissuekey":
            //  {
            //    var keys = row.CacheKey.Split('|');
            //    if (keys.Length == 2)
            //      count += InvalidatePoliticianIssueByPoliticianKeyIssueKey(keys[0], keys[1]);
            //  }
            //  break;

            //case "referendumall":
            //  count += InvalidateReferendumAll();
            //  break;

            //case "referendumbyelectionkey":
            //  count += InvalidateReferendumByElectionkey(row.CacheKey);
            //  break;

            default:
                throw new VoteException(
                          "Unidentified invalidation transaction type" + row.TransactionType);
            }
            CacheInvalidation.UpdateIsCommonCacheInvalidatedById(true, row.Id);
            return(count);
        }