Created
March 22, 2023 21:31
-
-
Save allenmichael/507da07a564f58b5f551e55e4840c825 to your computer and use it in GitHub Desktop.
All the resources needed to create a .NET worker app to run on Amazon Linux through systemd
This file contains hidden or 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
<Project Sdk="Microsoft.NET.Sdk.Worker"> | |
<PropertyGroup> | |
<OutputType>Exe</OutputType> | |
<TargetFramework>net7.0</TargetFramework> | |
<ImplicitUsings>enable</ImplicitUsings> | |
<Nullable>enable</Nullable> | |
<PublishSingleFile>true</PublishSingleFile> | |
<SelfContained>true</SelfContained> | |
<PublishTrimmed>true</PublishTrimmed> | |
</PropertyGroup> | |
<ItemGroup> | |
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.0" /> | |
<PackageReference Include="Microsoft.Extensions.Hosting.Systemd" Version="7.0.0" /> | |
</ItemGroup> | |
</Project> |
This file contains hidden or 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
[Unit] | |
Description=Continuously running daemon from the `dotnet new worker` template | |
[Service] | |
Type=notify | |
# the self-contained, single-file executable that systemd runs to start the service | |
ExecStart=/home/ec2-user/DaemonWorker/DaemonWorker | |
# used to identify logs when using journalctl | |
SyslogIdentifier=DaemonWorker | |
# don't forget to use chmod +x /srv/DaemonWorker/DaemonWorker add execution permissions to the executable file | |
User=ec2-user | |
# restart if crashes | |
Restart=always | |
RestartSec=5 | |
[Install] | |
WantedBy=multi-user.target |
This file contains hidden or 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
# Assumed install within Amazon Linux environment, your commands might vary based on OS | |
dotnet publish -c Release -r linux-x64 | |
mkdir -p /home/ec2-user/DaemonWorker | |
cp ./bin/Release/net7.0/linux-x64/publish/DaemonWorker /home/ec2-user/DaemonWorker | |
chmod u+x /home/ec2-user/DaemonWorker/DaemonWorker | |
sudo cp DaemonWorker.service /etc/systemd/system/DaemonWorker.service | |
sudo systemctl daemon-reload | |
sudo systemctl start DaemonWorker | |
systemctl status DaemonWorker | |
journalctl -u DaemonWorker -f |
This file contains hidden or 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
using DaemonWorker; | |
IHost host = Host.CreateDefaultBuilder(args) | |
.UseSystemd() | |
.ConfigureServices(services => | |
{ | |
services.AddHostedService<Worker>(); | |
}) | |
.Build(); | |
host.Run(); |
This file contains hidden or 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
{ | |
"runtimeOptions": { | |
"configProperties": { | |
"System.Globalization.Invariant": true | |
} | |
} | |
} |
This file contains hidden or 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
# Assumed uninstall within Amazon Linux environment, your commands might vary based on OS | |
sudo systemctl stop DaemonWorker | |
sudo systemctl disable DaemonWorker | |
sudo rm /etc/systemd/system/DaemonWorker.service | |
rm -r /home/ec2-user/DaemonWorker | |
sudo systemctl daemon-reload | |
sudo systemctl reset-failed DaemonWorker |
This file contains hidden or 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
namespace DaemonWorker; | |
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) | |
{ | |
var time = DateTime.Now; | |
_logger.LogInformation($"Checking for the daemon hour: {time}"); | |
if (time.Hour >= 0 && time.Hour <= 5 || time.Hour >= 21 && time.Hour <= 23) | |
{ | |
_logger.LogCritical($"It's time for night of the daemons!: {time}"); | |
_logger.LogWarning($"What a horrible night to have a curse: {time}"); | |
} | |
else | |
{ | |
_logger.LogWarning($"The morning sun has vanquished the horrible night: {time}"); | |
} | |
await Task.Delay(5000, stoppingToken); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment