-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added AzureQueue provider to collect and relay messages created on a …
…website (#280) Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
9197e98
commit 40f4592
Showing
15 changed files
with
180 additions
and
18 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
11 changes: 11 additions & 0 deletions
11
src/TagzApp.Providers.AzureQueue/AzureQueueConfiguration.cs
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,11 @@ | ||
namespace TagzApp.Providers.AzureQueue; | ||
|
||
public class AzureQueueConfiguration | ||
{ | ||
|
||
public string QueueConnectionString { get; set; } = string.Empty; | ||
|
||
public bool Activated { get; set; } | ||
|
||
} | ||
|
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,77 @@ | ||
using Azure.Storage.Queues; | ||
using System.Text.Json; | ||
|
||
namespace TagzApp.Providers.AzureQueue; | ||
|
||
public class AzureQueueProvider : ISocialMediaProvider | ||
{ | ||
private const string QueueName = "tagzapp-content"; | ||
private readonly AzureQueueConfiguration _Configuration; | ||
private QueueClient _Client; | ||
private SocialMediaStatus _Status = SocialMediaStatus.Unhealthy; | ||
private string _StatusMessage = "Not started"; | ||
|
||
public string Id => "WEBSITE"; | ||
public string DisplayName => "Website"; | ||
public string DllName { get { return "AzureQueue"; } } | ||
public string Description => "Q+A submitted through a website form"; | ||
public TimeSpan NewContentRetrievalFrequency => TimeSpan.FromSeconds(5); | ||
|
||
public AzureQueueProvider(AzureQueueConfiguration configuration) | ||
Check warning on line 20 in src/TagzApp.Providers.AzureQueue/AzureQueueProvider.cs GitHub Actions / build
|
||
{ | ||
_Configuration = configuration; | ||
} | ||
|
||
|
||
public async Task<IEnumerable<Content>> GetContentForHashtag(Hashtag tag, DateTimeOffset since) | ||
{ | ||
|
||
var messageResponse = await _Client.ReceiveMessagesAsync(maxMessages: 10); | ||
if (!messageResponse.Value.Any()) return Enumerable.Empty<Content>(); | ||
|
||
var outList = new List<Content>(); | ||
|
||
foreach (var msg in messageResponse.Value) | ||
{ | ||
|
||
var content = JsonSerializer.Deserialize<Content>(msg.Body.ToStream()); | ||
if (content is not null) | ||
{ | ||
content.HashtagSought = tag.Text.ToLowerInvariant(); | ||
outList.Add(content); | ||
await _Client.DeleteMessageAsync(msg.MessageId, msg.PopReceipt); | ||
} | ||
|
||
} | ||
|
||
return outList; | ||
|
||
|
||
} | ||
|
||
public Task<(SocialMediaStatus Status, string Message)> GetHealth() | ||
{ | ||
return Task.FromResult((_Status, _StatusMessage)); | ||
} | ||
|
||
public async Task StartAsync() | ||
{ | ||
|
||
_Client = new QueueClient(_Configuration.QueueConnectionString, QueueName); | ||
|
||
try | ||
{ | ||
await _Client.CreateIfNotExistsAsync(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
_Status = SocialMediaStatus.Unhealthy; | ||
_StatusMessage = $"Unable to start a connection to the Azure Queue: {ex.Message}"; | ||
return; | ||
} | ||
|
||
_Status = SocialMediaStatus.Healthy; | ||
_StatusMessage = "Connected"; | ||
|
||
} | ||
} |
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,40 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using TagzApp.Communication; | ||
|
||
namespace TagzApp.Providers.AzureQueue; | ||
|
||
public class StartAzureQueue : BaseConfigurationProvider, IConfigureProvider | ||
{ | ||
|
||
private AzureQueueConfiguration _azureQueueConfiguration; | ||
|
||
public StartAzureQueue(IProviderConfigurationRepository providerConfigurationRepository) : base(providerConfigurationRepository) | ||
Check warning on line 11 in src/TagzApp.Providers.AzureQueue/StartAzureQueue.cs GitHub Actions / build
|
||
{ | ||
} | ||
|
||
public async Task<IServiceCollection> RegisterServices(IServiceCollection services, CancellationToken cancellationToken = default) | ||
{ | ||
await LoadConfigurationValuesAsync("WEBSITE", cancellationToken); | ||
|
||
services.AddSingleton(_azureQueueConfiguration ?? new()); | ||
services.AddTransient<ISocialMediaProvider, AzureQueueProvider>(); | ||
return services; | ||
} | ||
|
||
protected override void MapConfigurationValues(ProviderConfiguration providerConfiguration) | ||
{ | ||
|
||
var config = providerConfiguration.ConfigurationSettings; | ||
|
||
if (config != null) | ||
{ | ||
_azureQueueConfiguration = new() | ||
{ | ||
Activated = providerConfiguration.Activated, | ||
QueueConnectionString = config["QueueConnectionString"] ?? string.Empty | ||
}; | ||
} | ||
|
||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
src/TagzApp.Providers.AzureQueue/TagzApp.Providers.AzureQueue.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,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Azure.Storage.Queues" Version="12.16.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\TagzApp.Common\TagzApp.Common.csproj" /> | ||
<ProjectReference Include="..\TagzApp.Communication\TagzApp.Communication.csproj" /> | ||
</ItemGroup> | ||
|
||
</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
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
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
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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