You've already forked Sonarr
							
							
				mirror of
				https://github.com/Sonarr/Sonarr.git
				synced 2025-10-31 00:07:55 +02:00 
			
		
		
		
	Added commands to delete logs and log files (separately)
This commit is contained in:
		| @@ -64,13 +64,13 @@ | ||||
|     <Compile Include="ConfigFileProviderTest.cs" /> | ||||
|     <Compile Include="EnsureTest\PathExtensionFixture.cs" /> | ||||
|     <Compile Include="EnvironmentTests\StartupArgumentsFixture.cs" /> | ||||
|     <Compile Include="EnvironmentTests\EnviromentProviderTest.cs" /> | ||||
|     <Compile Include="EnvironmentTests\EnvironmentProviderTest.cs" /> | ||||
|     <Compile Include="EventingTests\MessageAggregatorCommandTests.cs" /> | ||||
|     <Compile Include="EventingTests\MessageAggregatorEventTests.cs" /> | ||||
|     <Compile Include="ReflectionExtensions.cs" /> | ||||
|     <Compile Include="PathExtensionFixture.cs" /> | ||||
|     <Compile Include="DiskProviderFixture.cs" /> | ||||
|     <Compile Include="EnviromentProviderTest.cs" /> | ||||
|     <Compile Include="EnvironmentProviderTest.cs" /> | ||||
|     <Compile Include="ProcessProviderTests.cs" /> | ||||
|     <Compile Include="ReflectionTests\ReflectionExtensionFixture.cs" /> | ||||
|     <Compile Include="ServiceFactoryFixture.cs" /> | ||||
|   | ||||
| @@ -32,6 +32,7 @@ namespace NzbDrone.Core.Datastore | ||||
|         void SetFields(TModel model, params Expression<Func<TModel, object>>[] properties); | ||||
|         TModel Single(); | ||||
|         PagingSpec<TModel> GetPaged(PagingSpec<TModel> pagingSpec); | ||||
|         void DeleteAll(); | ||||
|     } | ||||
|  | ||||
|  | ||||
| @@ -217,6 +218,10 @@ namespace NzbDrone.Core.Datastore | ||||
|             return pagingSpec; | ||||
|         } | ||||
|  | ||||
|         public void DeleteAll() | ||||
|         { | ||||
|             DataMapper.Delete<TModel>(c => c.Id > 0); | ||||
|         } | ||||
|  | ||||
|         private void PublishModelEvent(TModel model, RepositoryAction action) | ||||
|         { | ||||
|   | ||||
| @@ -0,0 +1,8 @@ | ||||
| using NzbDrone.Common.Messaging; | ||||
|  | ||||
| namespace NzbDrone.Core.Instrumentation.Commands | ||||
| { | ||||
|     public class DeleteLogFilesCommand : ICommand | ||||
|     { | ||||
|     } | ||||
| } | ||||
							
								
								
									
										40
									
								
								NzbDrone.Core/Instrumentation/DeleteLogFilesService.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										40
									
								
								NzbDrone.Core/Instrumentation/DeleteLogFilesService.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,40 @@ | ||||
| using System; | ||||
| using System.Collections.Generic; | ||||
| using System.IO; | ||||
| using System.Linq; | ||||
| using NLog; | ||||
| using NzbDrone.Common; | ||||
| using NzbDrone.Common.EnvironmentInfo; | ||||
| using NzbDrone.Common.Messaging; | ||||
| using NzbDrone.Core.Instrumentation.Commands; | ||||
|  | ||||
| namespace NzbDrone.Core.Instrumentation | ||||
| { | ||||
|     public interface IDeleteLogFilesService | ||||
|     { | ||||
|     } | ||||
|  | ||||
|     public class DeleteLogFilesService : IDeleteLogFilesService, IExecute<DeleteLogFilesCommand> | ||||
|     { | ||||
|         private readonly IDiskProvider _diskProvider; | ||||
|         private readonly IAppFolderInfo _appFolderInfo; | ||||
|         private readonly Logger _logger; | ||||
|  | ||||
|         public DeleteLogFilesService(IDiskProvider diskProvider, IAppFolderInfo appFolderInfo, Logger logger) | ||||
|         { | ||||
|             _diskProvider = diskProvider; | ||||
|             _appFolderInfo = appFolderInfo; | ||||
|             _logger = logger; | ||||
|         } | ||||
|  | ||||
|         public void Execute(DeleteLogFilesCommand message) | ||||
|         { | ||||
|             var logFiles = _diskProvider.GetFiles(_appFolderInfo.GetLogFolder(), SearchOption.TopDirectoryOnly); | ||||
|  | ||||
|             foreach (var logFile in logFiles) | ||||
|             { | ||||
|                 _diskProvider.DeleteFile(logFile); | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -1,4 +1,5 @@ | ||||
| using NzbDrone.Common.Messaging; | ||||
| using System; | ||||
| using NzbDrone.Common.Messaging; | ||||
| using NzbDrone.Core.Datastore; | ||||
| using NzbDrone.Core.Instrumentation.Commands; | ||||
|  | ||||
| @@ -9,7 +10,7 @@ namespace NzbDrone.Core.Instrumentation | ||||
|         PagingSpec<Log> Paged(PagingSpec<Log> pagingSpec); | ||||
|     } | ||||
|  | ||||
|     public class LogService : ILogService, IExecute<TrimLogCommand> | ||||
|     public class LogService : ILogService, IExecute<TrimLogCommand>, IExecute<ClearLogCommand> | ||||
|     { | ||||
|         private readonly ILogRepository _logRepository; | ||||
|  | ||||
| @@ -27,5 +28,10 @@ namespace NzbDrone.Core.Instrumentation | ||||
|         { | ||||
|             _logRepository.Trim(); | ||||
|         } | ||||
|  | ||||
|         public void Execute(ClearLogCommand message) | ||||
|         { | ||||
|             _logRepository.DeleteAll(); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -207,7 +207,10 @@ | ||||
|     <Compile Include="Indexers\IndexerWithSetting.cs" /> | ||||
|     <Compile Include="Indexers\RssSyncCommand.cs" /> | ||||
|     <Compile Include="Indexers\XElementExtensions.cs" /> | ||||
|     <Compile Include="Instrumentation\Commands\ClearLogCommand.cs" /> | ||||
|     <Compile Include="Instrumentation\Commands\DeleteLogFilesCommand.cs" /> | ||||
|     <Compile Include="Instrumentation\Commands\TrimLogCommand.cs" /> | ||||
|     <Compile Include="Instrumentation\DeleteLogFilesService.cs" /> | ||||
|     <Compile Include="Instrumentation\SetLoggingLevel.cs" /> | ||||
|     <Compile Include="Jobs\TaskManager.cs" /> | ||||
|     <Compile Include="Lifecycle\ApplicationShutdownRequested.cs" /> | ||||
|   | ||||
| @@ -9,12 +9,14 @@ define( | ||||
|         'Logs/Files/Collection', | ||||
|         'Logs/Files/Row', | ||||
|         'Logs/Files/ContentsView', | ||||
|         'Logs/Files/ContentsModel' | ||||
|     ], function (App, Marionette, Backgrid, FilenameCell, RelativeDateCell, LogFileCollection, LogFileRow, ContentsView, ContentsModel) { | ||||
|         'Logs/Files/ContentsModel', | ||||
|         'Shared/Toolbar/ToolbarLayout' | ||||
|     ], function (App, Marionette, Backgrid, FilenameCell, RelativeDateCell, LogFileCollection, LogFileRow, ContentsView, ContentsModel, ToolbarLayout) { | ||||
|         return Marionette.Layout.extend({ | ||||
|             template: 'Logs/Files/LayoutTemplate', | ||||
|  | ||||
|             regions: { | ||||
|                 toolbar  : '#x-toolbar', | ||||
|                 grid     : '#x-grid', | ||||
|                 contents : '#x-contents' | ||||
|             }, | ||||
| @@ -35,22 +37,62 @@ define( | ||||
|  | ||||
|             initialize: function () { | ||||
|                 this.collection = new LogFileCollection(); | ||||
|                 this.collectionPromise = this.collection.fetch(); | ||||
|  | ||||
|  | ||||
|                 App.vent.on(App.Commands.ShowLogFile, this._showLogFile, this); | ||||
|             }, | ||||
|  | ||||
|             onShow: function () { | ||||
|                 var self = this; | ||||
|                 this._fetchAndShow(); | ||||
|                 this._showToolbar(); | ||||
|                 this._showTable(); | ||||
|             }, | ||||
|  | ||||
|                 this.collectionPromise.done(function (){ | ||||
|                     self._showLogFile({ model: _.first(self.collection.models) }); | ||||
|             _fetchAndShow: function () { | ||||
|                 var self = this; | ||||
|  | ||||
|                 var promise = this.collection.fetch(); | ||||
|                 promise.done(function () { | ||||
|                     self._showLogFile({ model: self.collection.first() }); | ||||
|                 }); | ||||
|             }, | ||||
|  | ||||
|             _showTable: function () { | ||||
|             _showToolbar: function () { | ||||
|  | ||||
|                 var leftSideButtons = { | ||||
|                     type      : 'default', | ||||
|                     storeState: false, | ||||
|                     items     : | ||||
|                         [ | ||||
|                             { | ||||
|                                 title         : 'Refresh', | ||||
|                                 icon          : 'icon-refresh', | ||||
|                                 ownerContext  : this, | ||||
|                                 callback      : this._refreshLogs | ||||
|                             }, | ||||
|  | ||||
|                             { | ||||
|                                 title          : 'Delete Log Files', | ||||
|                                 icon           : 'icon-trash', | ||||
|                                 command        : 'deleteLogFiles', | ||||
|                                 successMessage : 'Log files have been deleted', | ||||
|                                 errorMessage   : 'Failed to delete log files', | ||||
|                                 ownerContext   : this, | ||||
|                                 successCallback: this._refreshLogs | ||||
|                             } | ||||
|                         ] | ||||
|                 }; | ||||
|  | ||||
|                 this.toolbar.show(new ToolbarLayout({ | ||||
|                     left   : | ||||
|                         [ | ||||
|                             leftSideButtons | ||||
|                         ], | ||||
|                     context: this | ||||
|                 })); | ||||
|             }, | ||||
|  | ||||
|             _showTable: function () { | ||||
|                 this.grid.show(new Backgrid.Grid({ | ||||
|                     row       : LogFileRow, | ||||
|                     columns   : this.columns, | ||||
| @@ -60,6 +102,13 @@ define( | ||||
|             }, | ||||
|  | ||||
|             _showLogFile: function (options) { | ||||
|  | ||||
|                 this.contents.close(); | ||||
|  | ||||
|                 if (!options.model) { | ||||
|                     return; | ||||
|                 } | ||||
|  | ||||
|                 var self = this; | ||||
|                 var filename = options.model.get('filename'); | ||||
|  | ||||
| @@ -74,6 +123,10 @@ define( | ||||
|                         self.contents.show(new ContentsView({ model: model })); | ||||
|                     } | ||||
|                 }); | ||||
|             }, | ||||
|  | ||||
|             _refreshLogs: function () { | ||||
|                 this._fetchAndShow(); | ||||
|             } | ||||
|         }); | ||||
|     }); | ||||
|   | ||||
| @@ -1,4 +1,5 @@ | ||||
| <div class="row"> | ||||
| <div id="x-toolbar"/> | ||||
| <div class="row"> | ||||
|     <div class="span12"> | ||||
|         <div id="x-grid"/> | ||||
|     </div> | ||||
|   | ||||
| @@ -53,19 +53,6 @@ define( | ||||
|                     } | ||||
|                 ], | ||||
|  | ||||
|             leftSideButtons: { | ||||
|                 type      : 'default', | ||||
|                 storeState: false, | ||||
|                 items     : | ||||
|                     [ | ||||
|                         { | ||||
|                             title: 'Files', | ||||
|                             icon : 'icon-file', | ||||
|                             route: 'logs/files' | ||||
|                         } | ||||
|                     ] | ||||
|             }, | ||||
|  | ||||
|             initialize: function () { | ||||
|                 this.collection = new LogCollection(); | ||||
|                 this.collection.fetch(); | ||||
| @@ -92,13 +79,48 @@ define( | ||||
|             }, | ||||
|  | ||||
|             _showToolbar: function () { | ||||
|                 var leftSideButtons = { | ||||
|                     type      : 'default', | ||||
|                         storeState: false, | ||||
|                         items     : | ||||
|                     [ | ||||
|                         { | ||||
|                             title         : 'Refresh', | ||||
|                             icon          : 'icon-refresh', | ||||
|                             ownerContext  : this, | ||||
|                             callback      : this._refreshLogs | ||||
|                         }, | ||||
|  | ||||
|                         { | ||||
|                             title          : 'Clear Logs', | ||||
|                             icon           : 'icon-trash', | ||||
|                             command        : 'clearLog', | ||||
|                             successMessage : 'Logs have been cleared', | ||||
|                             errorMessage   : 'Failed to clear logs', | ||||
|                             ownerContext   : this, | ||||
|                             successCallback: this._refreshLogs | ||||
|                         }, | ||||
|  | ||||
|                         { | ||||
|                             title: 'Files', | ||||
|                             icon : 'icon-file', | ||||
|                             route: 'logs/files' | ||||
|                         } | ||||
|                     ] | ||||
|                 }; | ||||
|  | ||||
|                 this.toolbar.show(new ToolbarLayout({ | ||||
|                     left   : | ||||
|                         [ | ||||
|                             this.leftSideButtons | ||||
|                             leftSideButtons | ||||
|                         ], | ||||
|                     context: this | ||||
|                 })); | ||||
|             }, | ||||
|  | ||||
|             _refreshLogs: function () { | ||||
|                 this.collection.fetch({ reset: true }); | ||||
|                 this._showTable(); | ||||
|             } | ||||
|         }); | ||||
|     }); | ||||
|   | ||||
| @@ -44,8 +44,9 @@ define( | ||||
|                 } | ||||
|             }, | ||||
|  | ||||
|  | ||||
|             invokeCommand: function () { | ||||
|                 //TODO: Use Actioneer to handle icon swapping | ||||
|  | ||||
|                 var command = this.model.get('command'); | ||||
|                 if (command) { | ||||
|                     this.idle = false; | ||||
| @@ -60,18 +61,35 @@ define( | ||||
|                                 message: self.model.get('successMessage') | ||||
|                             }); | ||||
|                         } | ||||
|  | ||||
|                         if (self.model.get('successCallback')) { | ||||
|                             if (!self.model.ownerContext) { | ||||
|                                 throw 'ownerContext must be set.'; | ||||
|                             } | ||||
|  | ||||
|                             self.model.get('successCallback').call(self.model.ownerContext); | ||||
|                         } | ||||
|                     }); | ||||
|  | ||||
|                     commandPromise.fail(function (options) { | ||||
|                         if (options.readyState === 0 || options.status === 0) { | ||||
|                             return; | ||||
|                         } | ||||
|  | ||||
|                         if (self.model.get('errorMessage')) { | ||||
|                             Messenger.show({ | ||||
|                                 message: self.model.get('errorMessage'), | ||||
|                                 type   : 'error' | ||||
|                             }); | ||||
|                         } | ||||
|  | ||||
|                         if (self.model.get('failCallback')) { | ||||
|                             if (!self.model.ownerContext) { | ||||
|                                 throw 'ownerContext must be set.'; | ||||
|                             } | ||||
|  | ||||
|                             self.model.get('failCallback').call(self.model.ownerContext); | ||||
|                         } | ||||
|                     }); | ||||
|  | ||||
|                     commandPromise.always(function () { | ||||
| @@ -81,6 +99,14 @@ define( | ||||
|                             self.idle = true; | ||||
|                         } | ||||
|                     }); | ||||
|  | ||||
|                     if (self.model.get('alwaysCallback')) { | ||||
|                         if (!self.model.ownerContext) { | ||||
|                             throw 'ownerContext must be set.'; | ||||
|                         } | ||||
|  | ||||
|                         self.model.get('alwaysCallback').call(self.model.ownerContext); | ||||
|                     } | ||||
|                 } | ||||
|             }, | ||||
|  | ||||
| @@ -103,7 +129,6 @@ define( | ||||
|                     throw 'ownerContext must be set.'; | ||||
|                 } | ||||
|  | ||||
|  | ||||
|                 var callback = this.model.get('callback'); | ||||
|                 if (callback) { | ||||
|                     callback.call(this.model.ownerContext); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user