-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProgram.fs
79 lines (69 loc) · 2.43 KB
/
Program.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
module Emulsion.Program
open System
open System.IO
open Akka.Actor
open Microsoft.Extensions.Configuration
open System.Threading
open Emulsion.Actors
open Emulsion.MessageSystem
open Emulsion.Settings
let private getConfiguration directory fileName =
let config =
ConfigurationBuilder()
.SetBasePath(directory)
.AddJsonFile(fileName)
.Build()
Settings.read config
let private logError = printfn "ERROR: %A"
let private logInfo = printfn "INFO : %s"
let private startMessageSystem (system: IMessageSystem) receiver =
Async.StartChild <| async {
do! Async.SwitchToNewThread()
try
system.Run receiver
with
| ex -> logError ex
}
let private startApp config =
async {
printfn "Prepare system..."
use system = ActorSystem.Create("emulsion")
printfn "Prepare factories..."
let restartContext = {
cooldown = TimeSpan.FromSeconds(30.0) // TODO[F]: Customize through the config.
logError = logError
logMessage = logInfo
}
let! cancellationToken = Async.CancellationToken
let xmpp = Xmpp.Client(restartContext, cancellationToken, config.xmpp)
let telegram = Telegram.Client(restartContext, cancellationToken, config.telegram)
let factories = { xmppFactory = Xmpp.spawn xmpp
telegramFactory = Telegram.spawn telegram }
printfn "Prepare Core..."
let core = Core.spawn factories system "core"
printfn "Starting message systems..."
let! telegramSystem = startMessageSystem telegram core.Tell
let! xmppSystem = startMessageSystem xmpp core.Tell
printfn "Ready. Wait for termination..."
do! Async.AwaitTask system.WhenTerminated
printfn "Waiting for terminating of message systems..."
do! telegramSystem
do! xmppSystem
}
let private runApp app =
Async.RunSynchronously app
0
let private defaultConfigFileName = "emulsion.json"
[<EntryPoint>]
let main = function
| [| |] ->
getConfiguration (Directory.GetCurrentDirectory()) defaultConfigFileName
|> startApp
|> runApp
| [| configPath |] ->
getConfiguration (Path.GetDirectoryName configPath) (Path.GetFileName configPath)
|> startApp
|> runApp
| _ ->
printfn "Arguments: [config file name] (%s by default)" defaultConfigFileName
0