Radarr/src/NzbDrone.Common/Processes/PidFileProvider.cs

45 lines
1.1 KiB
C#
Raw Normal View History

2018-11-23 07:03:32 +00:00
using System;
using System.IO;
using NLog;
using NzbDrone.Common.EnvironmentInfo;
2018-11-23 07:03:32 +00:00
using NzbDrone.Common.Exceptions;
namespace NzbDrone.Common.Processes
{
public interface IProvidePidFile
{
void Write();
}
public class PidFileProvider : IProvidePidFile
{
private readonly IAppFolderInfo _appFolderInfo;
private readonly Logger _logger;
2020-08-25 20:26:45 +00:00
public PidFileProvider(IAppFolderInfo appFolderInfo, Logger logger)
{
_appFolderInfo = appFolderInfo;
_logger = logger;
}
public void Write()
{
2014-12-07 20:54:07 +00:00
if (OsInfo.IsWindows)
{
return;
}
2018-11-23 07:03:32 +00:00
var filename = Path.Combine(_appFolderInfo.AppDataFolder, "radarr.pid");
2014-12-07 20:54:07 +00:00
try
{
2020-08-25 20:26:45 +00:00
File.WriteAllText(filename, ProcessProvider.GetCurrentProcessId().ToString());
2014-12-07 20:54:07 +00:00
}
catch (Exception ex)
{
2016-02-11 21:13:42 +00:00
_logger.Error(ex, "Unable to write PID file: " + filename);
2018-11-23 07:03:32 +00:00
throw new RadarrStartupException(ex, "Unable to write PID file {0}", filename);
}
}
}
}