Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Processor for updateMongoDriverSettings pipeline to alter MongoDB settings that are not available in the connection string
using System;
using Sitecore.Analytics.Pipelines.UpdateMongoDriverSettings;
using Sitecore.Diagnostics;
namespace MySite.Foundation.SitecoreExtensions.Pipelines.UpdateMongoDriverSettings
{
public class CustomMongoDriverSettings : UpdateMongoDriverSettingsProcessor
{
private readonly string _maxConnectionPoolSize;
private readonly string _minConnectionPoolSize;
private readonly string _maxConnectionIdleTimeSeconds;
private readonly string _maxConnectionLifeTimeSeconds;
public CustomMongoDriverSettings(string maxConnectionPoolSize, string minConnectionPoolSize, string maxConnectionIdleTimeSeconds, string maxConnectionLifeTimeSeconds)
{
_maxConnectionPoolSize = maxConnectionPoolSize;
_minConnectionPoolSize = minConnectionPoolSize;
_maxConnectionIdleTimeSeconds = maxConnectionIdleTimeSeconds;
_maxConnectionLifeTimeSeconds = maxConnectionLifeTimeSeconds;
}
public override void UpdateSettings(UpdateMongoDriverSettingsArgs args)
{
Log.Debug($"[MySite.Foundation.SitecoreExtensions.Pipelines.UpdateMongoDriverSettings.CustomMongoDriverSettings] UpdateSettings Default Values: MaxConnectionPoolSize = {args.MongoSettings.MaxConnectionPoolSize} MinConnectionPoolSize = {args.MongoSettings.MinConnectionPoolSize} MaxConnectionTimeSpan = {args.MongoSettings.MaxConnectionIdleTime} MaxConnectionLifeTime = {args.MongoSettings.MaxConnectionLifeTime}", this);
int maxConnectionPoolSizeInt = int.TryParse(_maxConnectionPoolSize, out maxConnectionPoolSizeInt) ? maxConnectionPoolSizeInt : args.MongoSettings.MaxConnectionPoolSize;
int minConnectionPoolSizeInt = int.TryParse(_minConnectionPoolSize, out minConnectionPoolSizeInt) ? minConnectionPoolSizeInt : args.MongoSettings.MinConnectionPoolSize;
int maxConnectionIdleTimeSecondsInt = int.TryParse(_maxConnectionIdleTimeSeconds, out maxConnectionIdleTimeSecondsInt) ? maxConnectionIdleTimeSecondsInt : args.MongoSettings.MaxConnectionIdleTime.Seconds;
int maxConnectionLifeTimeSecondsInt = int.TryParse(_maxConnectionLifeTimeSeconds, out maxConnectionLifeTimeSecondsInt) ? maxConnectionLifeTimeSecondsInt : args.MongoSettings.MaxConnectionLifeTime.Seconds;
var maxConnectionIdleTimeSpan = new TimeSpan(0, 0, maxConnectionIdleTimeSecondsInt);
var maxConnectionLifeTimeSpan = new TimeSpan(0, 0, maxConnectionLifeTimeSecondsInt);
args.MongoSettings.MaxConnectionPoolSize = maxConnectionPoolSizeInt;
args.MongoSettings.MinConnectionPoolSize = minConnectionPoolSizeInt;
args.MongoSettings.MaxConnectionIdleTime = maxConnectionIdleTimeSpan;
args.MongoSettings.MaxConnectionLifeTime = maxConnectionLifeTimeSpan;
Log.Debug($"[MySite.Foundation.SitecoreExtensions.Pipelines.UpdateMongoDriverSettings.CustomMongoDriverSettings] UpdateSettings Set Values: MaxConnectionPoolSize = {args.MongoSettings.MaxConnectionPoolSize} MinConnectionPoolSize = {args.MongoSettings.MinConnectionPoolSize} MaxConnectionTimeSpan = {args.MongoSettings.MaxConnectionIdleTime} MaxConnectionLifeTime = {args.MongoSettings.MaxConnectionLifeTime}", this);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment