-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New project added to help keep the azure storage cleaned up for performance purposes.
- Loading branch information
1 parent
d41ef80
commit 4ee6f1f
Showing
152 changed files
with
322,084 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
Postworthy.Tasks.AzureCleanUp/Postworthy.Tasks.AzureCleanUp.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> | ||
<PropertyGroup> | ||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | ||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> | ||
<ProjectGuid>{F2DC0FDE-3BB6-485C-9E17-A6B53023AEB6}</ProjectGuid> | ||
<OutputType>Exe</OutputType> | ||
<AppDesignerFolder>Properties</AppDesignerFolder> | ||
<RootNamespace>Postworthy.Tasks.AzureCleanUp</RootNamespace> | ||
<AssemblyName>Postworthy.Tasks.AzureCleanUp</AssemblyName> | ||
<TargetFrameworkVersion>v4.5.1</TargetFrameworkVersion> | ||
<FileAlignment>512</FileAlignment> | ||
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> | ||
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> | ||
<PlatformTarget>AnyCPU</PlatformTarget> | ||
<DebugSymbols>true</DebugSymbols> | ||
<DebugType>full</DebugType> | ||
<Optimize>false</Optimize> | ||
<OutputPath>bin\Debug\</OutputPath> | ||
<DefineConstants>DEBUG;TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> | ||
<PlatformTarget>AnyCPU</PlatformTarget> | ||
<DebugType>pdbonly</DebugType> | ||
<Optimize>true</Optimize> | ||
<OutputPath>bin\Release\</OutputPath> | ||
<DefineConstants>TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Reference Include="Microsoft.Data.Edm, Version=5.6.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"> | ||
<SpecificVersion>False</SpecificVersion> | ||
<HintPath>..\packages\Microsoft.Data.Edm.5.6.2\lib\net40\Microsoft.Data.Edm.dll</HintPath> | ||
</Reference> | ||
<Reference Include="Microsoft.Data.OData, Version=5.6.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"> | ||
<SpecificVersion>False</SpecificVersion> | ||
<HintPath>..\packages\Microsoft.Data.OData.5.6.2\lib\net40\Microsoft.Data.OData.dll</HintPath> | ||
</Reference> | ||
<Reference Include="Microsoft.Data.Services.Client, Version=5.6.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"> | ||
<SpecificVersion>False</SpecificVersion> | ||
<HintPath>..\packages\Microsoft.Data.Services.Client.5.6.2\lib\net40\Microsoft.Data.Services.Client.dll</HintPath> | ||
</Reference> | ||
<Reference Include="Microsoft.WindowsAzure.Configuration"> | ||
<HintPath>..\packages\Microsoft.WindowsAzure.ConfigurationManager.1.8.0.0\lib\net35-full\Microsoft.WindowsAzure.Configuration.dll</HintPath> | ||
</Reference> | ||
<Reference Include="Microsoft.WindowsAzure.Storage"> | ||
<HintPath>..\packages\WindowsAzure.Storage.4.3.0\lib\net40\Microsoft.WindowsAzure.Storage.dll</HintPath> | ||
</Reference> | ||
<Reference Include="Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL"> | ||
<HintPath>..\packages\Newtonsoft.Json.5.0.8\lib\net45\Newtonsoft.Json.dll</HintPath> | ||
</Reference> | ||
<Reference Include="System" /> | ||
<Reference Include="System.configuration" /> | ||
<Reference Include="System.Core" /> | ||
<Reference Include="System.Spatial, Version=5.6.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"> | ||
<SpecificVersion>False</SpecificVersion> | ||
<HintPath>..\packages\System.Spatial.5.6.2\lib\net40\System.Spatial.dll</HintPath> | ||
</Reference> | ||
<Reference Include="System.Xml.Linq" /> | ||
<Reference Include="System.Data.DataSetExtensions" /> | ||
<Reference Include="Microsoft.CSharp" /> | ||
<Reference Include="System.Data" /> | ||
<Reference Include="System.Xml" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Compile Include="Program.cs" /> | ||
<Compile Include="Properties\AssemblyInfo.cs" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<None Include="App.config" /> | ||
<None Include="packages.config" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\Postworthy.Models\Postworthy.Models.csproj"> | ||
<Project>{1628e67a-0233-4f42-a3f7-3ff4a121f9a8}</Project> | ||
<Name>Postworthy.Models</Name> | ||
</ProjectReference> | ||
</ItemGroup> | ||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> | ||
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" /> | ||
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild"> | ||
<PropertyGroup> | ||
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText> | ||
</PropertyGroup> | ||
<Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.targets'))" /> | ||
</Target> | ||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. | ||
Other similar extension points exist, see Microsoft.Common.targets. | ||
<Target Name="BeforeBuild"> | ||
</Target> | ||
<Target Name="AfterBuild"> | ||
</Target> | ||
--> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
using Microsoft.WindowsAzure.Storage; | ||
using Microsoft.WindowsAzure.Storage.Blob; | ||
using Microsoft.WindowsAzure.Storage.RetryPolicies; | ||
using Postworthy.Models.Repository; | ||
using Postworthy.Models.Twitter; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Configuration; | ||
using System.IO; | ||
using System.IO.Compression; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Postworthy.Tasks.AzureCleanUp | ||
{ | ||
class Program | ||
{ | ||
|
||
static void Main(string[] args) | ||
{ | ||
var connectionString = ConfigurationManager.AppSettings["AzureStorageConnectionString"]; | ||
if (string.IsNullOrEmpty(connectionString)) | ||
throw new Exception("Config Section 'appSettings' missing AzureStorageConnectionString value!"); | ||
|
||
var storageAccount = CloudStorageAccount.Parse(connectionString); | ||
var blobClient = storageAccount.CreateCloudBlobClient(); | ||
|
||
var deleteTasks = new List<Task>(10000000); | ||
|
||
Action<Task> delete = t => | ||
{ | ||
lock (deleteTasks) | ||
{ | ||
deleteTasks.Add(t); | ||
} | ||
Console.Clear(); | ||
Console.WriteLine("Deleting " + deleteTasks.Count + " items"); | ||
}; | ||
|
||
var version = TwitterModel.VERSION; | ||
|
||
bool cleanVersion = args.Any(a => a.ToLower() == "version"); | ||
bool shrink = args.Any(a => a.ToLower().StartsWith("shrink")); | ||
int newSize = args.Where(a => a.ToLower().StartsWith("shrink")).Select(x => x == "shrink" ? 500 : int.Parse(x.Replace("shrink", ""))).FirstOrDefault(); | ||
|
||
blobClient.ListContainers() | ||
//.Take(1) //Short circuit for testing | ||
.ToList().AsParallel().ForAll(c => | ||
{ | ||
var index = c.GetDirectoryReference("Index"); | ||
foreach (var b in index.ListBlobs().Where(x => x is CloudBlockBlob).Cast<CloudBlockBlob>()) | ||
{ | ||
if (cleanVersion && !b.Name.Contains(version)) | ||
{ | ||
//Delete Index | ||
var i = c.GetBlockBlobReference(b.Name); | ||
delete(i.DeleteIfExistsAsync()); | ||
//Delete all Tweets | ||
var d = c.GetDirectoryReference(b.Name.Split('/').Last()); | ||
foreach (var t in d.ListBlobs().Where(x => x is CloudBlockBlob).Cast<CloudBlockBlob>()) | ||
{ | ||
delete(t.DeleteIfExistsAsync()); | ||
} | ||
} | ||
if (shrink && newSize > 0 && b.Name.Contains(version)) | ||
{ | ||
//Get Storage Index | ||
var i = c.GetBlockBlobReference(b.Name); | ||
var storageIndex = Newtonsoft.Json.JsonConvert.DeserializeObject<StorageEntityIndex>(DownloadBlob(i)); | ||
if (storageIndex.EntityKeys.Count > newSize) | ||
{ | ||
//Delete extra Tweets | ||
var d = c.GetDirectoryReference(b.Name.Split('/').Last()); | ||
foreach (var t in d.ListBlobs().Where(x => x is CloudBlockBlob).Cast<CloudBlockBlob>().OrderByDescending(x => x.Properties.LastModified).Skip(newSize)) | ||
{ | ||
storageIndex.EntityKeys.Remove(t.Name.Split('/').Last()); | ||
delete(t.DeleteIfExistsAsync()); | ||
} | ||
//Update Storage Index | ||
UploadBlob(i, storageIndex); | ||
} | ||
} | ||
} | ||
}); | ||
|
||
Console.WriteLine("Waiting on all tasks to complete"); | ||
Task.WaitAll(deleteTasks.ToArray()); | ||
} | ||
|
||
private static string DownloadBlob(CloudBlockBlob blob) | ||
{ | ||
using (var stream = new MemoryStream()) | ||
{ | ||
StreamReader reader; | ||
try | ||
{ | ||
blob.DownloadToStream(stream, options: new BlobRequestOptions() | ||
{ | ||
RetryPolicy = new LinearRetry(TimeSpan.FromSeconds(5), 3) | ||
}); | ||
} | ||
catch (StorageException se) | ||
{ | ||
return ""; | ||
} | ||
try | ||
{ | ||
stream.Seek(0, 0); | ||
reader = new StreamReader(new GZipStream(stream, CompressionMode.Decompress)); | ||
return reader.ReadToEnd(); | ||
} | ||
catch | ||
{ | ||
stream.Seek(0, 0); | ||
reader = new StreamReader(stream); | ||
return reader.ReadToEnd(); | ||
} | ||
} | ||
} | ||
|
||
private static void UploadBlob(CloudBlockBlob blob, RepositoryEntity obj) | ||
{ | ||
using (var streamCompressed = new MemoryStream()) | ||
{ | ||
using (var gzip = new GZipStream(streamCompressed, CompressionMode.Compress)) | ||
{ | ||
var data = Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(obj)); | ||
gzip.Write(data, 0, data.Length); | ||
gzip.Flush(); | ||
gzip.Close(); | ||
|
||
using (var streamOut = new MemoryStream(streamCompressed.ToArray())) | ||
{ | ||
blob.UploadFromStream(streamOut); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private class StorageEntityIndex : RepositoryEntity | ||
{ | ||
public const string DIRECTORY_KEY = "Index"; | ||
public string Key { get; set; } | ||
public StorageEntityIndex() | ||
{ | ||
EntityKeys = new List<string>(); | ||
} | ||
|
||
public StorageEntityIndex(string key) | ||
{ | ||
this.Key = key; | ||
EntityKeys = new List<string>(); | ||
} | ||
|
||
public StorageEntityIndex(string key, List<string> EntityKeys) | ||
{ | ||
this.Key = key; | ||
this.EntityKeys = EntityKeys; | ||
} | ||
|
||
public List<string> EntityKeys { get; set; } | ||
|
||
public override string UniqueKey | ||
{ | ||
get { return Key; } | ||
} | ||
|
||
public override bool IsEqual(RepositoryEntity other) | ||
{ | ||
return this.UniqueKey == other.UniqueKey; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System.Reflection; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
|
||
// General Information about an assembly is controlled through the following | ||
// set of attributes. Change these attribute values to modify the information | ||
// associated with an assembly. | ||
[assembly: AssemblyTitle("Postworthy.Tasks.AzureCleanUp")] | ||
[assembly: AssemblyDescription("")] | ||
[assembly: AssemblyConfiguration("")] | ||
[assembly: AssemblyCompany("")] | ||
[assembly: AssemblyProduct("Postworthy.Tasks.AzureCleanUp")] | ||
[assembly: AssemblyCopyright("Copyright © 2015")] | ||
[assembly: AssemblyTrademark("")] | ||
[assembly: AssemblyCulture("")] | ||
|
||
// Setting ComVisible to false makes the types in this assembly not visible | ||
// to COM components. If you need to access a type in this assembly from | ||
// COM, set the ComVisible attribute to true on that type. | ||
[assembly: ComVisible(false)] | ||
|
||
// The following GUID is for the ID of the typelib if this project is exposed to COM | ||
[assembly: Guid("b6f9d46d-86e4-47b3-b232-b48e0df6357d")] | ||
|
||
// Version information for an assembly consists of the following four values: | ||
// | ||
// Major Version | ||
// Minor Version | ||
// Build Number | ||
// Revision | ||
// | ||
// You can specify all the values or you can default the Build and Revision Numbers | ||
// by using the '*' as shown below: | ||
// [assembly: AssemblyVersion("1.0.*")] | ||
[assembly: AssemblyVersion("1.0.0.0")] | ||
[assembly: AssemblyFileVersion("1.0.0.0")] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<packages> | ||
<package id="Microsoft.Data.Edm" version="5.6.2" targetFramework="net451" /> | ||
<package id="Microsoft.Data.OData" version="5.6.2" targetFramework="net451" /> | ||
<package id="Microsoft.Data.Services.Client" version="5.6.2" targetFramework="net451" /> | ||
<package id="Microsoft.WindowsAzure.ConfigurationManager" version="1.8.0.0" targetFramework="net451" /> | ||
<package id="Newtonsoft.Json" version="5.0.8" targetFramework="net451" /> | ||
<package id="System.Spatial" version="5.6.2" targetFramework="net451" /> | ||
<package id="WindowsAzure.Storage" version="4.3.0" targetFramework="net451" /> | ||
</packages> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Oops, something went wrong.