private void TimerElapsed(object state)
        {
            if (_executing)
            {
                return;
            }

            _executing   = true;
            _nextRunDate = DateTime.MaxValue;

            try
            {
                ActivatePendingDocuments();
            }
            catch (Exception e)
            {
                _log.ErrorException("Error when trying to activate temporal revision documents", e);
            }
            finally
            {
                var runDate = PendingRevisionsIndex.GetNextActivationDate(_database);
                ResetTimer(runDate, SystemTime.UtcNow);
                _executing = false;
            }
        }
        private void ActivatePendingDocuments()
        {
            using (_database.DisableAllTriggersForCurrentThread())
            {
                var revisionKeys = PendingRevisionsIndex.GetRevisionsRequiringActivation(_database);

                foreach (var revisionkey in revisionKeys)
                {
                    _log.Info("Activating Temporal Document {0}", revisionkey);

                    // Establish a new transaction
                    var transactionInformation = new TransactionInformation {
                        Id = Guid.NewGuid().ToString(), Timeout = TimeSpan.FromMinutes(1)
                    };

                    // Get the current key from the revision key
                    var currentKey = revisionkey.Substring(0, revisionkey.IndexOf(TemporalConstants.TemporalKeySeparator, StringComparison.Ordinal));

                    // Mark the document as non-pending
                    _database.SetDocumentMetadata(revisionkey, transactionInformation, TemporalMetadata.RavenDocumentTemporalPending, false);

                    // Mark it in the history also
                    Etag historyEtag;
                    var  history = _database.GetTemporalHistoryFor(currentKey, transactionInformation, out historyEtag);
                    history.Revisions.First(x => x.Key == revisionkey).Pending = false;
                    _database.SaveTemporalHistoryFor(currentKey, history, transactionInformation, historyEtag);

                    // Load the new revisions document
                    var newRevisionDoc = _database.Get(revisionkey, transactionInformation);
                    var temporal       = newRevisionDoc.Metadata.GetTemporalMetadata();
                    if (temporal.Deleted)
                    {
                        // When the revision is a deletion, delete the current document
                        _database.Delete(currentKey, null, transactionInformation);
                    }
                    else
                    {
                        // Prepare the current document metadata
                        newRevisionDoc.Metadata.Remove(TemporalMetadata.RavenDocumentTemporalDeleted);
                        newRevisionDoc.Metadata.Remove(TemporalMetadata.RavenDocumentTemporalPending);
                        newRevisionDoc.Metadata.Remove("@id");
                        temporal.Status         = TemporalStatus.Current;
                        temporal.RevisionNumber = int.Parse(newRevisionDoc.Key.Split('/').Last());

                        // Copy the revision to the current document
                        _database.Put(currentKey, null, newRevisionDoc.DataAsJson, newRevisionDoc.Metadata, transactionInformation);
                    }

                    // Commit the transaction
                    _database.Commit(transactionInformation.Id);
                }
            }
        }
        public void Execute(DocumentDatabase database)
        {
            if (!database.IsBundleActive(TemporalConstants.BundleName))
            {
                return;
            }

            _database = database;

            PendingRevisionsIndex.CreateIndex(database);

            var now = SystemTime.UtcNow;

            ResetTimer(now, now);
        }