-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use Hangfire to schedule tweets. (#108)
Also, create unique index on the NameEntries_Name column.
- Loading branch information
Showing
20 changed files
with
212 additions
and
133 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
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 |
---|---|---|
@@ -1,29 +1,26 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Compile Remove="Interfaces\**" /> | ||
<EmbeddedResource Remove="Interfaces\**" /> | ||
<None Remove="Interfaces\**" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="BCrypt.Net-Next" Version="4.0.3" /> | ||
<PackageReference Include="Dapper" Version="2.1.35" /> | ||
<PackageReference Include="FluentValidation" Version="11.9.1" /> | ||
<PackageReference Include="MediatR" Version="12.2.0" /> | ||
<PackageReference Include="Microsoft.AspNetCore.Authentication" Version="2.2.0" /> | ||
<PackageReference Include="MongoDB.Driver" Version="2.23.1" /> | ||
<PackageReference Include="MySqlConnector" Version="2.3.7" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Core\Core.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> | ||
<ItemGroup> | ||
<FrameworkReference Include="Microsoft.AspNetCore.App" /> | ||
</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
18 changes: 8 additions & 10 deletions
18
Application/EventHandlers/PostPublishedNameCommandHandler.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 |
---|---|---|
@@ -1,25 +1,23 @@ | ||
using Application.Events; | ||
using Application.Services; | ||
using MediatR; | ||
using System.Collections.Concurrent; | ||
|
||
namespace Application.EventHandlers | ||
{ | ||
public class PostPublishedNameCommandHandler : INotificationHandler<PostPublishedNameCommand> | ||
{ | ||
private readonly ConcurrentQueue<PostPublishedNameCommand> _nameQueue; | ||
private readonly ITwitterService _twitterService; | ||
|
||
public PostPublishedNameCommandHandler(ConcurrentQueue<PostPublishedNameCommand> nameQueue) | ||
public PostPublishedNameCommandHandler( | ||
ITwitterService twitterService) | ||
{ | ||
_nameQueue = nameQueue; | ||
_twitterService = twitterService; | ||
|
||
} | ||
|
||
public Task Handle(PostPublishedNameCommand notification, CancellationToken cancellationToken) | ||
public async Task Handle(PostPublishedNameCommand notification, CancellationToken cancellationToken) | ||
{ | ||
// Enqueue the indexed name for processing by the BackgroundService | ||
_nameQueue.Enqueue(notification); | ||
|
||
// Return a completed task, so it doesn't block the main thread | ||
return Task.CompletedTask; | ||
await _twitterService.PostNewNameAsync(notification.Name, notification.Meaning, cancellationToken); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Application.Services | ||
{ | ||
public interface ITwitterService | ||
{ | ||
Task PostNewNameAsync(string name, string meaning, CancellationToken cancellationToken); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,7 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
</Project> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,6 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Core.Events | ||
namespace Core.Events | ||
{ | ||
public record NameIndexed(string Name) | ||
public record NameIndexed(string Name, string Meaning) | ||
{ | ||
} | ||
} |
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 |
---|---|---|
@@ -1,18 +1,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" /> | ||
<PackageReference Include="MongoDB.Driver" Version="2.23.1" /> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.1" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Core\Core.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using Hangfire; | ||
using Hangfire.Mongo; | ||
using MongoDB.Driver; | ||
using Hangfire.Mongo.Migration.Strategies; | ||
using Hangfire.Mongo.Migration.Strategies.Backup; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.AspNetCore.Builder; | ||
using Api.Utilities; | ||
|
||
namespace Infrastructure.Hangfire | ||
{ | ||
public static class DependencyInjection | ||
{ | ||
public static IServiceCollection SetupHangfire(this IServiceCollection services, string mongoConnectionString) | ||
{ | ||
var mongoUrlBuilder = new MongoUrlBuilder(mongoConnectionString); | ||
var mongoClient = new MongoClient(mongoUrlBuilder.ToMongoUrl()); | ||
|
||
services.AddHangfire(configuration => configuration | ||
.SetDataCompatibilityLevel(CompatibilityLevel.Version_180) | ||
.UseSimpleAssemblyNameTypeSerializer() | ||
.UseRecommendedSerializerSettings() | ||
.UseMongoStorage(mongoClient, mongoUrlBuilder.DatabaseName, new MongoStorageOptions | ||
{ | ||
MigrationOptions = new MongoMigrationOptions | ||
{ | ||
MigrationStrategy = new MigrateMongoMigrationStrategy(), | ||
BackupStrategy = new CollectionMongoBackupStrategy() | ||
}, | ||
Prefix = "hangfire.mongo", | ||
CheckConnection = true, | ||
CheckQueuedJobsStrategy = CheckQueuedJobsStrategy.TailNotificationsCollection | ||
}) | ||
); | ||
|
||
services.AddHangfireServer(serverOptions => | ||
{ | ||
serverOptions.ServerName = "Hangfire.Mongo server 1"; | ||
}); | ||
return services; | ||
} | ||
|
||
public static void UseHangfireDashboard(this IApplicationBuilder app, string dashboardPath) | ||
{ | ||
app.UseHangfireDashboard(dashboardPath, new DashboardOptions | ||
{ | ||
Authorization = [new HangfireAuthFilter()] | ||
}); | ||
} | ||
} | ||
} |
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,12 @@ | ||
using Hangfire.Dashboard; | ||
|
||
namespace Api.Utilities | ||
{ | ||
public class HangfireAuthFilter : IDashboardAuthorizationFilter | ||
{ | ||
public bool Authorize(DashboardContext context) | ||
{ | ||
return context.GetHttpContext().Request.Host.Host == "localhost"; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.