-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.scala
66 lines (54 loc) · 2.03 KB
/
Main.scala
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
package opmlalerts
import akka.actor.typed._
import akka.actor.typed.scaladsl.{ ActorContext, Behaviors }
import akka.actor.typed.scaladsl.adapter._
import com.beachape.filemanagement.Messages._
import com.beachape.filemanagement.MonitorActor
import java.nio.file.Paths
import java.nio.file.StandardWatchEventKinds._
import scala.io.StdIn
import scala.util.Try
import sys.process._
object Main extends App {
def exitWithError(error: String) = {
println(fansi.Bold.On(fansi.Color.Red(error)))
sys.exit(1)
}
val opmlPathStr =
args.headOption getOrElse exitWithError("Specify path to an OPML file")
val opmlPath = {
val t = Try { Paths.get(opmlPathStr) }
if (t.isFailure)
exitWithError(s"'$opmlPathStr' is not a valid path: ${t.failed.get}")
t.get
}
def spawnManager(ctx: ActorContext[EventAtPath]) =
ctx.spawnAnonymous(Manager.manage(opmlPath))
val root: Behavior[EventAtPath] = Behaviors.setup { ctx ⇒
val screenWidth = Try { "tput cols".!!.trim.toInt }.toOption
ctx.system.log.debug("Spawning printer with screen width = {}", screenWidth)
ctx.spawn(Printer.printOnConsole(screenWidth), "printer")
ctx.system.log.debug("Spawning file monitor for {}", opmlPath)
val fileMonitor = ctx.actorOf(MonitorActor(concurrency = 1), "fileMonitor")
fileMonitor ! RegisterSubscriber(event = ENTRY_MODIFY, path = opmlPath,
subscriber = ctx.self.toUntyped)
ctx.system.log.debug("Spawning manager with OPML file {}", opmlPath)
restarter(spawnManager(ctx))
}
def restarter(manager: ActorRef[Manager.Message]): Behavior[EventAtPath] =
Behaviors.immutable {
case (ctx, _: EventAtPath) ⇒ {
ctx.system.log.info("OPML {} updated, restarting", opmlPath)
ctx.stop(manager)
restarter(spawnManager(ctx))
}
}
val system = ActorSystem(root, "opmlalerts")
try {
println(fansi.Bold.On("***** Press ENTER to exit the system"))
StdIn.readLine()
} finally {
val _ = system.terminate()
}
println("Bye!")
}