Permalink
Browse files

Will now handle service initialization in an async manner

  • Loading branch information...
1 parent b35b98e commit 544597a396ffc3f5a8daf3e93b8aa57f77ac8c23 @ayende ayende committed Mar 23, 2012
Showing with 31 additions and 4 deletions.
  1. +31 −4 Raven.Server/RavenService.cs
@@ -3,15 +3,18 @@
// Copyright (c) Hibernating Rhinos LTD. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
+using System;
+using System.Diagnostics;
using System.ServiceProcess;
-using Raven.Database;
using Raven.Database.Config;
+using Task = System.Threading.Tasks.Task;
namespace Raven.Server
{
internal partial class RavenService : ServiceBase
{
private RavenDbServer server;
+ private Task startTask;
public RavenService()
{
@@ -20,13 +23,37 @@ public RavenService()
protected override void OnStart(string[] args)
{
- server = new RavenDbServer(new RavenConfiguration());
+ startTask = Task.Factory.StartNew(() =>
+ {
+ try
+ {
+ server = new RavenDbServer(new RavenConfiguration());
+ }
+ catch (Exception e)
+ {
+ EventLog.WriteEntry("RavenDB service failed to start because of an error" + Environment.NewLine + e, EventLogEntryType.Error);
+ Stop();
+ }
+ });
+
+ if(startTask.Wait(TimeSpan.FromSeconds(20)) == false)
+ {
+ EventLog.WriteEntry(
+ "Startup for RavenDB service seems to be taking longer than usual, moving initialization to a background thread",
+ EventLogEntryType.Warning);
+ }
+
}
protected override void OnStop()
{
- if (server != null)
- server.Dispose();
+ startTask.ContinueWith(task =>
+ {
+ if (server != null)
+ server.Dispose();
+ return task;
+ }).Wait();
+
}
}
}

0 comments on commit 544597a

Please sign in to comment.