Skip to content

Commit 49eae44

Browse files
author
Maarten Willebrands
committed
Add support to update solr
1 parent 01b317f commit 49eae44

File tree

4 files changed

+61
-10
lines changed

4 files changed

+61
-10
lines changed

SyncTokenGnerator/Program.cs

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
using Microsoft.Azure.SqlDatabase.ElasticScale.ShardManagement;
2+
using Newtonsoft.Json;
3+
using Newtonsoft.Json.Linq;
24
using System;
35
using System.Collections.Generic;
46
using System.Data.SqlClient;
57
using System.IO;
68
using System.Linq;
9+
using System.Net;
710
using System.Runtime.Serialization.Formatters.Binary;
811

912
namespace SyncTokenGenerator
@@ -13,9 +16,9 @@ internal class Program
1316
static void Main(string[] args)
1417
{
1518
Console.WriteLine("Please enter the connectionstring to the shardmapmanager");
16-
1719
var shardMapManagerConnectionString = Console.ReadLine();
1820
Dictionary<string, long?> shardChangeVersions = new Dictionary<string, long?>();
21+
string syncToken = "";
1922

2023
try
2124
{
@@ -56,16 +59,52 @@ static void Main(string[] args)
5659
{
5760
BinaryFormatter binaryFormatter = new BinaryFormatter();
5861
binaryFormatter.Serialize(memoryStream, shardChangeVersions);
62+
syncToken = Convert.ToBase64String(memoryStream.ToArray());
5963
Console.WriteLine("The new sync token is:");
60-
Console.WriteLine(Convert.ToBase64String(memoryStream.ToArray()));
64+
Console.WriteLine(syncToken);
6165
}
6266
}
63-
catch(Exception ex)
67+
catch (Exception ex)
6468
{
65-
Console.WriteLine("Unable to access the Shard Map Manager database");
66-
Console.WriteLine(ex.Message);
69+
WriteError("Unable to access the Shard Map Manager database");
70+
WriteError(ex.Message);
71+
}
72+
73+
Console.WriteLine();
74+
Console.WriteLine("Update XDB SOLR Index? (Y/N)");
75+
bool updateSolr = Console.ReadLine().ToLowerInvariant() == "y";
76+
if (updateSolr)
77+
{
78+
try
79+
{
80+
Console.WriteLine("Please enter the SOLR XDB connectionstring, for ex https://localhost:8983/solr/sitecore_xdb");
81+
var solrConnectionString = Console.ReadLine();
82+
if (!solrConnectionString.EndsWith("/"))
83+
{
84+
solrConnectionString += "/";
85+
}
86+
solrConnectionString += "update?commit=true";
87+
using (var webclient = new WebClient())
88+
{
89+
var postBody = $"[{{ \"id\":\"xdb-index-token\",\"xdbtokenbytes_s\":\"{syncToken}\"}}]";
90+
webclient.Headers.Add("content-type", "application/json");
91+
var response = webclient.UploadString(solrConnectionString, "post", postBody);
92+
var jsonResponse = !string.IsNullOrEmpty(response) ? JObject.Parse(response) : null; ;
93+
if (!int.TryParse((jsonResponse["responseHeader"]?["status"] as JValue)?.Value?.ToString(), out var status) || status != 0)
94+
{
95+
WriteError("Invalid response recieved from SOLR");
96+
WriteError(jsonResponse?.ToString());
97+
}
98+
}
99+
}
100+
catch (Exception ex)
101+
{
102+
WriteError("Unable to update Solr");
103+
WriteError(ex.Message);
104+
}
67105
}
68-
106+
107+
Console.WriteLine("Done, press any key to exit");
69108
Console.ReadLine();
70109
}
71110

@@ -84,5 +123,13 @@ static string Credentials(string connectionString)
84123
}
85124
return result.ToString();
86125
}
126+
127+
static void WriteError(string message)
128+
{
129+
var originalColor = Console.ForegroundColor;
130+
Console.ForegroundColor = ConsoleColor.Red;
131+
Console.Error.WriteLine(message);
132+
Console.ForegroundColor = originalColor;
133+
}
87134
}
88135
}

SyncTokenGnerator/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
// You can specify all the values or you can default the Build and Revision Numbers
3333
// by using the '*' as shown below:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("1.0.0.0")]
36-
[assembly: AssemblyFileVersion("1.0.0.0")]
35+
[assembly: AssemblyVersion("1.1.0.0")]
36+
[assembly: AssemblyFileVersion("1.1.0.0")]

SyncTokenGnerator/SyncTokenGenerator.csproj

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<FileAlignment>512</FileAlignment>
1313
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
1414
<Deterministic>true</Deterministic>
15+
<IsWebBootstrapper>false</IsWebBootstrapper>
1516
<PublishUrl>publish\</PublishUrl>
1617
<Install>true</Install>
1718
<InstallFrom>Disk</InstallFrom>
@@ -22,9 +23,8 @@
2223
<UpdatePeriodically>false</UpdatePeriodically>
2324
<UpdateRequired>false</UpdateRequired>
2425
<MapFileExtensions>true</MapFileExtensions>
25-
<ApplicationRevision>0</ApplicationRevision>
26+
<ApplicationRevision>1</ApplicationRevision>
2627
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
27-
<IsWebBootstrapper>false</IsWebBootstrapper>
2828
<UseApplicationTrust>false</UseApplicationTrust>
2929
<PublishWizardCompleted>true</PublishWizardCompleted>
3030
<BootstrapperEnabled>true</BootstrapperEnabled>
@@ -64,6 +64,9 @@
6464
<Reference Include="Microsoft.Azure.SqlDatabase.ElasticScale.Client, Version=2.3.0.0, Culture=neutral, PublicKeyToken=b1e0ec4a89ab0a4a, processorArchitecture=MSIL">
6565
<HintPath>..\packages\Microsoft.Azure.SqlDatabase.ElasticScale.Client.2.3.0\lib\net451\Microsoft.Azure.SqlDatabase.ElasticScale.Client.dll</HintPath>
6666
</Reference>
67+
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
68+
<HintPath>..\packages\Newtonsoft.Json.13.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
69+
</Reference>
6770
<Reference Include="System" />
6871
<Reference Include="System.Core" />
6972
<Reference Include="System.Xml.Linq" />

SyncTokenGnerator/packages.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
33
<package id="Microsoft.Azure.SqlDatabase.ElasticScale.Client" version="2.3.0" targetFramework="net472" />
4+
<package id="Newtonsoft.Json" version="13.0.1" targetFramework="net472" />
45
</packages>

0 commit comments

Comments
 (0)