Skip to content

Instantly share code, notes, and snippets.

View deadwards90's full-sized avatar

Daniel Edwards deadwards90

View GitHub Profile
public static class ServiceCollectionExtensions
{
/// <summary>
/// Adds a set of Azure Relays and an associated Request Hanlder.
/// </summary>
/// <typeparam name="T">The type you wish to be injected as the Request Handler for incoming messages</typeparam>
/// <param name="services">Service collection</param>
/// <param name="relayConnectionStrings">Array of relay connection strings</param>
/// <returns>Service Collection</returns>
public static IServiceCollection AddAzureRelayListeners<T> (this IServiceCollection services, params string[] relayConnectionStrings)
public class Worker : IHostedService
{
private readonly IEnumerable<IAzureRelayListener> _listeners;
private readonly ILogger _logger;
public Worker(IEnumerable<IAzureRelayListener> listeners, ILogger<Worker> logger)
{
_listeners = listeners;
_logger = logger;
}
public interface IAzureRelayListener
{
Task StartAsync();
Task StopAsync();
}
public class AzureRelayListener : IAzureRelayListener
{
private readonly HybridConnectionListener _listener;
private readonly IRequestHandler _requestHandler;
public class QueuedHostedService : BackgroundService
{
private readonly IJobStatusService _jobStatusService;
private readonly ILogger _logger;
public IBackgroundTaskQueue TaskQueue { get; }
public QueuedHostedService(IBackgroundTaskQueue taskQueue,
IJobStatusService jobStatusService,
ILogger<QueuedHostedService> logger)
{
public interface IBackgroundTaskQueue
{
void QueueBackgroundWorkItem(string jobId, Func<CancellationToken, Task> workItem);
Task<(string jobId, Func<CancellationToken, Task> func)> DequeueAsync(
CancellationToken cancellationToken);
}
public class BackgroundTaskQueue : IBackgroundTaskQueue
{
public interface IJobStatusService
{
void SetState(string jobId, State newState);
State GetState(string jobId);
}
public class JobStatusService : IJobStatusService
{
private readonly ConcurrentDictionary<string, State> _jobStatus = new ConcurrentDictionary<string, State>();
public class GatlingService
{
public async Task<RunResults> RunSimulation(RunSettings runSettings)
{
var runId = runSettings.RunId;
var gatlingStartInfo = new ProcessStartInfo(runSettings.GatlingPath)
{
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
public class FileService
{
public async Task<RunSettings> CreateRunFolders(Guid runId, Stream requestStream)
{
var gatlingHomeFolder = Environment.GetEnvironmentVariable("GATLING_HOME");
using (var memoryStream = new MemoryStream())
{
await requestStream.CopyToAsync(memoryStream);
await System.IO.File.WriteAllBytesAsync($"/tmp/{runId}.zip", memoryStream.ToArray());
[Route("startasync/{runId:guid}")]
[HttpPost]
public async Task<IActionResult> StartAsync(Guid runId)
{
if (ValidateQueryParameters(runId, out var badRequest)) return badRequest;
var runSettings = await _fileService.CreateRunFolders(runId, Request.Body);
_queue.QueueBackgroundWorkItem(runId.ToString(), async token =>
{
[Route("start/{runId:guid}")]
[HttpPost]
public async Task<IActionResult> Start(Guid runId, [FromQuery] bool returnReport)
{
if (ValidateQueryParameters(runId, out var badRequest)) return badRequest;
var runSettings = await _fileService.CreateRunFolders(runId, Request.Body);
if (returnReport)
{