1
0
mirror of https://github.com/bitwarden/server.git synced 2024-12-24 17:17:40 +01:00

port events processor over to webjobs sdk 3

This commit is contained in:
Kyle Spearrin 2019-02-26 11:31:37 -05:00
parent d6eeca3138
commit 0e756208e8
6 changed files with 71 additions and 39 deletions

View File

@ -39,14 +39,14 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Icons", "src\Icons\Icons.cs
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Events", "src\Events\Events.csproj", "{994DD611-F266-4BD3-8072-3B1B57267ED5}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EventsProcessor", "src\EventsProcessor\EventsProcessor.csproj", "{2235D24F-E607-47F4-81AD-BB4504ADF9C6}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Admin", "src\Admin\Admin.csproj", "{B131CEF3-89FB-4C90-ADB0-9E9C4246EB56}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Notifications", "src\Notifications\Notifications.csproj", "{28635027-20E5-42FA-B218-B6C878DE5350}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Core.Test", "test\Core.Test\Core.Test.csproj", "{8EF31E6C-400A-4174-8BE3-502B08FB10B5}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EventsProcessor", "src\EventsProcessor\EventsProcessor.csproj", "{79BB453F-D0D8-4DDF-9809-A405C56692BD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -91,10 +91,6 @@ Global
{994DD611-F266-4BD3-8072-3B1B57267ED5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{994DD611-F266-4BD3-8072-3B1B57267ED5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{994DD611-F266-4BD3-8072-3B1B57267ED5}.Release|Any CPU.Build.0 = Release|Any CPU
{2235D24F-E607-47F4-81AD-BB4504ADF9C6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2235D24F-E607-47F4-81AD-BB4504ADF9C6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2235D24F-E607-47F4-81AD-BB4504ADF9C6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2235D24F-E607-47F4-81AD-BB4504ADF9C6}.Release|Any CPU.Build.0 = Release|Any CPU
{B131CEF3-89FB-4C90-ADB0-9E9C4246EB56}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B131CEF3-89FB-4C90-ADB0-9E9C4246EB56}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B131CEF3-89FB-4C90-ADB0-9E9C4246EB56}.Release|Any CPU.ActiveCfg = Release|Any CPU
@ -107,6 +103,10 @@ Global
{8EF31E6C-400A-4174-8BE3-502B08FB10B5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8EF31E6C-400A-4174-8BE3-502B08FB10B5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8EF31E6C-400A-4174-8BE3-502B08FB10B5}.Release|Any CPU.Build.0 = Release|Any CPU
{79BB453F-D0D8-4DDF-9809-A405C56692BD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{79BB453F-D0D8-4DDF-9809-A405C56692BD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{79BB453F-D0D8-4DDF-9809-A405C56692BD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{79BB453F-D0D8-4DDF-9809-A405C56692BD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -121,10 +121,10 @@ Global
{66B0A682-658A-4A82-B606-A077A4871448} = {DD5BD056-4AAE-43EF-BBD2-0B569B8DA84E}
{9CF59342-3912-4B45-A2BA-0F173666586D} = {DD5BD056-4AAE-43EF-BBD2-0B569B8DA84D}
{994DD611-F266-4BD3-8072-3B1B57267ED5} = {DD5BD056-4AAE-43EF-BBD2-0B569B8DA84D}
{2235D24F-E607-47F4-81AD-BB4504ADF9C6} = {DD5BD056-4AAE-43EF-BBD2-0B569B8DA84D}
{B131CEF3-89FB-4C90-ADB0-9E9C4246EB56} = {DD5BD056-4AAE-43EF-BBD2-0B569B8DA84D}
{28635027-20E5-42FA-B218-B6C878DE5350} = {DD5BD056-4AAE-43EF-BBD2-0B569B8DA84D}
{8EF31E6C-400A-4174-8BE3-502B08FB10B5} = {DD5BD056-4AAE-43EF-BBD2-0B569B8DA84F}
{79BB453F-D0D8-4DDF-9809-A405C56692BD} = {DD5BD056-4AAE-43EF-BBD2-0B569B8DA84D}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {E01CBF68-2E20-425F-9EDB-E0A6510CA92F}

View File

@ -1,18 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk;Microsoft.NET.Sdk.Publish">
<Project Sdk="Microsoft.NET.Sdk;Microsoft.NET.Sdk.Publish">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net471</TargetFramework>
<TargetFramework>netcoreapp2.1</TargetFramework>
<RootNamespace>Bit.EventsProcessor</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.WebJobs" Version="2.2.0" />
<PackageReference Include="Microsoft.Azure.WebJobs" Version="3.0.4" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions" Version="3.0.1" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Storage" Version="3.0.3" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.2.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Core\Core.csproj" />
</ItemGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Configuration" />
<None Remove="appsettings.json" />
</ItemGroup>
<ItemGroup>
<Content Include="appsettings.json">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<None Update="Settings.job">

View File

@ -1,13 +1,13 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Bit.Core.Models.Data;
using Bit.Core.Services;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
@ -15,22 +15,22 @@ namespace Bit.EventsProcessor
{
public class Functions
{
private static IEventWriteService _eventWriteService;
private readonly IEventWriteService _eventWriteService;
static Functions()
public Functions(IConfiguration config)
{
var storageConnectionString = ConfigurationManager.ConnectionStrings["AzureWebJobsStorage"];
if(storageConnectionString == null || string.IsNullOrWhiteSpace(storageConnectionString.ConnectionString))
var storageConnectionString = config["AzureWebJobsStorage"];
if(string.IsNullOrWhiteSpace(storageConnectionString))
{
return;
}
var repo = new Core.Repositories.TableStorage.EventRepository(storageConnectionString.ConnectionString);
var repo = new Core.Repositories.TableStorage.EventRepository(storageConnectionString);
_eventWriteService = new RepositoryEventWriteService(repo);
}
public async static Task ProcessQueueMessageAsync([QueueTrigger("event")] string message,
TextWriter logger, CancellationToken cancellationToken)
public async Task ProcessQueueMessageAsync([QueueTrigger("event")] string message,
CancellationToken cancellationToken, ILogger logger)
{
if(_eventWriteService == null || message == null || message.Length == 0)
{
@ -39,6 +39,7 @@ namespace Bit.EventsProcessor
try
{
logger.LogInformation("Processing message.");
var events = new List<IEvent>();
var token = JToken.Parse(message);
@ -55,14 +56,20 @@ namespace Bit.EventsProcessor
}
await _eventWriteService.CreateManyAsync(events);
logger.LogInformation("Processed message.");
}
catch(JsonReaderException)
{
await logger.WriteLineAsync("JsonReaderException: Unable to parse message.");
logger.LogError("JsonReaderException: Unable to parse message.");
}
catch(JsonSerializationException)
{
await logger.WriteLineAsync("JsonSerializationException: Unable to serialize token.");
logger.LogError("JsonSerializationException: Unable to serialize token.");
}
catch(Exception e)
{
logger.LogError(e, "Exception occurred. " + e.Message);
throw e;
}
}
}

View File

@ -1,21 +1,40 @@
using Microsoft.Azure.WebJobs;
using System;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace Bit.EventsProcessor
{
public class Program
class Program
{
private static void Main()
static void Main(string[] args)
{
var config = new JobHostConfiguration();
if(config.IsDevelopment)
var builder = new HostBuilder();
builder.ConfigureWebJobs(b =>
{
config.UseDevelopmentSettings();
b.AddAzureStorageCoreServices();
b.AddAzureStorage(a =>
{
a.BatchSize = 5;
});
// Not working. ref: https://github.com/Azure/azure-webjobs-sdk/issues/1962
b.AddDashboardLogging();
});
builder.ConfigureLogging((context, b) =>
{
b.AddConsole();
b.SetMinimumLevel(LogLevel.Warning);
});
builder.ConfigureHostConfiguration(b =>
{
b.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
b.AddEnvironmentVariables();
});
var host = builder.Build();
using(host)
{
host.Run();
}
config.Queues.BatchSize = 5;
var host = new JobHost(config);
host.RunAndBlock();
}
}
}

View File

@ -1,5 +0,0 @@
{
"$schema": "http://schemastore.org/schemas/json/webjob-publish-settings.json",
"webJobName": "EventsProcessor",
"runMode": "Continuous"
}

View File

@ -0,0 +1,3 @@
{
"AzureWebJobsStorage": ""
}