Skip to content

Commit

Permalink
Merge pull request #3 from kaeedo/bugfix/FileInUse
Browse files Browse the repository at this point in the history
Actually use Actor model for intended purpose to fix rookie concurren…
  • Loading branch information
Kai authored Jan 30, 2017
2 parents 6376619 + 99ef890 commit 67f0c06
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 57 deletions.
34 changes: 31 additions & 3 deletions Cursed.Base/Actors/CacheActor.fs
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
namespace Cursed.Base

module CacheActor =
open System
open Common
open System.IO
open System.Text
open System.Collections.Generic
open Newtonsoft.Json

let private cacheFileLocation = HomePath @@ "cache.txt"

let private ensureDirectory directoryPath =
let directory = new DirectoryInfo(directoryPath)
if not directory.Exists then
directory.Create()

let private ensureFile fileName =
let file = new FileInfo(fileName)

ensureDirectory <| file.DirectoryName

if not file.Exists then
let newFile = file.Create()
newFile.Close()

let FileLoop =
let inboxHandler (inbox: MailboxProcessor<FileReplyMessage>) =
Expand Down Expand Up @@ -46,12 +66,20 @@ module CacheActor =
p
)

File.WriteAllText(cacheFileLocation, JsonConvert.SerializeObject(newState), Encoding.UTF8)
return! messageLoop newState
| GetCache reply ->
reply.Reply oldState
return! messageLoop oldState
| Load projects ->
return! messageLoop projects
| Load ->
ensureFile cacheFileLocation
let cache = File.ReadAllText(cacheFileLocation, Encoding.UTF8)
let projects = JsonConvert.DeserializeObject<IList<Project>>(cache)

if isNull projects then
return! messageLoop []
else
return! messageLoop (projects |> List.ofSeq)
| FileReplyMessage.Restart ->
return! messageLoop []

Expand Down
7 changes: 3 additions & 4 deletions Cursed.Base/Cursed.Base.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
<Compile Include="ModpackController.fs" />
<Compile Include="Actors\ViewActor.fs" />
<Compile Include="Actors\CacheActor.fs" />
<Compile Include="DataAccess.fs" />
<Compile Include="Modpack.fs" />
<Compile Include="Startup.fs" />
<Compile Include="MainFormController.fs" />
Expand Down Expand Up @@ -109,14 +108,14 @@
<Choose>
<When Condition="$(TargetFrameworkIdentifier) == '.NETFramework' And ($(TargetFrameworkVersion) == 'v4.0' Or $(TargetFrameworkVersion) == 'v4.5' Or $(TargetFrameworkVersion) == 'v4.5.1' Or $(TargetFrameworkVersion) == 'v4.5.2' Or $(TargetFrameworkVersion) == 'v4.5.3' Or $(TargetFrameworkVersion) == 'v4.6' Or $(TargetFrameworkVersion) == 'v4.6.1' Or $(TargetFrameworkVersion) == 'v4.6.2' Or $(TargetFrameworkVersion) == 'v4.6.3')">
<ItemGroup>
<Reference Include="System.Xml.Linq">
<Paket>True</Paket>
</Reference>
<Reference Include="FSharp.Data">
<HintPath>..\packages\FSharp.Data\lib\net40\FSharp.Data.dll</HintPath>
<Private>True</Private>
<Paket>True</Paket>
</Reference>
<Reference Include="System.Xml.Linq">
<Paket>True</Paket>
</Reference>
</ItemGroup>
</When>
<When Condition="($(TargetFrameworkIdentifier) == '.NETCore') Or ($(TargetFrameworkIdentifier) == '.NETStandard' And ($(TargetFrameworkVersion) == 'v1.1' Or $(TargetFrameworkVersion) == 'v1.2' Or $(TargetFrameworkVersion) == 'v1.3' Or $(TargetFrameworkVersion) == 'v1.4' Or $(TargetFrameworkVersion) == 'v1.5' Or $(TargetFrameworkVersion) == 'v1.6')) Or ($(TargetFrameworkIdentifier) == '.NETCoreApp' And $(TargetFrameworkVersion) == 'v1.0') Or ($(TargetFrameworkIdentifier) == 'MonoAndroid') Or ($(TargetFrameworkIdentifier) == 'MonoTouch') Or ($(TargetFrameworkIdentifier) == 'Xamarin.iOS') Or ($(TargetFrameworkIdentifier) == 'Xamarin.Mac') Or ($(TargetFrameworkProfile) == 'Profile7') Or ($(TargetFrameworkProfile) == 'Profile44')">
Expand Down
37 changes: 0 additions & 37 deletions Cursed.Base/DataAccess.fs

This file was deleted.

16 changes: 11 additions & 5 deletions Cursed.Base/MainForm.fs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ type MainForm(app: Application) =
layout

do
DataAccess.LoadCache ()
CacheActor.FileLoop.Post Load

let dynamicLayout =
let layout = new DynamicLayout()
Expand Down Expand Up @@ -159,11 +159,17 @@ type MainForm(app: Application) =
base.DataContext <- modpack

async {
let! isLatest = Startup.IsLatest

if not isLatest then
try
let! isLatest = Startup.IsLatest

if not isLatest then
app.Invoke (fun () ->
app.MainForm.Title <- sprintf "Cursed - Update Available"
)
with
| :? Exception ->
app.Invoke (fun () ->
app.MainForm.Title <- sprintf "Cursed - Update Available"
app.MainForm.Title <- sprintf "Cursed - Update check failed"
)
}
|> Async.Start
3 changes: 0 additions & 3 deletions Cursed.Base/Modpack.fs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ open HttpFs.Client
open Eto.Forms
open Common
open ModpackController
open DataAccess

type ModpackBase() =
let propertyChanged = new Event<_, _>()
Expand Down Expand Up @@ -94,8 +93,6 @@ type Modpack(app: Application) as this =
let saveToCache projectId modName fileId fileName =
CacheActor.FileLoop.Post <| SaveProject { Id = projectId; Name = modName; Files = [] }
CacheActor.FileLoop.Post <| SaveMod (projectId, { Id = fileId; FileName = fileName })
let cache = CacheActor.FileLoop.PostAndReply GetCache
Save cache

let cachedModName =
let cache = CacheActor.FileLoop.PostAndReply GetCache
Expand Down
2 changes: 1 addition & 1 deletion Cursed.Base/State.fs
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ type FileReplyMessage =
| SaveProject of Project
| SaveMod of projectId: int * ModFile
| GetCache of AsyncReplyChannel<Project list>
| Load of Project list
| Load
| Restart
6 changes: 3 additions & 3 deletions Cursed/Cursed.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,14 @@
<Choose>
<When Condition="$(TargetFrameworkIdentifier) == '.NETFramework' And ($(TargetFrameworkVersion) == 'v4.0' Or $(TargetFrameworkVersion) == 'v4.5' Or $(TargetFrameworkVersion) == 'v4.5.1' Or $(TargetFrameworkVersion) == 'v4.5.2' Or $(TargetFrameworkVersion) == 'v4.5.3' Or $(TargetFrameworkVersion) == 'v4.6' Or $(TargetFrameworkVersion) == 'v4.6.1' Or $(TargetFrameworkVersion) == 'v4.6.2' Or $(TargetFrameworkVersion) == 'v4.6.3')">
<ItemGroup>
<Reference Include="System.Xml.Linq">
<Paket>True</Paket>
</Reference>
<Reference Include="FSharp.Data">
<HintPath>..\packages\FSharp.Data\lib\net40\FSharp.Data.dll</HintPath>
<Private>True</Private>
<Paket>True</Paket>
</Reference>
<Reference Include="System.Xml.Linq">
<Paket>True</Paket>
</Reference>
</ItemGroup>
</When>
<When Condition="($(TargetFrameworkIdentifier) == '.NETCore') Or ($(TargetFrameworkIdentifier) == '.NETStandard' And ($(TargetFrameworkVersion) == 'v1.1' Or $(TargetFrameworkVersion) == 'v1.2' Or $(TargetFrameworkVersion) == 'v1.3' Or $(TargetFrameworkVersion) == 'v1.4' Or $(TargetFrameworkVersion) == 'v1.5' Or $(TargetFrameworkVersion) == 'v1.6')) Or ($(TargetFrameworkIdentifier) == '.NETCoreApp' And $(TargetFrameworkVersion) == 'v1.0') Or ($(TargetFrameworkIdentifier) == 'MonoAndroid') Or ($(TargetFrameworkIdentifier) == 'MonoTouch') Or ($(TargetFrameworkIdentifier) == 'Xamarin.iOS') Or ($(TargetFrameworkIdentifier) == 'Xamarin.Mac') Or ($(TargetFrameworkProfile) == 'Profile7') Or ($(TargetFrameworkProfile) == 'Profile44')">
Expand Down
2 changes: 1 addition & 1 deletion paket.dependencies
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ nuget Eto.Platform.Gtk3
#nuget Eto.Platform.Mac.Template
nuget Eto.Platform.Wpf
nuget FSharp.Data
nuget FSharp.Core redirects: force
nuget FSharp.Core redirects: force
nuget Http.fs
nuget Newtonsoft.Json
nuget SharpZipLib
Expand Down
3 changes: 3 additions & 0 deletions release-notes.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## New in 1.2.3 (Release 2017/01/30)
* Fix concurrency issues with cache saving

## New in 1.2.2 (Release 2017/01/07)
* Fix handling of Zip files in Linux systems

Expand Down

0 comments on commit 67f0c06

Please sign in to comment.