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
<startmongodb> <processor type="Mongo.PingMongoDB, Mongo" /> <processor type="Mongo.StartMongoDB, Mongo" /> </startmongodb>
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(); try { server.Ping(); Log.Audit("Mongo Allready running", this); args.AbortPipeline(); } 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 }; try { Log.Audit("Trying to start mongo",this); using (var exeProcess = System.Diagnostics.Process.Start(startInfo)) { exeProcess.WaitForExit(50); } 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.
-
01/10/2014 at 13:53Sitecore and xDB – Setting up MongoDB on your developer machine | Brian Pedersen's Sitecore and .NET Blog
-
21/01/2015 at 08:31MongoDB Setups für Sitecore – Maku