1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-24 12:35:25 +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 EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Events", "src\Events\Events.csproj", "{994DD611-F266-4BD3-8072-3B1B57267ED5}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Events", "src\Events\Events.csproj", "{994DD611-F266-4BD3-8072-3B1B57267ED5}"
EndProject 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}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Admin", "src\Admin\Admin.csproj", "{B131CEF3-89FB-4C90-ADB0-9E9C4246EB56}"
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Notifications", "src\Notifications\Notifications.csproj", "{28635027-20E5-42FA-B218-B6C878DE5350}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Notifications", "src\Notifications\Notifications.csproj", "{28635027-20E5-42FA-B218-B6C878DE5350}"
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Core.Test", "test\Core.Test\Core.Test.csproj", "{8EF31E6C-400A-4174-8BE3-502B08FB10B5}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Core.Test", "test\Core.Test\Core.Test.csproj", "{8EF31E6C-400A-4174-8BE3-502B08FB10B5}"
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EventsProcessor", "src\EventsProcessor\EventsProcessor.csproj", "{79BB453F-D0D8-4DDF-9809-A405C56692BD}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU 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}.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.ActiveCfg = Release|Any CPU
{994DD611-F266-4BD3-8072-3B1B57267ED5}.Release|Any CPU.Build.0 = 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.ActiveCfg = Debug|Any CPU
{B131CEF3-89FB-4C90-ADB0-9E9C4246EB56}.Debug|Any CPU.Build.0 = 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 {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}.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.ActiveCfg = Release|Any CPU
{8EF31E6C-400A-4174-8BE3-502B08FB10B5}.Release|Any CPU.Build.0 = 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 EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
@ -121,10 +121,10 @@ Global
{66B0A682-658A-4A82-B606-A077A4871448} = {DD5BD056-4AAE-43EF-BBD2-0B569B8DA84E} {66B0A682-658A-4A82-B606-A077A4871448} = {DD5BD056-4AAE-43EF-BBD2-0B569B8DA84E}
{9CF59342-3912-4B45-A2BA-0F173666586D} = {DD5BD056-4AAE-43EF-BBD2-0B569B8DA84D} {9CF59342-3912-4B45-A2BA-0F173666586D} = {DD5BD056-4AAE-43EF-BBD2-0B569B8DA84D}
{994DD611-F266-4BD3-8072-3B1B57267ED5} = {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} {B131CEF3-89FB-4C90-ADB0-9E9C4246EB56} = {DD5BD056-4AAE-43EF-BBD2-0B569B8DA84D}
{28635027-20E5-42FA-B218-B6C878DE5350} = {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} {8EF31E6C-400A-4174-8BE3-502B08FB10B5} = {DD5BD056-4AAE-43EF-BBD2-0B569B8DA84F}
{79BB453F-D0D8-4DDF-9809-A405C56692BD} = {DD5BD056-4AAE-43EF-BBD2-0B569B8DA84D}
EndGlobalSection EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {E01CBF68-2E20-425F-9EDB-E0A6510CA92F} 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> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>net471</TargetFramework> <TargetFramework>netcoreapp2.1</TargetFramework>
<RootNamespace>Bit.EventsProcessor</RootNamespace> <RootNamespace>Bit.EventsProcessor</RootNamespace>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <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>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Core\Core.csproj" /> <ProjectReference Include="..\Core\Core.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Reference Include="System" /> <None Remove="appsettings.json" />
<Reference Include="System.Configuration" /> </ItemGroup>
<ItemGroup>
<Content Include="appsettings.json">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Update="Settings.job"> <None Update="Settings.job">

View File

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