1
0
mirror of https://github.com/bitwarden/server.git synced 2025-02-08 00:31:27 +01:00
bitwarden-server/src/EventsProcessor/Functions.cs

64 lines
2.1 KiB
C#
Raw Normal View History

2017-12-08 21:02:54 +01:00
using System;
2017-12-08 22:03:20 +01:00
using System.Collections.Generic;
using System.Configuration;
2017-12-08 21:02:54 +01:00
using System.IO;
using System.Threading;
using System.Threading.Tasks;
2017-12-08 22:03:20 +01:00
using Bit.Core.Models.Data;
using Bit.Core.Services;
2017-12-08 21:02:54 +01:00
using Microsoft.Azure.WebJobs;
2017-12-08 22:03:20 +01:00
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
2017-12-08 21:02:54 +01:00
namespace Bit.EventsProcessor
{
public class Functions
{
2017-12-08 22:03:20 +01:00
private static IEventWriteService _eventWriteService;
static Functions()
{
var storageConnectionString = ConfigurationManager.ConnectionStrings["AzureWebJobsStorage"];
if(storageConnectionString == null || string.IsNullOrWhiteSpace(storageConnectionString.ConnectionString))
{
return;
}
var repo = new Core.Repositories.TableStorage.EventRepository(storageConnectionString.ConnectionString);
_eventWriteService = new RepositoryEventWriteService(repo);
}
public async static Task ProcessQueueMessageAsync([QueueTrigger("event")] string message,
TextWriter logger, CancellationToken token)
2017-12-08 21:02:54 +01:00
{
2017-12-08 22:03:20 +01:00
if(_eventWriteService == null || message == null || message.Length == 0)
{
return;
}
try
{
var jToken = JToken.Parse(message);
if(jToken is JArray)
{
var entities = jToken.ToObject<IList<EventTableEntity>>();
await _eventWriteService.CreateManyAsync(entities);
}
else if(jToken is JObject)
{
var entity = jToken.ToObject<EventTableEntity>();
await _eventWriteService.CreateAsync(entity);
}
}
catch(JsonReaderException)
{
await logger.WriteLineAsync("JsonReaderException: Unable to parse message.");
}
catch(JsonSerializationException)
{
await logger.WriteLineAsync("JsonSerializationException: Unable to serialize token.");
}
2017-12-08 21:02:54 +01:00
}
}
}