Skip to content

Commit

Permalink
Merge pull request #3215 from MediaBrowser/dev
Browse files Browse the repository at this point in the history
adopt linux log rotation
  • Loading branch information
LukePulverenti authored Mar 29, 2018
2 parents 161d9e6 + 9ab0977 commit 8c12f41
Show file tree
Hide file tree
Showing 19 changed files with 137 additions and 119 deletions.
30 changes: 25 additions & 5 deletions Emby.Server.Implementations/HttpServer/HttpListenerHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -527,11 +527,7 @@ protected async Task RequestHandler(IHttpRequest httpReq, string urlString, stri

if (!ValidateSsl(httpReq.RemoteIp, urlString))
{
var httpsUrl = urlString
.Replace("http://", "https://", StringComparison.OrdinalIgnoreCase)
.Replace(":" + _config.Configuration.PublicPort.ToString(CultureInfo.InvariantCulture), ":" + _config.Configuration.PublicHttpsPort.ToString(CultureInfo.InvariantCulture), StringComparison.OrdinalIgnoreCase);

RedirectToUrl(httpRes, httpsUrl);
RedirectToSecureUrl(httpReq, httpRes, urlString);
return;
}

Expand Down Expand Up @@ -730,6 +726,30 @@ private void Write(IResponse response, string text)
outputStream.Write(bOutput, 0, bOutput.Length);
}

private void RedirectToSecureUrl(IHttpRequest httpReq, IResponse httpRes, string url)
{
int currentPort;
Uri uri;
if (Uri.TryCreate(url, UriKind.Absolute, out uri))
{
currentPort = uri.Port;
var builder = new UriBuilder(uri);
builder.Port = _config.Configuration.PublicHttpsPort;
builder.Scheme = "https";
url = builder.Uri.ToString();

RedirectToUrl(httpRes, url);
}
else
{
var httpsUrl = url
.Replace("http://", "https://", StringComparison.OrdinalIgnoreCase)
.Replace(":" + _config.Configuration.PublicPort.ToString(CultureInfo.InvariantCulture), ":" + _config.Configuration.PublicHttpsPort.ToString(CultureInfo.InvariantCulture), StringComparison.OrdinalIgnoreCase);

RedirectToUrl(httpRes, url);
}
}

public static void RedirectToUrl(IResponse httpRes, string url)
{
httpRes.StatusCode = 302;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ public static string GetResponseContentType(IRequest httpReq)
var serverDefaultContentType = "application/json";

var acceptContentTypes = httpReq.AcceptTypes;
var defaultContentType = httpReq.ContentType;
string defaultContentType = null;
if (HasAnyOfContentTypes(httpReq, FormUrlEncoded, MultiPartFormData))
{
defaultContentType = serverDefaultContentType;
Expand Down
8 changes: 7 additions & 1 deletion Emby.Server.Implementations/IO/ManagedFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,13 @@ public string MakeAbsolutePath(string folderPath, string filePath)
return filePath;
}

if (filePath[0] == '/' || filePath[0] == '\\') //relative path
var firstChar = filePath[0];
if (firstChar == '/')
{
// For this we don't really know.
return filePath;
}
if (firstChar == '\\') //relative path
{
filePath = filePath.Substring(1);
}
Expand Down
8 changes: 5 additions & 3 deletions Emby.Server.Implementations/Library/MediaSourceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ await item.RefreshMetadata(new MediaBrowser.Controller.Providers.MetadataRefresh
{
SetUserProperties(hasMediaSources as BaseItem, source, user);
}

// Validate that this is actually possible
if (source.SupportsDirectStream)
{
Expand Down Expand Up @@ -188,10 +188,12 @@ private bool SupportsDirectStream(string path, MediaProtocol protocol)
{
if (path != null)
{
if (path.IndexOf(".m3u", StringComparison.OrdinalIgnoreCase) == -1)
if (path.IndexOf(".m3u", StringComparison.OrdinalIgnoreCase) != -1)
{
//return true;
return false;
}

return true;
}
}

Expand Down
80 changes: 71 additions & 9 deletions Emby.Server.Implementations/Logging/SimpleLogManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,31 @@ public ILogger GetLogger(string name)
return new NamedLogger(name, this);
}

public void ReloadLogger(LogSeverity severity)
public async Task ReloadLogger(LogSeverity severity, CancellationToken cancellationToken)
{
LogSeverity = severity;

var logger = _fileLogger;
if (logger != null)
{
logger.Dispose();
await TryMoveToArchive(logger.Path, cancellationToken).ConfigureAwait(false);
}

var path = Path.Combine(LogDirectory, LogFilePrefix + "-" + decimal.Floor(DateTime.Now.Ticks / 10000000) + ".txt");
var newPath = Path.Combine(LogDirectory, LogFilePrefix + ".txt");

_fileLogger = new FileLogger(path);
if (File.Exists(newPath))
{
newPath = await TryMoveToArchive(newPath, cancellationToken).ConfigureAwait(false);
}

_fileLogger = new FileLogger(newPath);

if (LoggerLoaded != null)
{
try
{

LoggerLoaded(this, EventArgs.Empty);

}
catch (Exception ex)
{
Expand All @@ -60,6 +64,42 @@ public void ReloadLogger(LogSeverity severity)
}
}

private async Task<string> TryMoveToArchive(string file, CancellationToken cancellationToken, int retryCount = 0)
{
var archivePath = GetArchiveFilePath();

try
{
File.Move(file, archivePath);

return file;
}
catch (FileNotFoundException)
{
return file;
}
catch (DirectoryNotFoundException)
{
return file;
}
catch
{
if (retryCount >= 50)
{
return GetArchiveFilePath();
}

await Task.Delay(100, cancellationToken).ConfigureAwait(false);

return await TryMoveToArchive(file, cancellationToken, retryCount + 1).ConfigureAwait(false);
}
}

private string GetArchiveFilePath()
{
return Path.Combine(LogDirectory, LogFilePrefix + "-" + decimal.Floor(DateTime.Now.Ticks / 10000000) + ".txt");
}

public event EventHandler LoggerLoaded;

public void Flush()
Expand Down Expand Up @@ -104,6 +144,9 @@ public void Dispose()
if (logger != null)
{
logger.Dispose();

var task = TryMoveToArchive(logger.Path, CancellationToken.None);
Task.WaitAll(task);
}

_fileLogger = null;
Expand All @@ -119,9 +162,13 @@ public class FileLogger : IDisposable
private readonly CancellationTokenSource _cancellationTokenSource;
private readonly BlockingCollection<string> _queue = new BlockingCollection<string>();

public string Path { get; set; }

public FileLogger(string path)
{
Directory.CreateDirectory(Path.GetDirectoryName(path));
Path = path;

Directory.CreateDirectory(System.IO.Path.GetDirectoryName(path));

_fileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read, 32768);
_cancellationTokenSource = new CancellationTokenSource();
Expand All @@ -144,6 +191,10 @@ private void LogInternal()
}

_fileStream.Write(bytes, 0, bytes.Length);
if (_disposed)
{
return;
}

_fileStream.Flush(true);
}
Expand Down Expand Up @@ -177,12 +228,23 @@ public void Flush()

public void Dispose()
{
if (_disposed)
{
return;
}

_disposed = true;
_cancellationTokenSource.Cancel();

Flush();
var stream = _fileStream;
if (stream != null)
{
using (stream)
{
stream.Flush(true);
}
}

_disposed = true;
_fileStream.Dispose();
GC.SuppressFinalize(this);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,9 @@ public Task Execute(CancellationToken cancellationToken, IProgress<double> progr

progress.Report(0);

LogManager.ReloadLogger(ConfigurationManager.CommonConfiguration.EnableDebugLevelLogging
return LogManager.ReloadLogger(ConfigurationManager.CommonConfiguration.EnableDebugLevelLogging
? LogSeverity.Debug
: LogSeverity.Info);

return Task.FromResult(true);
: LogSeverity.Info, cancellationToken);
}

/// <summary>
Expand Down Expand Up @@ -96,7 +94,7 @@ public string Category

public bool IsHidden
{
get { return true; }
get { return false; }
}

public bool IsEnabled
Expand Down
14 changes: 2 additions & 12 deletions MediaBrowser.Controller/Entities/Folder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -363,22 +363,17 @@ private async Task ValidateChildrenInternal2(IProgress<double> progress, Cancell

if (IsFileProtocol)
{
var isOffline = false;
IEnumerable<BaseItem> nonCachedChildren;

try
{
nonCachedChildren = GetNonCachedChildren(directoryService);
}
catch (IOException ex)
catch (Exception ex)
{
nonCachedChildren = new BaseItem[] { };

isOffline = true;
return;
}

if (nonCachedChildren == null) return; //nothing to validate

progress.Report(5);

if (recursive)
Expand Down Expand Up @@ -428,9 +423,6 @@ private async Task ValidateChildrenInternal2(IProgress<double> progress, Cancell
{
}

else if (isOffline)
{
}
else
{
Logger.Debug("Removed item: " + item.Path);
Expand Down Expand Up @@ -1481,8 +1473,6 @@ public List<BaseItem> GetLinkedChildren(User user)
.OfType<Folder>()
.ToList();

//var allUserRootChildren = LibraryManager.GetUserRootFolder().Children.OfType<Folder>().ToList();

var collectionFolderIds = allUserRootChildren
.Select(i => i.Id)
.ToList();
Expand Down
4 changes: 3 additions & 1 deletion MediaBrowser.Model/Logging/ILogManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Threading;
using System.Threading.Tasks;

namespace MediaBrowser.Model.Logging
{
Expand Down Expand Up @@ -29,7 +31,7 @@ public interface ILogManager
/// <summary>
/// Reloads the logger.
/// </summary>
void ReloadLogger(LogSeverity severity);
Task ReloadLogger(LogSeverity severity, CancellationToken cancellationToken);

/// <summary>
/// Occurs when [logger loaded].
Expand Down
2 changes: 2 additions & 0 deletions MediaBrowser.Model/Providers/RemoteSearchResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ public class RemoteSearchResult : IHasProviderIds
public string Overview { get; set; }

public RemoteSearchResult AlbumArtist { get; set; }
public RemoteSearchResult[] Artists { get; set; }

public RemoteSearchResult()
{
ProviderIds = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
Artists = new RemoteSearchResult[] { };
}
}
}
1 change: 0 additions & 1 deletion MediaBrowser.Providers/MediaBrowser.Providers.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@
<Compile Include="Music\AudioDbExternalIds.cs" />
<Compile Include="Music\AudioMetadataService.cs" />
<Compile Include="Music\Extensions.cs" />
<Compile Include="Music\ImvdbProvider.cs" />
<Compile Include="Music\MovieDbMusicVideoProvider.cs" />
<Compile Include="Music\MusicBrainzArtistProvider.cs" />
<Compile Include="Music\MusicExternalIds.cs" />
Expand Down
Loading

0 comments on commit 8c12f41

Please sign in to comment.