Skip to content

Commit d5e4eff

Browse files
committed
Separate idle settings times for easier config
- Also fix watcher compression mode being set to "NIL" when the system or another program totally uncompresses a folder
1 parent a1e9d10 commit d5e4eff

4 files changed

Lines changed: 29 additions & 14 deletions

File tree

CompactGUI.Watcher/BackgroundCompactor.vb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@ Public Class BackgroundCompactor
2222
Private _excludedFileTypes As String()
2323

2424

25-
Private Const LAST_SYSTEM_MODIFIED_TIME_THRESHOLD As Integer = 300 ' 5 minutes
25+
Private ReadOnly _logger As ILogger(Of Watcher)
2626

27-
Private _logger As ILogger(Of Watcher)
27+
Private ReadOnly _idleSettings As IdleSettings
2828

29-
Public Sub New(excludedFileTypes As String(), logger As ILogger(Of Watcher))
29+
Public Sub New(excludedFileTypes As String(), logger As ILogger(Of Watcher), settings As IdleSettings)
3030

3131
_excludedFileTypes = excludedFileTypes
3232
_logger = logger
33+
_idleSettings = settings
3334
AddHandler IdleDetector.IsIdle, AddressOf OnSystemIdle
3435
AddHandler IdleDetector.IsNotIdle, AddressOf OnSystemNotIdle
3536

@@ -77,7 +78,7 @@ Public Class BackgroundCompactor
7778

7879
For Each folder In foldersCopy
7980
folder.IsWorking = True
80-
Dim recentThresholdDate As DateTime = DateTime.Now.AddSeconds(-LAST_SYSTEM_MODIFIED_TIME_THRESHOLD)
81+
Dim recentThresholdDate As DateTime = DateTime.Now.AddSeconds(-_idleSettings.LastSystemModifiedTimeThresholdSeconds)
8182
If folder.LastSystemModifiedDate > recentThresholdDate Then
8283
WatcherLog.SkippingRecentlyModifiedFolder(_logger, folder.DisplayName)
8384
Continue For
@@ -119,6 +120,7 @@ Public Class BackgroundCompactor
119120

120121
End If
121122
folder.IsWorking = False
123+
folder.RefreshProperties()
122124
_compactor.Dispose()
123125
WatcherLog.FinishedCompactingFolder(_logger, folder.DisplayName)
124126
Next

CompactGUI.Watcher/IdleDetector.vb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ Public Class IdleDetector
66
Public Shared Event IsIdle As EventHandler
77
Public Shared Event IsNotIdle As EventHandler
88

9+
Private Shared _settings As IdleSettings
10+
911
Private Shared _timerTask As Task
1012
Private Shared _idletimer As PeriodicTimer
1113
Private Shared ReadOnly _cts As New CancellationTokenSource
@@ -16,10 +18,14 @@ Public Class IdleDetector
1618

1719
Public Shared Property IsAlreadyIdle As Boolean = False
1820

19-
Shared Sub New()
20-
_idletimer = New PeriodicTimer(TimeSpan.FromSeconds(5))
21+
22+
Public Shared Sub Initialize(settings As IdleSettings)
23+
_settings = settings
24+
_idletimer = New PeriodicTimer(TimeSpan.FromSeconds(_settings.IdleCheckIntervalSeconds))
25+
2126
End Sub
2227

28+
2329
Public Shared Sub Start()
2430
If _timerTask Is Nothing OrElse _timerTask.IsCompleted Then _timerTask = IdleTimerDoWorkAsync()
2531
End Sub
@@ -36,7 +42,7 @@ Public Class IdleDetector
3642
Try
3743
While Await _idletimer.WaitForNextTickAsync(_cts.Token) AndAlso Not _cts.Token.IsCancellationRequested
3844

39-
If GetIdleTime() > 300 AndAlso Not Paused AndAlso IsEnabled Then
45+
If GetIdleTime() > _settings.IdleThresholdSeconds AndAlso Not Paused AndAlso IsEnabled Then
4046
If Not IsAlreadyIdle Then
4147
IsAlreadyIdle = True
4248
RaiseEvent IsIdle(Nothing, EventArgs.Empty)

CompactGUI.Watcher/IdleSettings.vb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Public Class IdleSettings
2+
Public Property IdleCheckIntervalSeconds As Integer = 5
3+
Public Property IdleThresholdSeconds As Integer = 15
4+
Public Property LastSystemModifiedTimeThresholdSeconds As Integer = 300
5+
End Class

CompactGUI.Watcher/Watcher.vb

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ Partial Public Class Watcher : Inherits ObservableRecipient : Implements IRecipi
2626
Private ReadOnly _logger As ILogger(Of Watcher)
2727
Private ReadOnly _settingsService As ISettingsService
2828

29-
Private Const LAST_SYSTEM_MODIFIED_TIME_THRESHOLD As Integer = 180 ' 3 minutes
30-
3129
<NotifyPropertyChangedFor(NameOf(TotalSaved))>
3230
<ObservableProperty> Private _LastAnalysed As DateTime
3331
<ObservableProperty> Private _WatchedFolders As New ObservableCollection(Of WatchedFolder)
@@ -36,6 +34,7 @@ Partial Public Class Watcher : Inherits ObservableRecipient : Implements IRecipi
3634
<ObservableProperty> Private _BGCompactor As BackgroundCompactor
3735

3836
Private ReadOnly Property WatcherJSONFile As IO.FileInfo
37+
Private ReadOnly IdleSettings As IdleSettings
3938

4039
Public ReadOnly Property TotalSaved As Long
4140
Get
@@ -47,10 +46,12 @@ Partial Public Class Watcher : Inherits ObservableRecipient : Implements IRecipi
4746
Sub New(excludedFiletypes As String(), logger As ILogger(Of Watcher), settingsService As ISettingsService)
4847
_logger = logger
4948
_settingsService = settingsService
50-
5149
_DataFolder = settingsService.DataFolder
5250
WatcherJSONFile = New IO.FileInfo(IO.Path.Combine(_DataFolder.FullName, "watcher.json"))
5351

52+
IdleSettings = New IdleSettings
53+
IdleDetector.Initialize(IdleSettings)
54+
5455
WatcherLog.WatcherStarted(logger)
5556
IsActive = True
5657

@@ -59,13 +60,14 @@ Partial Public Class Watcher : Inherits ObservableRecipient : Implements IRecipi
5960
AddHandler IdleDetector.IsIdle, AddressOf OnSystemIdle
6061
AddHandler WatchedFolders.CollectionChanged, AddressOf WatchedFolders_CollectionChanged
6162

62-
BGCompactor = New BackgroundCompactor(excludedFiletypes, _logger)
63+
BGCompactor = New BackgroundCompactor(excludedFiletypes, _logger, IdleSettings)
6364
InitializeWatchedFoldersAsync()
6465

6566

6667
End Sub
6768

6869

70+
6971
Private _disableCounter As Integer = 0
7072
Private _counterLock As New SemaphoreSlim(1, 1)
7173

@@ -186,7 +188,7 @@ Partial Public Class Watcher : Inherits ObservableRecipient : Implements IRecipi
186188
.LastCheckedDate = DateTime.Now
187189
.LastCheckedSize = newItem.LastCheckedSize
188190
.LastSystemModifiedDate = DateTime.Now
189-
.CompressionLevel = newItem.CompressionLevel
191+
.CompressionLevel = If(newItem.CompressionLevel <> WOFCompressionAlgorithm.NO_COMPRESSION,newItem.CompressionLevel, existingItem.CompressionLevel)
190192
End With
191193
existingItem.HasTargetChanged = False
192194
End Sub
@@ -274,7 +276,7 @@ Partial Public Class Watcher : Inherits ObservableRecipient : Implements IRecipi
274276
Try
275277

276278
If Not IsWatchingEnabled Then Return
277-
Dim recentThresholdDate As DateTime = DateTime.Now.AddSeconds(-LAST_SYSTEM_MODIFIED_TIME_THRESHOLD)
279+
Dim recentThresholdDate As DateTime = DateTime.Now.AddSeconds(-IdleSettings.LastSystemModifiedTimeThresholdSeconds)
278280
If WatchedFolders.Any(Function(x) x.LastChangedDate > recentThresholdDate) Then Return
279281

280282
If _parseWatchersSemaphore.CurrentCount <> 0 Then
@@ -381,7 +383,7 @@ Partial Public Class Watcher : Inherits ObservableRecipient : Implements IRecipi
381383

382384
If analysedFiles.Count <> 0 Then
383385
Dim mainCompressionLVL = analysedFiles?.Select(Function(f) f.CompressionMode).Max
384-
watched.CompressionLevel = If(mainCompressionLVL, WOFCompressionAlgorithm.NO_COMPRESSION)
386+
watched.CompressionLevel = If(mainCompressionLVL <> WOFCompressionAlgorithm.NO_COMPRESSION, mainCompressionLVL, watched.CompressionLevel)
385387

386388
If checkDiskModified Then
387389
Dim lastDiskWriteTime = analysedFiles.Select(Function(fl)

0 commit comments

Comments
 (0)