Database.put can work or not if database is inactive

14 views
Skip to first unread message

Tim Zhang

unread,
Sep 19, 2014, 4:19:45 AM9/19/14
to rav...@googlegroups.com
The following if my code ,I want to sure when the database is inactive  then Database.Put can work well or not.

Database.Put(RavenConstant.NsbrTestDocumentKey,null,
                    RavenJObject.FromObject(new NsbrTestDocument{ TestValue = "Write Test Data"}),metadata,null);

                Database.Get(RavenConstant.NsbrTestDocumentKey, null);


public class SendMasterHeartbeatMessageTask :  IDisposable
    {
        private static readonly Logger logger = NLog.LogManager.GetCurrentClassLogger();
        private Timer timer;
        public DocumentDatabase Database { get; set; }
        private int nsbrUnsentMessagesAlertThreshold;
        public const string RavenDocumentsFromUnsentSnapshot = "Nsbr/DocumentsFromUnsentSnapshot";
        public bool isDuplexDb = false;


        private volatile bool executing;

        #region Execute
        public void Execute(DocumentDatabase database)
        {
            if (string.IsNullOrEmpty(database.Name))  //except system db
                return;

            Database = database;

            EnsureIndex(database);

            isDuplexDb = Cofiguration.WebAppConfigUtilities.GetConfiguredBooleanValue(RavenConstant.NsbrDisableTargetPluginNservcieBusTask);
            
            nsbrUnsentMessagesAlertThreshold = Cofiguration.WebAppConfigUtilities.GetConfiguredIntValue(RavenConstant.NsbrUnsentMessagesAlertThreshold, 100);

            var resentFrequencyInSeconds = Cofiguration.WebAppConfigUtilities.GetConfiguredIntValue(RavenConstant.NsbrHeartbeatFrenquencyInSeconds, 30);

            logger.Info("Initialized SendMasterHeartbeatMessageTask, will scan for every {0} seconds",resentFrequencyInSeconds);

            timer = new Timer(TimerCallback, null, TimeSpan.FromSeconds(resentFrequencyInSeconds), TimeSpan.FromSeconds(resentFrequencyInSeconds));
        } 
        #endregion
        
        #region Dispose
        public void Dispose()
        {
            if (timer != null)
                timer.Dispose();
        } 
        #endregion

        #region TimerCallback
        private void TimerCallback(object state)
        {
            if (executing)
                return;

            executing = true;
            try
            {
                var metadata = new RavenJObject()
                {
                    {Constants.RavenEntityName,RavenJToken.FromObject(string.Format("{0}s",typeof(NsbrTestDocument).Name.ToLower()))}
                };

                Database.Put(RavenConstant.NsbrTestDocumentKey,null,
                    RavenJObject.FromObject(new NsbrTestDocument{ TestValue = "Write Test Data"}),metadata,null);

                Database.Get(RavenConstant.NsbrTestDocumentKey, null);

                var queryResult = Database.Query(RavenDocumentsFromUnsentSnapshot, new IndexQuery
                    {
                        Start = 0,
                        PageSize = 1,
                    }, CancellationToken.None);

                if (queryResult.TotalResults>=nsbrUnsentMessagesAlertThreshold)
                {
                    logger.Fatal("Unsent Message count is greater than the threshold.");
                }
                var message = new RavenHostHeartbeatMessage
                {
                    IsReadWriteWorks = true,
                    HostName = GlobalBus.HostName,
                    DbName = Database.Name,
                    UnsentMessageCount = queryResult.TotalResults
                };
                if (!isDuplexDb)
                    message.IsMasterDb = true;
                GlobalBus.Bus.Send(message);
                
            }
            catch (Exception e)
            {
                logger.FatalException("SendMasterHeartbeatMessageTask Error", e);
            }
            finally
            {
                executing = false;
            }
        } 
        #endregion

        #region EnsureIndex
        private static void EnsureIndex(DocumentDatabase database)
        {
            var indexDefinition = database.GetIndexDefinition(RavenDocumentsFromUnsentSnapshot);
            if (indexDefinition == null)
            {
                database.PutIndex(RavenDocumentsFromUnsentSnapshot,
                    new IndexDefinition
                    {
                        Map =
                            @"
from doc in docs
let tag = doc[""@metadata""][""Raven-Entity-Name""]
where tag != null && ( tag==""nsbrunsentdocumentsnapshots"" || tag ==""nsbrunsentattachmentsnapshots"" )
select new { Tag = tag }"
                    });
                logger.Info(() => string.Format("created the index {0}-{1}", RavenDocumentsFromUnsentSnapshot, database.Name ?? Constants.SystemDatabase));
            }
        } 
        #endregion

    }

Oren Eini (Ayende Rahien)

unread,
Sep 19, 2014, 4:26:36 AM9/19/14
to ravendb
I'm not sure that I understand.
Your code seems to be intended to run inside RavenDB, what are you askin?

Hibernating Rhinos Ltd  

Oren Eini l CEO Mobile: + 972-52-548-6969

Office: +972-4-622-7811 l Fax: +972-153-4-622-7811

 


--
You received this message because you are subscribed to the Google Groups "RavenDB - 2nd generation document database" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages