From 53e4c09cc1f161d3eb19918bbf2d0081a4ff637a Mon Sep 17 00:00:00 2001 From: Derrick Agorhom <76208189+derekagorhom@users.noreply.github.com> Date: Wed, 3 Jul 2024 12:21:29 +0200 Subject: [PATCH 1/4] Fixed problem with R-instat start up --- instat/frmMain.vb | 76 ++++++++++++++--------------------------------- 1 file changed, 23 insertions(+), 53 deletions(-) diff --git a/instat/frmMain.vb b/instat/frmMain.vb index b1a58b1cc0c..7b454b236e9 100644 --- a/instat/frmMain.vb +++ b/instat/frmMain.vb @@ -1120,59 +1120,29 @@ Public Class frmMain End Sub Public Sub DeleteAutoSaveFiles() - Try - If Directory.Exists(strAutoSaveDataFolderPath) OrElse Directory.Exists(strAutoSaveLogFolderPath) OrElse Directory.Exists(strAutoSaveInternalLogFolderPath) Then - ' Define the retention policy (keep last N autosaves) - Dim retentionCount As Integer = 5 ' Example: Keep the last 5 autosaves - - ' Retrieve autosaved files - If Directory.Exists(strAutoSaveDataFolderPath) Then - Dim autoSaveDirectory As New DirectoryInfo(strAutoSaveDataFolderPath) - Dim files As FileInfo() = autoSaveDirectory.GetFiles("data_*.rds") ' Adjust pattern to match actual filenames - - ' Sort files by last write time in descending order - Dim sortedFiles As IOrderedEnumerable(Of FileInfo) = files.OrderByDescending(Function(f) f.LastWriteTime) - - ' Determine files to delete based on retention policy - Dim filesToDelete As IEnumerable(Of FileInfo) = sortedFiles.Skip(retentionCount) - - ' Delete older autosaved files - For Each file In filesToDelete - file.Delete() - Next - ElseIf Directory.Exists(strAutoSaveLogFolderPath) Then - Dim autoSaveDirectory As New DirectoryInfo(strAutoSaveLogFolderPath) - Dim files As FileInfo() = autoSaveDirectory.GetFiles("log*.R") ' Adjust pattern to match actual filenames - - ' Sort files by last write time in descending order - Dim sortedFiles As IOrderedEnumerable(Of FileInfo) = files.OrderByDescending(Function(f) f.LastWriteTime) - - ' Determine files to delete based on retention policy - Dim filesToDelete As IEnumerable(Of FileInfo) = sortedFiles.Skip(retentionCount) - - ' Delete older autosaved files - For Each file In filesToDelete - file.Delete() - Next - ElseIf Directory.Exists(strAutoSaveInternalLogFolderPath) Then - Dim autoSaveDirectory As New DirectoryInfo(strAutoSaveInternalLogFolderPath) - Dim files As FileInfo() = autoSaveDirectory.GetFiles("debug_log*.R") ' Adjust pattern to match actual filenames - - ' Sort files by last write time in descending order - Dim sortedFiles As IOrderedEnumerable(Of FileInfo) = files.OrderByDescending(Function(f) f.LastWriteTime) - - ' Determine files to delete based on retention policy - Dim filesToDelete As IEnumerable(Of FileInfo) = sortedFiles.Skip(retentionCount) - - ' Delete older autosaved files - For Each file In filesToDelete - file.Delete() - Next - End If - End If - Catch ex As Exception - MsgBox("Could not delete auto save data file at: " & strCurrentAutoSaveDataFilePath & Environment.NewLine & ex.Message) - End Try + If strCurrentAutoSaveDataFilePath <> "" Then + Try + File.Delete(strCurrentAutoSaveDataFilePath) + Catch ex As Exception + MsgBox("Could not delete auto save data file at: " & strCurrentAutoSaveDataFilePath & Environment.NewLine & ex.Message) + End Try + End If + + If clsRLink.strAutoSaveLogFilePath <> "" Then + Try + File.Delete(clsRLink.strAutoSaveLogFilePath) + Catch ex As Exception + MsgBox("Could not delete auto save log file at: " & clsRLink.strAutoSaveLogFilePath & Environment.NewLine & ex.Message) + End Try + End If + + If clsRLink.strAutoSaveDebugLogFilePath <> "" Then + Try + File.Delete(clsRLink.strAutoSaveDebugLogFilePath) + Catch ex As Exception + MsgBox("Could not delete auto save debug log file at: " & clsRLink.strAutoSaveDebugLogFilePath & Environment.NewLine & ex.Message) + End Try + End If End Sub From 10189dce81c45dd2008c5aebc9ade9f19fb41577 Mon Sep 17 00:00:00 2001 From: Derrick Agorhom <76208189+derekagorhom@users.noreply.github.com> Date: Thu, 4 Jul 2024 13:56:56 +0200 Subject: [PATCH 2/4] code changes --- instat/frmMain.vb | 86 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 58 insertions(+), 28 deletions(-) diff --git a/instat/frmMain.vb b/instat/frmMain.vb index 7b454b236e9..e0a1dbaa222 100644 --- a/instat/frmMain.vb +++ b/instat/frmMain.vb @@ -458,15 +458,15 @@ Public Class frmMain '--------------------------------------- 'prompt user for recovery selection If (strAutoSavedLogFilePaths.Length > 0 OrElse - strAutoSavedDataFilePaths.Length > 0) AndAlso - MsgBox("We have detected that R-Instat may have closed unexpectedly last time." & Environment.NewLine & - "Would you like to see auto recovery options?", - MessageBoxButtons.YesNo, "Auto Recovery") = MsgBoxResult.Yes Then + strAutoSavedDataFilePaths.Length > 0) Then + 'MsgBox("We have detected that R-Instat may have closed unexpectedly last time." & Environment.NewLine & + ' "Would you like to see auto recovery options?", + ' MessageBoxButtons.YesNo, "Auto Recovery") = MsgBoxResult.Yes Then dlgAutoSaveRecovery.strAutoSavedLogFilePaths = strAutoSavedLogFilePaths dlgAutoSaveRecovery.strAutoSavedDataFilePaths = strAutoSavedDataFilePaths dlgAutoSaveRecovery.strAutoSavedInternalLogFilePaths = strAutoSavedInternalLogFilePaths - dlgAutoSaveRecovery.ShowDialog() + 'dlgAutoSaveRecovery.ShowDialog() 'todo. the dialog design is meant to only return just one option; script, data file path or new session. 'refactor the dialog to enforce the design @@ -1120,29 +1120,59 @@ Public Class frmMain End Sub Public Sub DeleteAutoSaveFiles() - If strCurrentAutoSaveDataFilePath <> "" Then - Try - File.Delete(strCurrentAutoSaveDataFilePath) - Catch ex As Exception - MsgBox("Could not delete auto save data file at: " & strCurrentAutoSaveDataFilePath & Environment.NewLine & ex.Message) - End Try - End If - - If clsRLink.strAutoSaveLogFilePath <> "" Then - Try - File.Delete(clsRLink.strAutoSaveLogFilePath) - Catch ex As Exception - MsgBox("Could not delete auto save log file at: " & clsRLink.strAutoSaveLogFilePath & Environment.NewLine & ex.Message) - End Try - End If - - If clsRLink.strAutoSaveDebugLogFilePath <> "" Then - Try - File.Delete(clsRLink.strAutoSaveDebugLogFilePath) - Catch ex As Exception - MsgBox("Could not delete auto save debug log file at: " & clsRLink.strAutoSaveDebugLogFilePath & Environment.NewLine & ex.Message) - End Try - End If + Try + If Directory.Exists(strAutoSaveDataFolderPath) OrElse Directory.Exists(strAutoSaveLogFolderPath) OrElse Directory.Exists(strAutoSaveInternalLogFolderPath) Then + ' Define the retention policy (keep last N autosaves) + Dim retentionCount As Integer = 5 ' Example: Keep the last 5 autosaves + + ' Retrieve autosaved files + If Directory.Exists(strAutoSaveDataFolderPath) Then + Dim autoSaveDirectory As New DirectoryInfo(strAutoSaveDataFolderPath) + Dim files As FileInfo() = autoSaveDirectory.GetFiles("data_*.rds") ' Adjust pattern to match actual filenames + + ' Sort files by last write time in descending order + Dim sortedFiles As IOrderedEnumerable(Of FileInfo) = files.OrderByDescending(Function(f) f.LastWriteTime) + + ' Determine files to delete based on retention policy + Dim filesToDelete As IEnumerable(Of FileInfo) = sortedFiles.Skip(retentionCount) + + ' Delete older autosaved files + For Each file In filesToDelete + file.Delete() + Next + ElseIf Directory.Exists(strAutoSaveLogFolderPath) Then + Dim autoSaveDirectory As New DirectoryInfo(strAutoSaveLogFolderPath) + Dim files As FileInfo() = autoSaveDirectory.GetFiles("log*.R") ' Adjust pattern to match actual filenames + + ' Sort files by last write time in descending order + Dim sortedFiles As IOrderedEnumerable(Of FileInfo) = files.OrderByDescending(Function(f) f.LastWriteTime) + + ' Determine files to delete based on retention policy + Dim filesToDelete As IEnumerable(Of FileInfo) = sortedFiles.Skip(retentionCount) + + ' Delete older autosaved files + For Each file In filesToDelete + file.Delete() + Next + ElseIf Directory.Exists(strAutoSaveInternalLogFolderPath) Then + Dim autoSaveDirectory As New DirectoryInfo(strAutoSaveInternalLogFolderPath) + Dim files As FileInfo() = autoSaveDirectory.GetFiles("debug_log*.R") ' Adjust pattern to match actual filenames + + ' Sort files by last write time in descending order + Dim sortedFiles As IOrderedEnumerable(Of FileInfo) = files.OrderByDescending(Function(f) f.LastWriteTime) + + ' Determine files to delete based on retention policy + Dim filesToDelete As IEnumerable(Of FileInfo) = sortedFiles.Skip(retentionCount) + + ' Delete older autosaved files + For Each file In filesToDelete + file.Delete() + Next + End If + End If + Catch ex As Exception + MsgBox("Could not delete auto save data file at: " & strCurrentAutoSaveDataFilePath & Environment.NewLine & ex.Message) + End Try End Sub From b5792c31ca16f164f8d127c1cf427882e2341aee Mon Sep 17 00:00:00 2001 From: Ntalumeso Date: Thu, 4 Jul 2024 20:11:51 +0300 Subject: [PATCH 3/4] fix auto recovery --- instat/frmMain.vb | 54 ++++++++++++++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/instat/frmMain.vb b/instat/frmMain.vb index b1a58b1cc0c..c2351ac4d28 100644 --- a/instat/frmMain.vb +++ b/instat/frmMain.vb @@ -54,6 +54,8 @@ Public Class frmMain Public strAutoSaveLogFolderPath As String = Path.Combine(Path.GetTempPath, "R-Instat_log_auto_save") Public strAutoSaveInternalLogFolderPath As String = Path.Combine(Path.GetTempPath, "R-Instat_debug_log_auto_save") + Private strMarkerFilePath As String = Path.Combine(strAutoSaveLogFolderPath, "app_marker.log") + Public strCurrentAutoSaveDataFilePath As String = "" Private strLatestVersion As String = "" @@ -457,32 +459,41 @@ Public Class frmMain '--------------------------------------- 'prompt user for recovery selection - If (strAutoSavedLogFilePaths.Length > 0 OrElse - strAutoSavedDataFilePaths.Length > 0) AndAlso - MsgBox("We have detected that R-Instat may have closed unexpectedly last time." & Environment.NewLine & - "Would you like to see auto recovery options?", - MessageBoxButtons.YesNo, "Auto Recovery") = MsgBoxResult.Yes Then - - dlgAutoSaveRecovery.strAutoSavedLogFilePaths = strAutoSavedLogFilePaths - dlgAutoSaveRecovery.strAutoSavedDataFilePaths = strAutoSavedDataFilePaths - dlgAutoSaveRecovery.strAutoSavedInternalLogFilePaths = strAutoSavedInternalLogFilePaths - dlgAutoSaveRecovery.ShowDialog() - - 'todo. the dialog design is meant to only return just one option; script, data file path or new session. - 'refactor the dialog to enforce the design - - 'get the R script from read file if selected by the user - strScript = dlgAutoSaveRecovery.GetScript() - 'get the data file path if selected by the user - strDataFilePath = dlgAutoSaveRecovery.GetDataFilePath() + 'Check if the marker file exists + If File.Exists(strMarkerFilePath) Then + Dim lastExitStatus As String = File.ReadAllText(strMarkerFilePath).Trim() + If lastExitStatus <> "CleanExit" AndAlso + MsgBox("We have detected that R-Instat may have closed unexpectedly last time." & Environment.NewLine & + "Would you like to see auto recovery options?", + MessageBoxButtons.YesNo, "Auto Recovery") = MsgBoxResult.Yes Then + + dlgAutoSaveRecovery.strAutoSavedLogFilePaths = strAutoSavedLogFilePaths + dlgAutoSaveRecovery.strAutoSavedDataFilePaths = strAutoSavedDataFilePaths + dlgAutoSaveRecovery.strAutoSavedInternalLogFilePaths = strAutoSavedInternalLogFilePaths + dlgAutoSaveRecovery.ShowDialog() + + 'todo. the dialog design is meant to only return just one option; script, data file path or new session. + 'refactor the dialog to enforce the design + + 'get the R script from read file if selected by the user + strScript = dlgAutoSaveRecovery.GetScript() + 'get the data file path if selected by the user + strDataFilePath = dlgAutoSaveRecovery.GetDataFilePath() + End If End If + + Using writer As StreamWriter = New StreamWriter(strMarkerFilePath, False) + writer.WriteLine("Running") + End Using + '--------------------------------------- + '--------------------------------------- 'delete the recovery files If strAutoSavedLogFilePaths.Length > 0 Then Try - File.Delete(strAutoSavedLogFilePaths(0)) + File.Delete(strAutoSavedLogFilePaths(1)) '1 to avoid deleting app_marker file Catch ex As Exception MsgBox("Could not delete backup log file" & Environment.NewLine, "Error deleting file") End Try @@ -1078,6 +1089,11 @@ Public Class frmMain If RuntimeInformation.IsOSPlatform(OSPlatform.Windows) Then CefRuntimeWrapper.ShutDownCef() End If + + Using writer As StreamWriter = New StreamWriter(strMarkerFilePath, False) + writer.WriteLine("CleanExit") + End Using + Catch ex As Exception MsgBox("Error attempting to save setting files to App Data folder." & Environment.NewLine & "System error message: " & ex.Message, MsgBoxStyle.Critical, "Error saving settings") End Try From fe79c4c1e0d1236976ab3221a3a72f6a2118e761 Mon Sep 17 00:00:00 2001 From: Derrick Agorhom <76208189+derekagorhom@users.noreply.github.com> Date: Fri, 5 Jul 2024 09:05:27 +0200 Subject: [PATCH 4/4] Code changes --- instat/frmMain.vb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/instat/frmMain.vb b/instat/frmMain.vb index e0a1dbaa222..b1a58b1cc0c 100644 --- a/instat/frmMain.vb +++ b/instat/frmMain.vb @@ -458,15 +458,15 @@ Public Class frmMain '--------------------------------------- 'prompt user for recovery selection If (strAutoSavedLogFilePaths.Length > 0 OrElse - strAutoSavedDataFilePaths.Length > 0) Then - 'MsgBox("We have detected that R-Instat may have closed unexpectedly last time." & Environment.NewLine & - ' "Would you like to see auto recovery options?", - ' MessageBoxButtons.YesNo, "Auto Recovery") = MsgBoxResult.Yes Then + strAutoSavedDataFilePaths.Length > 0) AndAlso + MsgBox("We have detected that R-Instat may have closed unexpectedly last time." & Environment.NewLine & + "Would you like to see auto recovery options?", + MessageBoxButtons.YesNo, "Auto Recovery") = MsgBoxResult.Yes Then dlgAutoSaveRecovery.strAutoSavedLogFilePaths = strAutoSavedLogFilePaths dlgAutoSaveRecovery.strAutoSavedDataFilePaths = strAutoSavedDataFilePaths dlgAutoSaveRecovery.strAutoSavedInternalLogFilePaths = strAutoSavedInternalLogFilePaths - 'dlgAutoSaveRecovery.ShowDialog() + dlgAutoSaveRecovery.ShowDialog() 'todo. the dialog design is meant to only return just one option; script, data file path or new session. 'refactor the dialog to enforce the design