-
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.
Merge pull request #1 from fbarresi/main
Added Service Host project
- Loading branch information
Showing
15 changed files
with
418 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# This workflow will build a .NET project | ||
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-net | ||
|
||
name: .NET | ||
|
||
on: | ||
push: | ||
branches: [ "main" ] | ||
pull_request: | ||
branches: [ "main" ] | ||
|
||
jobs: | ||
build: | ||
|
||
runs-on: windows-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Setup .NET | ||
uses: actions/setup-dotnet@v3 | ||
with: | ||
dotnet-version: 7.0.x | ||
- name: Restore dependencies | ||
run: dotnet restore | ||
- name: Build | ||
run: dotnet build --configuration Release --no-restore | ||
- name: Publish ReadyToRun | ||
run: dotnet publish ImageCompressor\ImageCompressor.csproj -c Release -r win-x64 /p:AssemblyVersion=1.0.${{ github.run_number }} /p:FileVersion=1.0.${{ github.run_number }} /p:SelfContained=true /p:PublishReadyToRun=true /p:PublishSingleFile=true -o out | ||
- name: Compress ReadyToRun | ||
run: Compress-Archive out\ImageCompressor.exe ImageCompressor_ReadyToRun_v1.0.${{ github.run_number }}.zip | ||
- name: Publish Service ReadyToRun | ||
run: dotnet publish ImageCompressor.Service\ImageCompressor.Service.csproj -c Release -r win-x64 /p:AssemblyVersion=1.0.${{ github.run_number }} /p:FileVersion=1.0.${{ github.run_number }} /p:SelfContained=true /p:PublishReadyToRun=true /p:PublishSingleFile=true -o out.Service | ||
- name: Compress Service ReadyToRun | ||
run: Compress-Archive out.Service\ImageCompressor.Service.exe ImageCompressor.Service_ReadyToRun_v1.0.${{ github.run_number }}.zip | ||
- name: Create Release | ||
id: create_release | ||
uses: actions/create-release@v1 | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
with: | ||
tag_name: v1.0.${{ github.run_number }} | ||
release_name: v1.0.${{ github.run_number }} | ||
draft: false | ||
prerelease: true | ||
- name: Upload ReadyToRun | ||
id: upload-release-asset | ||
uses: actions/upload-release-asset@v1 | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
with: | ||
upload_url: ${{ steps.create_release.outputs.upload_url }} # This pulls from the CREATE RELEASE step above, referencing it's ID to get its outputs object, which include a `upload_url`. See this blog post for more info: https://jasonet.co/posts/new-features-of-github-actions/#passing-data-to-future-steps | ||
asset_path: ImageCompressor_ReadyToRun_v1.0.${{ github.run_number }}.zip | ||
asset_name: ImageCompressor_ReadyToRun_v1.0.${{ github.run_number }}.zip | ||
asset_content_type: application/zip | ||
- name: Upload ReadyToRun Service | ||
id: upload-release-asset2 | ||
uses: actions/upload-release-asset@v1 | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
with: | ||
upload_url: ${{ steps.create_release.outputs.upload_url }} # This pulls from the CREATE RELEASE step above, referencing it's ID to get its outputs object, which include a `upload_url`. See this blog post for more info: https://jasonet.co/posts/new-features-of-github-actions/#passing-data-to-future-steps | ||
asset_path: ImageCompressor.Service_ReadyToRun_v1.0.${{ github.run_number }}.zip | ||
asset_name: ImageCompressor.Service_ReadyToRun_v1.0.${{ github.run_number }}.zip | ||
asset_content_type: application/zip |
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 |
---|---|---|
|
@@ -360,4 +360,7 @@ MigrationBackup/ | |
.ionide/ | ||
|
||
# Fody - auto-generated XML schema | ||
FodyWeavers.xsd | ||
FodyWeavers.xsd | ||
|
||
#Rider | ||
.idea/ |
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,9 @@ | ||
namespace ImageCompressor.Service; | ||
|
||
public class CompressionTasks | ||
{ | ||
public string Name { get; set; } | ||
Check warning on line 5 in ImageCompressor.Service/CompressionTasks.cs GitHub Actions / build
Check warning on line 5 in ImageCompressor.Service/CompressionTasks.cs GitHub Actions / build
|
||
public TimeSpan InitialDelay { get; set; } = TimeSpan.FromSeconds(1); | ||
public TimeSpan Period { get; set; } = TimeSpan.FromMinutes(5); | ||
public CompressImagesSettings CompressionSettings { get; set; } | ||
Check warning on line 8 in ImageCompressor.Service/CompressionTasks.cs GitHub Actions / build
Check warning on line 8 in ImageCompressor.Service/CompressionTasks.cs GitHub Actions / build
|
||
} |
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,23 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Worker"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<UserSecretsId>dotnet-ImageCompressor.Service-303BD63E-8769-4D93-8BD9-EB337757F14E</UserSecretsId> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" /> | ||
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="7.0.1" /> | ||
<PackageReference Include="Serilog" Version="2.11.0" /> | ||
<PackageReference Include="Serilog.AspNetCore" Version="6.0.1" /> | ||
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.1" /> | ||
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" /> | ||
<PackageReference Include="System.Reactive" Version="6.0.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\ImageCompressor\ImageCompressor.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using ImageCompressor; | ||
using ImageCompressor.Service; | ||
using Microsoft.Extensions.Options; | ||
using Serilog; | ||
|
||
var configuration = new ConfigurationBuilder() | ||
.SetBasePath(Directory.GetCurrentDirectory()) | ||
.AddJsonFile("appsettings.json") | ||
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", true) | ||
.Build(); | ||
|
||
IHost host = Host.CreateDefaultBuilder(args) | ||
.ConfigureServices(services => | ||
{ | ||
services.AddOptions<SettingsRoot>() | ||
.BindConfiguration("Settings") // 👈 Bind the section | ||
.ValidateDataAnnotations() // 👈 Enable validation | ||
.ValidateOnStart(); // 👈 Validate on app start | ||
services.AddSingleton(resolver => resolver.GetRequiredService<IOptions<SettingsRoot>>().Value); | ||
}) | ||
.ConfigureServices(services => { services.AddHostedService<Worker>(); }) | ||
.UseSerilog((context, loggerConfiguration) => loggerConfiguration.ReadFrom.Configuration(configuration)) | ||
.UseWindowsService() | ||
.Build(); | ||
|
||
host.Run(); |
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 @@ | ||
{ | ||
"profiles": { | ||
"ImageCompressor.Service": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"environmentVariables": { | ||
"DOTNET_ENVIRONMENT": "Development" | ||
} | ||
} | ||
} | ||
} |
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,6 @@ | ||
namespace ImageCompressor.Service; | ||
|
||
public class SettingsRoot | ||
{ | ||
public List<CompressionTasks> CompressionTasks { get; set; } = new (); | ||
} |
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,67 @@ | ||
using System.Reactive.Disposables; | ||
using System.Reactive.Linq; | ||
|
||
namespace ImageCompressor.Service; | ||
|
||
public class Worker : BackgroundService | ||
{ | ||
protected virtual void Dispose(bool disposing) | ||
{ | ||
if (disposing) | ||
{ | ||
disposables.Dispose(); | ||
} | ||
} | ||
|
||
public sealed override void Dispose() | ||
{ | ||
Dispose(true); | ||
GC.SuppressFinalize(this); | ||
} | ||
|
||
private readonly ILogger<Worker> logger; | ||
private readonly SettingsRoot settings; | ||
|
||
private readonly CompositeDisposable disposables = new(); | ||
|
||
public Worker(ILogger<Worker> logger, SettingsRoot settings) | ||
{ | ||
this.logger = logger; | ||
this.settings = settings; | ||
} | ||
|
||
public override Task StartAsync(CancellationToken cancellationToken) | ||
{ | ||
foreach (var task in settings.CompressionTasks) | ||
{ | ||
logger.LogInformation("Setting up task {TaskName}...", task.Name); | ||
var validationResult = task.CompressionSettings.Validate(); | ||
logger.LogInformation("Validation result: {ValidationResult}: {ValidationMessage}", validationResult.Successful, validationResult.Message); | ||
if (validationResult.Successful) | ||
{ | ||
var subscrition = Observable.Timer(task.InitialDelay, task.Period) | ||
.Do(_ => logger.LogInformation("Executing task {TaskName}", task.Name)) | ||
.Do(_ => (new CompressImagesCommand()).ExecuteEmbedded(logger, task.CompressionSettings)) | ||
.Retry() | ||
.Subscribe(_ => {}, exception => logger.LogError(exception, "Error while executing task {TaskName}", task.Name)); | ||
disposables.Add(subscrition); | ||
|
||
} | ||
} | ||
return base.StartAsync(cancellationToken); | ||
} | ||
|
||
protected override async Task ExecuteAsync(CancellationToken stoppingToken) | ||
{ | ||
while (!stoppingToken.IsCancellationRequested) | ||
{ | ||
await Task.Delay(-1, stoppingToken); | ||
} | ||
} | ||
|
||
public override Task StopAsync(CancellationToken cancellationToken) | ||
{ | ||
disposables?.Dispose(); | ||
return base.StopAsync(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.Hosting.Lifetime": "Information" | ||
} | ||
} | ||
} |
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,32 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.Hosting.Lifetime": "Information" | ||
} | ||
}, | ||
"Serilog": { | ||
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ], | ||
"MinimumLevel": "Debug", | ||
"WriteTo": [ | ||
{ "Name": "Console" }, | ||
{ "Name": "File", "Args": { "path": "Logs/log.txt" } } | ||
] | ||
}, | ||
"Settings": { | ||
"CompressionTasks": [ | ||
{ | ||
"Name": "Compress", | ||
"CompressionSettings": { | ||
"SourcePath": "C:\\temp\\pictures", | ||
"TargetPath": "C:\\temp\\pictures\\out", | ||
"DeleteOriginal": true, | ||
"OverwriteExisting": true, | ||
"IncludeSubDirectories": true, | ||
"OutMode": "Jpeg", | ||
"MinAgeInDays": 30 | ||
} | ||
} | ||
] | ||
} | ||
} |
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
Oops, something went wrong.