ruf*_*ufo 6 c# linux systemd .net-core
我有一个 .NET core 3.1“worker”服务(例如https://devblogs.microsoft.com/dotnet/net-core-and-systemd/)。
我正在使用 FileSystemWatcher 类来监视文件夹中的新文件并触发它们的进程。该服务在 Windows 中按预期工作(作为服务并将其作为控制台程序运行),并且在 Linux 中直接在终端中运行时也可以工作。
当它作为守护进程 ( sudo systemctl start {myservice}.service) 启动时,它会启动,但不会监视该文件夹。
低于我的工人阶级。
如果我扩大系统中手表的最大数量,它就可以工作。
须藤纳米 /etc/sysctl.conf
fs.inotify.max_user_watches=1048576
为什么我需要增加最大手表数?请注意,我正在监视的文件夹只有大约 100 个文件(默认值约为 8192 个最多手表)。
我担心的是,我对这个类有一些不理解的地方,所以我为手表的最大数量设置了一个任意高的数字,而不知道何时会达到这个值或为什么。另外,FileSystemWatcher 似乎完全不知道发生了什么,所以我的程序对此无能为力,因为它不知道。
public class Worker : BackgroundService
{
private readonly ILogger<Worker> _logger;
private FileSystemWatcher _fileSystemWatcher;
public Worker(ILogger<Worker> logger)
{
_logger = logger;
}
public override Task StartAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("Service startup.");
fileSystemWatcher = new FileSystemWatcher(@"/myfolder/myfolder2");
_fileSystemWatcher.IncludeSubdirectories = true;
_fileSystemWatcher.Filter = "*.xml";
_fileSystemWatcher.Created += (e, args) => _logger.LogWarning("StartAsync watcher: {0}", args.FullPath);
_fileSystemWatcher.EnableRaisingEvents = true;
return base.StartAsync(cancellationToken);
}
public override async Task StopAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("Stopping Service");
await base.StopAsync(cancellationToken);
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
_logger.LogInformation("Service Execute.");
while (!stoppingToken.IsCancellationRequested)
{
_logger.LogInformation("Keep alive: {time}", DateTimeOffset.Now);
await Task.Delay(5*60*1000, stoppingToken);
}
_logger.LogInformation("Service Execute stopped.");
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3799 次 |
| 最近记录: |