Home > C#, MongoDB, Sitecore > Launch Mongo DB with Sitecore Pipelines

Launch Mongo DB with Sitecore Pipelines

My  colleague Brian Pedersen recently wrote at blog post about setting up MongoDB and starting it up with a simple bat-file read it here


Combine the first step “download” mongo with starting a process as my other colleague Anders Laub showed how to do in his crush png post here http://laubplusco.net/crush-png-in-sitecore/


Then you have a pipeline that can start mongo if it isn’t running.

First lets create a new custom pipeline to start a mongo instance if none  is running

First the new “startmongodb” pipeline

  <processor type="Mongo.PingMongoDB, Mongo" />
  <processor type="Mongo.StartMongoDB, Mongo" />

  Replace string constant with more generic method, First a processor that test to see if mongo is allready running.

   public class PingMongoDB
        public void Process(PipelineArgs args)
            Log.Audit("Pinging Mongo", this);
            //Replace with connectionstring from config file
            var client = new MongoClient("mongodb://localhost");

           var server = client.GetServer();

              Log.Audit("Mongo Allready running", this);
            catch (Exception)
                Log.Audit("Mongo Not Running", this);

 Now to the start mongo processor 

    public class StartMongoDB
        public void Process(PipelineArgs args)
            var startInfo = new ProcessStartInfo
                CreateNoWindow = true,
                UseShellExecute = false,
                //Replace with path to  your mongo
                FileName = "D:\\MongoDB\\bin\\mongod.exe",
              //Replace with path to  your mongo datadrive
                Arguments = @"--dbpath sc75rev140429\Databases\MongoDBData",
                WindowStyle = ProcessWindowStyle.Hidden
                Log.Audit("Trying to start mongo",this);
                using (var exeProcess = System.Diagnostics.Process.Start(startInfo))
                Log.Audit("Mongo started", this);
            catch (Exception exception)
                Log.Error("Could not start mongo", exception, this);


Finally we need to run the “startmongodb” pipeline when Sitecore Starts/Initialize so in

the initialize pipeline and at the end add 

<processor type="Mongo.RunMongoPipeline, Mongo" />

code for this simple processor

public class RunMongoPipeline
        public void Process(PipelineArgs arg)
            CorePipeline.Run("startmongodb",new PipelineArgs());

From the log we can now see if mongo isn’t runnnig and was started also there is now warnings in the log :
INFO AUDIT (default\Anonymous): Mongo Not Running
INFO AUDIT (default\Anonymous): Trying to start mongo
INFO AUDIT (default\Anonymous): Mongo started

so the follwing isn’t seen in the log which it would be if mongo wasn’t running

ERROR MongoDbDictionary.Store() has failed.
Exception: Sitecore.Analytics.DataAccess.DatabaseNotAvailableException
Message: Database not available
Source: Sitecore.Analytics.MongoDB
   at Sitecore.Analytics.Data.DataAccess.MongoDb.MongoDbCollection.Execute(Action action, ExceptionBehavior exceptionBehavior)
   at Sitecore.Analytics.Data.DataAccess.MongoDb.MongoDbDictionary.Store(Object value)

What is left now is cleaning the code and use args for supplying the correct values instead of hardcoded strings. This is left as a free of charge  excercise.

Categories: C#, MongoDB, Sitecore Tags: , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: