Skip to content

Commit

Permalink
Improve saving of Jobs List to avoid data loss on full disk
Browse files Browse the repository at this point in the history
  • Loading branch information
Dendraspis committed Apr 12, 2024
1 parent 0424580 commit 68ff314
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 40 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ v2.37.6 (2024-04-12)

- UI: Add Progress for dovi_tool and hdr10plus_tool
- UI: Adjust Progress Reformatting for SvtAv1EncApp
- UI: Improve saving of Jobs List to avoid data loss on full disk
- VapourSynth: Fix missing loading of SubText for hardcoded subtitles ([#1307](/../../issues/1307))


Expand Down
2 changes: 1 addition & 1 deletion Source/Forms/MainForm.vb
Original file line number Diff line number Diff line change
Expand Up @@ -4676,7 +4676,7 @@ Public Class MainForm
Exit Sub
End If

Dim jobPath = JobManager.GetJobPath()
Dim jobPath = JobManager.JobPath
SaveProjectPath(jobPath)
JobManager.AddJob(jobPath, jobPath, position)

Expand Down
84 changes: 45 additions & 39 deletions Source/General/JobManager.vb
Original file line number Diff line number Diff line change
Expand Up @@ -26,46 +26,21 @@ Public Class JobManager
End Get
End Property

Shared Function GetJobPath() As String
Dim name = p.TargetFile.Base

If name = "" Then
name = Macro.Expand(p.DefaultTargetName)
End If

If name = "" Then
name = p.SourceFile.Base
End If

Return p.TempDir + name + ".srip"
End Function

Shared Sub SaveJobs(jobs As List(Of Job))
Dim formatter As New BinaryFormatter
Dim counter As Integer

While True
Try
Using stream As New FileStream(Folder.Settings + "Jobs.dat",
FileMode.Create, FileAccess.Write, FileShare.None)

formatter.Serialize(stream, jobs)
End Using
Shared ReadOnly Property JobPath As String
Get
Dim name = p.TargetFile.Base

'otherwise exceptions, better solution not found
Thread.Sleep(100)
If name = "" Then
name = Macro.Expand(p.DefaultTargetName)
End If

Exit While
Catch ex As Exception
Thread.Sleep(500)
counter += 1
If name = "" Then
name = p.SourceFile.Base
End If

If counter > 9 Then
Throw ex
End If
End Try
End While
End Sub
Return Path.Combine(p.TempDir, name & ".srip")
End Get
End Property

Shared Sub RemoveJob(path As String)
Dim jobs = GetJobs()
Expand Down Expand Up @@ -119,9 +94,9 @@ Public Class JobManager
End Sub

Shared Function GetJobs() As List(Of Job)
Dim counter As Integer = 0
Dim formatter As New BinaryFormatter
Dim jobsPath = Folder.Settings + "Jobs.dat"
Dim counter As Integer
Dim jobsPath As String = Path.Combine(Folder.Settings, "Jobs.dat")

If File.Exists(jobsPath) Then
While True
Expand Down Expand Up @@ -150,4 +125,35 @@ Public Class JobManager

Return New List(Of Job)
End Function

Shared Sub SaveJobs(jobs As List(Of Job))
Dim counter As Integer = 0
Dim formatter As New BinaryFormatter
Dim jobsDir As String = Folder.Settings
Dim jobsPath As String = Path.Combine(jobsDir, "Jobs.dat")

While True
Try
Dim di As New DriveInfo(jobsDir)

If di.AvailableFreeSpace < 600000 Then Throw New AbortException()

Using stream As New FileStream(jobsPath, FileMode.Create, FileAccess.Write, FileShare.None)
formatter.Serialize(stream, jobs)
End Using

'otherwise exceptions, better solution not found
Thread.Sleep(100)

Exit While
Catch ex As Exception
Thread.Sleep(500)
counter += 1

If counter > 9 Then
Throw ex
End If
End Try
End While
End Sub
End Class

0 comments on commit 68ff314

Please sign in to comment.