Skip to content

Commit a7881e5

Browse files
committed
Add tray notifications for scheduler.
1 parent 5716f4f commit a7881e5

4 files changed

Lines changed: 52 additions & 31 deletions

File tree

CompactGUI.Watcher/Watcher.vb

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -102,28 +102,29 @@ Partial Public Class Watcher : Inherits ObservableRecipient : Implements IRecipi
102102
Next
103103

104104
Try
105-
Await Task.Run(Async Function()
106-
_settingsService.AppSettings.ScheduledBackgroundLastRan = DateTime.Now
107-
If Not IsWatchingEnabled Then Return False
108-
Dim recentThresholdDate As DateTime = DateTime.Now.AddSeconds(-IdleSettings.LastSystemModifiedTimeThresholdSeconds)
109-
If Not runAll AndAlso WatchedFolders.Any(Function(x) x.LastChangedDate > recentThresholdDate) Then Return False
110-
111-
If _parseWatchersSemaphore.CurrentCount <> 0 Then
112-
Await ParseWatchers(runAll, cToken)
113-
End If
114-
If cToken <> Nothing AndAlso cToken.IsCancellationRequested Then
115-
_logger.LogInformation("Watcher run cancelled by user.")
116-
Return False
117-
End If
118-
If _parseWatchersSemaphore.CurrentCount <> 0 AndAlso (IsBackgroundCompactingEnabled OrElse runAll) Then
119-
Await BackgroundCompact(runAll) 'Don't need to pass the cancellation token here, as the background compactor handles it internally.
120-
End If
121-
Return True
122-
End Function, cToken)
123105

106+
_settingsService.AppSettings.ScheduledBackgroundLastRan = DateTime.Now
107+
If Not IsWatchingEnabled Then Return False
108+
Dim recentThresholdDate As DateTime = DateTime.Now.AddSeconds(-IdleSettings.LastSystemModifiedTimeThresholdSeconds)
109+
If Not runAll AndAlso WatchedFolders.Any(Function(x) x.LastChangedDate > recentThresholdDate) Then Return False
124110

111+
If _parseWatchersSemaphore.CurrentCount <> 0 Then
112+
Await ParseWatchers(runAll, cToken)
113+
End If
114+
If cToken <> Nothing AndAlso cToken.IsCancellationRequested Then
115+
_logger.LogInformation("Watcher run cancelled by user.")
116+
Return False
117+
End If
118+
If _parseWatchersSemaphore.CurrentCount <> 0 AndAlso (IsBackgroundCompactingEnabled OrElse runAll) Then
119+
Await BackgroundCompact(runAll) 'Don't need to pass the cancellation token here, as the background compactor handles it internally.
120+
End If
121+
If cToken <> Nothing AndAlso cToken.IsCancellationRequested Then
122+
_logger.LogInformation("Watcher run cancelled by user.")
123+
Return False
124+
End If
125125
Return True
126-
Catch ex As OperationCanceledException
126+
127+
Catch ex As TaskCanceledException
127128
Return False
128129
Finally
129130

CompactGUI/Services/SchedulerService.vb

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,30 @@ Public Class SchedulerService
2323
Dim scheduler = CType(Application.GetService(Of IScheduler), Scheduler)
2424
If Not scheduler.TryUnschedule(NameOf(Watcher)) Then Return
2525

26-
scheduler.ScheduleAsync(Async Function() Await Application.GetService(Of Watcher.Watcher).RunWatcher()).
26+
scheduler.ScheduleAsync(Async Function() Await RunScheduledTask()).
2727
Cron($"{settings.ScheduledBackgroundMinute} {settings.ScheduledBackgroundHour} * * *").Zoned(TimeZoneInfo.Local).
2828
When(Function() Task.FromResult(IsSchedulerRunnable)).
2929
PreventOverlapping(NameOf(Watcher))
3030

3131
End Sub
3232

33+
34+
Public Async Function RunScheduledTask() As Task(Of Boolean)
35+
36+
Dim trayService = Application.GetService(Of TrayNotifierService)
37+
trayService.Notify_BackgroundSchedulerRunning()
38+
Dim task = Await Application.GetService(Of Watcher.Watcher).RunWatcher()
39+
40+
If task Then
41+
trayService.Notify_BackgroundSchedulerCompleted()
42+
Return True
43+
End If
44+
Return False
45+
46+
End Function
47+
48+
49+
3350
Public Function IsSchedulerRunnable() As Boolean
3451

3552
SchedulerServiceLog.CheckingSchedulerRunnable(logger)

CompactGUI/Services/TrayNotifierService.vb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
Imports System.Runtime.InteropServices
33
Imports System.Windows.Interop
44

5+
Imports CompactGUI.Core.Settings
6+
57
Public Class TrayNotifierService
68
Private Const NIM_ADD As Integer = &H0
79
Private Const NIM_MODIFY As Integer = &H1
@@ -92,5 +94,16 @@ Public Class TrayNotifierService
9294

9395
End Sub
9496

97+
Public Sub Notify_BackgroundSchedulerRunning()
98+
Dim title = "Scheduled Compression Running"
99+
Dim message = "CompactGUI is running a scheduled task and will compress monitored folders in the background"
100+
ShowBalloon(title, message)
101+
End Sub
102+
103+
Public Sub Notify_BackgroundSchedulerCompleted()
104+
Dim title = "Scheduled Compression Completed"
105+
Dim message = $"Next scheduled task is on {Application.GetService(Of ISettingsService).AppSettings.NextScheduledBackgroundRun}"
106+
ShowBalloon(title, message)
107+
End Sub
95108

96109
End Class

CompactGUI/Views/SettingsPage.xaml

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -302,21 +302,11 @@
302302
</i:EventTrigger>
303303
</i:Interaction.Triggers>
304304
</CheckBox>
305-
<!--<CheckBox x:Name="UiEnableBackgroundAutoCompression"
306-
Content="Run background compressor when system is idle"
307-
Margin="30,-3,0,-3"
308-
IsChecked="{Binding AppSettings.EnableBackgroundAutoCompression, Mode=TwoWay}">
309305

310-
<i:Interaction.Triggers>
311-
<i:EventTrigger EventName="Checked">
312-
<i:InvokeCommandAction Command="{Binding EnableBackgroundWatcherCommand}" />
313-
</i:EventTrigger>
314-
</i:Interaction.Triggers>
315-
</CheckBox>-->
316306
<WrapPanel Margin="45,10,0,0"
317307
IsEnabled="{Binding AppSettings.EnableBackgroundWatcher}"
318308
Orientation="Horizontal">
319-
<TextBlock Text="Run background compressor: " VerticalAlignment="Center" />
309+
<TextBlock Text="Compress folders: " VerticalAlignment="Center" />
320310

321311
<ComboBox x:Name="BackgroundModeSelector"
322312
Width="290" Height="35"

0 commit comments

Comments
 (0)