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
}