This commit is contained in:
Matias Pou 2026-01-06 13:59:38 -03:00
parent bcb8a3fd5a
commit 853e92a07e
10 changed files with 687 additions and 0 deletions

27
src/dotnet.worker.sln Normal file
View file

@ -0,0 +1,27 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.2.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "test.worker", ".worker\test.worker.csproj", "{74107446-02DC-BE26-B792-8CE657A1C25E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{74107446-02DC-BE26-B792-8CE657A1C25E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{74107446-02DC-BE26-B792-8CE657A1C25E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{74107446-02DC-BE26-B792-8CE657A1C25E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{74107446-02DC-BE26-B792-8CE657A1C25E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{74107446-02DC-BE26-B792-8CE657A1C25E} = {827E0CD3-B72D-47B6-A68D-7590B98EB39B}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {099751BD-F2A1-4C29-9C73-DA04A3F0BCE3}
EndGlobalSection
EndGlobal

15
src/worker/Dockerfile Normal file
View file

@ -0,0 +1,15 @@
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build
WORKDIR /App
# Copy everything
COPY . ./
# Restore as distinct layers
RUN dotnet restore
# Build and publish a release
RUN dotnet publish -o out
# Build runtime image
FROM mcr.microsoft.com/dotnet/aspnet:9.0
WORKDIR /App
COPY --from=build /App/out .
ENTRYPOINT ["dotnet", "test.worker.dll"]

13
src/worker/Program.cs Normal file
View file

@ -0,0 +1,13 @@
namespace test.worker;
public class Program
{
public static void Main(string[] args)
{
var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddHostedService<Worker>();
var host = builder.Build();
host.Run();
}
}

View file

@ -0,0 +1,12 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"test.worker": {
"commandName": "Project",
"dotnetRunMessages": true,
"environmentVariables": {
"DOTNET_ENVIRONMENT": "Development"
}
}
}
}

23
src/worker/Worker.cs Normal file
View file

@ -0,0 +1,23 @@
namespace test.worker;
public class Worker : BackgroundService
{
private readonly ILogger<Worker> _logger;
public Worker(ILogger<Worker> logger)
{
_logger = logger;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
if (_logger.IsEnabled(LogLevel.Information))
{
_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
}
await Task.Delay(1000, stoppingToken);
}
}
}

View file

@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}

View file

@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}

View file

@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk.Worker">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>dotnet-test.worker-155cc199-70a0-44da-994f-61f90df99323</UserSecretsId>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.6" />
</ItemGroup>
</Project>