Added Reload Beginning/Completed events

This commit is contained in:
LukePulverenti Luke Pulverenti luke pulverenti 2012-09-17 16:21:46 -04:00
parent fe427bc7f4
commit 9f1005d679
6 changed files with 108 additions and 72 deletions

View File

@ -0,0 +1,12 @@
using System;
namespace MediaBrowser.Common.Events
{
/// <summary>
/// Provides a generic EventArgs subclass that can hold any kind of object
/// </summary>
public class GenericEventArgs<T> : EventArgs
{
public T Argument { get; set; }
}
}

View File

@ -1,4 +1,5 @@
using MediaBrowser.Common.Logging; using MediaBrowser.Common.Events;
using MediaBrowser.Common.Logging;
using MediaBrowser.Common.Net; using MediaBrowser.Common.Net;
using MediaBrowser.Common.Net.Handlers; using MediaBrowser.Common.Net.Handlers;
using MediaBrowser.Common.Plugins; using MediaBrowser.Common.Plugins;
@ -24,6 +25,34 @@ namespace MediaBrowser.Common.Kernel
where TConfigurationType : BaseApplicationConfiguration, new() where TConfigurationType : BaseApplicationConfiguration, new()
where TApplicationPathsType : BaseApplicationPaths, new() where TApplicationPathsType : BaseApplicationPaths, new()
{ {
#region ReloadBeginning Event
/// <summary>
/// Fires whenever the kernel begins reloading
/// </summary>
public event EventHandler<GenericEventArgs<IProgress<TaskProgress>>> ReloadBeginning;
private void OnReloadBeginning(IProgress<TaskProgress> progress)
{
if (ReloadBeginning != null)
{
ReloadBeginning(this, new GenericEventArgs<IProgress<TaskProgress>> { Argument = progress });
}
}
#endregion
#region ReloadCompleted Event
/// <summary>
/// Fires whenever the kernel completes reloading
/// </summary>
public event EventHandler<GenericEventArgs<IProgress<TaskProgress>>> ReloadCompleted;
private void OnReloadCompleted(IProgress<TaskProgress> progress)
{
if (ReloadCompleted != null)
{
ReloadCompleted(this, new GenericEventArgs<IProgress<TaskProgress>> { Argument = progress });
}
}
#endregion
/// <summary> /// <summary>
/// Gets the current configuration /// Gets the current configuration
/// </summary> /// </summary>
@ -78,7 +107,7 @@ namespace MediaBrowser.Common.Kernel
// Performs initializations that can be reloaded at anytime // Performs initializations that can be reloaded at anytime
await Reload(progress).ConfigureAwait(false); await Reload(progress).ConfigureAwait(false);
progress.Report(new TaskProgress { Description = "Loading Complete" }); ReportProgress(progress, "Loading Complete");
} }
/// <summary> /// <summary>
@ -87,29 +116,41 @@ namespace MediaBrowser.Common.Kernel
protected virtual void InitializeInternal(IProgress<TaskProgress> progress) protected virtual void InitializeInternal(IProgress<TaskProgress> progress)
{ {
ApplicationPaths = new TApplicationPathsType(); ApplicationPaths = new TApplicationPathsType();
ReloadLogger(); ReloadLogger();
progress.Report(new TaskProgress { Description = "Loading configuration" }); ReportProgress(progress, "Loading Configuration");
ReloadConfiguration(); ReloadConfiguration();
progress.Report(new TaskProgress { Description = "Starting Http server" }); ReportProgress(progress, "Loading Http Server");
ReloadHttpServer(); ReloadHttpServer();
} }
/// <summary> /// <summary>
/// Performs initializations that can be reloaded at anytime /// Performs initializations that can be reloaded at anytime
/// </summary> /// </summary>
public virtual async Task Reload(IProgress<TaskProgress> progress) public async Task Reload(IProgress<TaskProgress> progress)
{
OnReloadBeginning(progress);
await ReloadInternal(progress).ConfigureAwait(false);
OnReloadCompleted(progress);
}
/// <summary>
/// Performs initializations that can be reloaded at anytime
/// </summary>
protected virtual async Task ReloadInternal(IProgress<TaskProgress> progress)
{ {
await Task.Run(() => await Task.Run(() =>
{ {
progress.Report(new TaskProgress { Description = "Loading Plugins" }); ReportProgress(progress, "Loading Plugins");
ReloadComposableParts(); ReloadComposableParts();
}).ConfigureAwait(false); }).ConfigureAwait(false);
} }
/// <summary> /// <summary>
/// Disposes the current logger and creates a new one /// Disposes the current logger and creates a new one
/// </summary> /// </summary>
@ -233,7 +274,7 @@ namespace MediaBrowser.Common.Kernel
public virtual void Dispose() public virtual void Dispose()
{ {
Logger.LogInfo("Beginning Kernel.Dispose"); Logger.LogInfo("Beginning Kernel.Dispose");
DisposeComposableParts(); DisposeComposableParts();
DisposeHttpServer(); DisposeHttpServer();
@ -257,7 +298,7 @@ namespace MediaBrowser.Common.Kernel
if (Plugins != null) if (Plugins != null)
{ {
Logger.LogInfo("Disposing Plugins"); Logger.LogInfo("Disposing Plugins");
foreach (BasePlugin plugin in Plugins) foreach (BasePlugin plugin in Plugins)
{ {
plugin.Dispose(); plugin.Dispose();
@ -273,7 +314,7 @@ namespace MediaBrowser.Common.Kernel
if (HttpServer != null) if (HttpServer != null)
{ {
Logger.LogInfo("Disposing Http Server"); Logger.LogInfo("Disposing Http Server");
HttpServer.Dispose(); HttpServer.Dispose();
} }
@ -293,7 +334,7 @@ namespace MediaBrowser.Common.Kernel
if (Logger.LoggerInstance != null) if (Logger.LoggerInstance != null)
{ {
Logger.LogInfo("Disposing Logger"); Logger.LogInfo("Disposing Logger");
Logger.LoggerInstance.Dispose(); Logger.LoggerInstance.Dispose();
} }
} }
@ -309,6 +350,16 @@ namespace MediaBrowser.Common.Kernel
} }
} }
protected void ReportProgress(IProgress<TaskProgress> progress, string message)
{
progress.Report(new TaskProgress { Description = message });
if (Logger.LoggerInstance != null)
{
Logger.LogInfo(message);
}
}
BaseApplicationPaths IKernel.ApplicationPaths BaseApplicationPaths IKernel.ApplicationPaths
{ {
get { return ApplicationPaths; } get { return ApplicationPaths; }

View File

@ -81,6 +81,7 @@
<Reference Include="WindowsBase" /> <Reference Include="WindowsBase" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Events\GenericEventArgs.cs" />
<Compile Include="Kernel\BaseApplicationPaths.cs" /> <Compile Include="Kernel\BaseApplicationPaths.cs" />
<Compile Include="Drawing\DrawingUtils.cs" /> <Compile Include="Drawing\DrawingUtils.cs" />
<Compile Include="Logging\TraceLogger.cs" /> <Compile Include="Logging\TraceLogger.cs" />
@ -151,6 +152,7 @@
<ItemGroup> <ItemGroup>
<Resource Include="Resources\Images\spinner.gif" /> <Resource Include="Resources\Images\spinner.gif" />
</ItemGroup> </ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets. Other similar extension points exist, see Microsoft.Common.targets.

View File

@ -5,7 +5,6 @@ using Microsoft.Shell;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Threading.Tasks;
using System.Windows; using System.Windows;
namespace MediaBrowser.Common.UI namespace MediaBrowser.Common.UI
@ -44,8 +43,6 @@ namespace MediaBrowser.Common.UI
var progress = new Progress<TaskProgress>(); var progress = new Progress<TaskProgress>();
progress.ProgressChanged += progress_ProgressChanged;
var splash = new Splash(progress); var splash = new Splash(progress);
splash.Show(); splash.Show();
@ -56,8 +53,6 @@ namespace MediaBrowser.Common.UI
await Kernel.Init(progress); await Kernel.Init(progress);
progress.ProgressChanged -= progress_ProgressChanged;
Logger.LogInfo("Kernel.Init completed in {0} seconds.", (DateTime.UtcNow - now).TotalSeconds); Logger.LogInfo("Kernel.Init completed in {0} seconds.", (DateTime.UtcNow - now).TotalSeconds);
splash.Close(); splash.Close();
@ -69,8 +64,6 @@ namespace MediaBrowser.Common.UI
} }
catch (Exception ex) catch (Exception ex)
{ {
progress.ProgressChanged -= progress_ProgressChanged;
if (Logger.LoggerInstance != null) if (Logger.LoggerInstance != null)
{ {
Logger.LogException(ex); Logger.LogException(ex);
@ -84,44 +77,8 @@ namespace MediaBrowser.Common.UI
} }
} }
public async Task ReloadKernel()
{
var progress = new Progress<TaskProgress>();
progress.ProgressChanged += progress_ProgressChanged;
try
{
DateTime now = DateTime.UtcNow;
await Kernel.Reload(progress);
progress.ProgressChanged -= progress_ProgressChanged;
Logger.LogInfo("Kernel.Reload completed in {0} seconds.", (DateTime.UtcNow - now).TotalSeconds);
}
catch (Exception ex)
{
progress.ProgressChanged -= progress_ProgressChanged;
Logger.LogException(ex);
// Shutdown the app with an error code
Shutdown(1);
}
}
void progress_ProgressChanged(object sender, TaskProgress e)
{
if (Logger.LoggerInstance != null)
{
Logger.LogInfo(e.Description);
}
}
protected virtual void OnKernelLoaded() protected virtual void OnKernelLoaded()
{ {
} }
protected override void OnExit(ExitEventArgs e) protected override void OnExit(ExitEventArgs e)

View File

@ -97,18 +97,18 @@ namespace MediaBrowser.Controller
/// <summary> /// <summary>
/// Performs initializations that can be reloaded at anytime /// Performs initializations that can be reloaded at anytime
/// </summary> /// </summary>
public override async Task Reload(IProgress<TaskProgress> progress) protected override async Task ReloadInternal(IProgress<TaskProgress> progress)
{ {
await base.Reload(progress).ConfigureAwait(false); await base.ReloadInternal(progress).ConfigureAwait(false);
ReloadWeatherClient(); ReloadWeatherClient();
ExtractFFMpeg(); ExtractFFMpeg();
progress.Report(new TaskProgress { Description = "Loading Users" }); ReportProgress(progress, "Loading Users");
ReloadUsers(); ReloadUsers();
progress.Report(new TaskProgress { Description = "Loading Media Library" }); ReportProgress(progress, "Loading Media Library");
await ReloadRoot(allowInternetProviders: false).ConfigureAwait(false); await ReloadRoot(allowInternetProviders: false).ConfigureAwait(false);
} }

View File

@ -1,4 +1,7 @@
using Hardcodet.Wpf.TaskbarNotification; using Hardcodet.Wpf.TaskbarNotification;
using MediaBrowser.Common.Events;
using MediaBrowser.Controller;
using MediaBrowser.Model.Progress;
using System; using System;
using System.ComponentModel; using System.ComponentModel;
using System.Threading; using System.Threading;
@ -11,15 +14,36 @@ namespace MediaBrowser.ServerApplication
/// </summary> /// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged public partial class MainWindow : Window, INotifyPropertyChanged
{ {
private Timer LoadingIconTimer { get; set; }
public MainWindow() public MainWindow()
{ {
InitializeComponent(); InitializeComponent();
Loaded += MainWindow_Loaded; Loaded += MainWindowLoaded;
} }
void MainWindow_Loaded(object sender, RoutedEventArgs e) void MainWindowLoaded(object sender, RoutedEventArgs e)
{ {
DataContext = this; DataContext = this;
Kernel.Instance.ReloadBeginning += KernelReloadBeginning;
Kernel.Instance.ReloadCompleted += KernelReloadCompleted;
}
void KernelReloadBeginning(object sender, GenericEventArgs<IProgress<TaskProgress>> e)
{
MbTaskbarIcon.ShowBalloonTip("Media Browser is reloading", "Please wait...", BalloonIcon.Info);
LoadingImageIndex = 0;
LoadingIconTimer = new Timer(LoadingIconTimerCallback, null, 0, 250);
}
void KernelReloadCompleted(object sender, GenericEventArgs<IProgress<TaskProgress>> e)
{
LoadingIconTimer.Dispose();
LoadingImageIndex = 0;
} }
public event PropertyChangedEventHandler PropertyChanged; public event PropertyChangedEventHandler PropertyChanged;
@ -62,17 +86,7 @@ namespace MediaBrowser.ServerApplication
private async void cmdReloadServer_click(object sender, RoutedEventArgs e) private async void cmdReloadServer_click(object sender, RoutedEventArgs e)
{ {
MbTaskbarIcon.ShowBalloonTip("Media Browser is reloading", "Please wait...", BalloonIcon.Info); await Kernel.Instance.Reload(new Progress<TaskProgress>()).ConfigureAwait(false);
LoadingImageIndex = 0;
Timer timer = new Timer(LoadingIconTimerCallback, null, 0, 250);
await (Application.Current as App).ReloadKernel().ConfigureAwait(false);
timer.Dispose();
LoadingImageIndex = 0;
} }
private void LoadingIconTimerCallback(object stateInfo) private void LoadingIconTimerCallback(object stateInfo)