예제 #1
0
        public override void Execute(object parameter)
        {
            var alertsDocument = new AlertsDocument
            {
                Alerts = new List <Alert>(alertsModel.ServerAlerts)
            };

            ApplicationModel.DatabaseCommands
            .PutAsync(Constants.RavenAlerts, null, RavenJObject.FromObject(alertsDocument), new RavenJObject())
            .ContinueOnSuccessInTheUIThread(() => ApplicationModel.Current.Notifications.Add(new Notification("Alerts Saved")));
        }
예제 #2
0
        public static void AddAlert(this DocumentDatabase self, Alert alert)
        {
            while (true)
            {
                using (self.TransactionalStorage.DisableBatchNesting())
                    using (var putSerialLock = self.DocumentLock.TryLock(25))
                    {
                        if (putSerialLock == null)
                        {
                            continue;
                        }

                        AlertsDocument alertsDocument;
                        var            alertsDoc = self.Documents.Get(Constants.RavenAlerts, null);
                        RavenJObject   metadata;
                        Etag           etag;
                        if (alertsDoc == null)
                        {
                            etag           = Etag.Empty;
                            alertsDocument = new AlertsDocument();
                            metadata       = new RavenJObject();
                        }
                        else
                        {
                            etag           = alertsDoc.Etag;
                            alertsDocument = alertsDoc.DataAsJson.JsonDeserialization <AlertsDocument>() ?? new AlertsDocument();
                            metadata       = alertsDoc.Metadata;
                        }

                        var withSameUniqe = alertsDocument.Alerts.FirstOrDefault(alert1 => alert1.UniqueKey == alert.UniqueKey);
                        if (withSameUniqe != null)
                        {
                            // copy information about observed
                            alert.LastDismissedAt = withSameUniqe.LastDismissedAt;
                            alertsDocument.Alerts.Remove(withSameUniqe);
                        }

                        alertsDocument.Alerts.Add(alert);
                        var document = RavenJObject.FromObject(alertsDocument);
                        document.Remove("Id");
                        try
                        {
                            self.Documents.Put(Constants.RavenAlerts, etag, document, metadata, null);
                            return;
                        }
                        catch (ConcurrencyException)
                        {
                            //try again...
                        }
                    }
            }
        }
예제 #3
0
        public static void AddAlert(this DocumentDatabase self, Alert alert)
        {
            while (true)
            {
                AlertsDocument alertsDocument;
                var            alertsDoc = self.Get(Constants.RavenAlerts, null);
                RavenJObject   metadata;
                Etag           etag;
                if (alertsDoc == null)
                {
                    etag           = Etag.Empty;
                    alertsDocument = new AlertsDocument();
                    metadata       = new RavenJObject();
                }
                else
                {
                    etag           = alertsDoc.Etag;
                    alertsDocument = alertsDoc.DataAsJson.JsonDeserialization <AlertsDocument>() ?? new AlertsDocument();
                    metadata       = alertsDoc.Metadata;
                }

                var withSameUniqe = alertsDocument.Alerts.FirstOrDefault(alert1 => alert1.UniqueKey == alert.UniqueKey);
                if (withSameUniqe != null)
                {
                    alertsDocument.Alerts.Remove(withSameUniqe);
                }

                alertsDocument.Alerts.Add(alert);
                var document = RavenJObject.FromObject(alertsDocument);
                document.Remove("Id");
                try
                {
                    self.Put(Constants.RavenAlerts, etag, document, metadata, null);
                    return;
                }
                catch (ConcurrencyException)
                {
                    //try again...
                }
            }
        }
예제 #4
0
        public static void AddAlert(this DocumentDatabase self, Alert alert)
        {
            var tries = 0;

            while (true)
            {
                using (self.TransactionalStorage.DisableBatchNesting())
                    using (var putSerialLock = self.DocumentLock.TryLock(25))
                    {
                        if (putSerialLock == null)
                        {
                            continue;
                        }

                        AlertsDocument alertsDocument;
                        var            alertsDoc = self.Documents.Get(Constants.RavenAlerts, null);
                        RavenJObject   metadata;
                        Etag           etag;
                        if (alertsDoc == null)
                        {
                            etag           = Etag.Empty;
                            alertsDocument = new AlertsDocument();
                            metadata       = new RavenJObject();
                        }
                        else
                        {
                            etag           = alertsDoc.Etag;
                            alertsDocument = alertsDoc.DataAsJson.JsonDeserialization <AlertsDocument>() ?? new AlertsDocument();
                            metadata       = alertsDoc.Metadata;
                        }

                        var withSameUnique = alertsDocument.Alerts.FirstOrDefault(alert1 => alert1.UniqueKey == alert.UniqueKey);
                        if (withSameUnique != null)
                        {
                            // copy information about observed
                            alert.LastDismissedAt = withSameUnique.LastDismissedAt;
                            alertsDocument.Alerts.Remove(withSameUnique);
                        }

                        alertsDocument.Alerts.Add(alert);
                        var document = RavenJObject.FromObject(alertsDocument);
                        document.Remove("Id");
                        try
                        {
                            self.Documents.Put(Constants.RavenAlerts, etag, document, metadata, null);
                            return;
                        }
                        catch (ConcurrencyException)
                        {
                            //try again...
                        }
                        catch (Exception e)
                        {
                            if (TransactionalStorageHelper.IsOutOfMemoryException(e))
                            {
                                if (tries++ < MaxTries)
                                {
                                    Thread.Sleep(11);
                                    continue;
                                }

                                Logger.WarnException("Couldn't save alerts document due to " +
                                                     $"{self.TransactionalStorage.FriendlyName} out of memory exception", e);
                                return;
                            }

                            throw;
                        }
                    }
            }
        }