-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ThreadUtilsIO.scala
71 lines (56 loc) · 2.67 KB
/
ThreadUtilsIO.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
67
68
69
70
71
package japgolly.webapputil.cats.effect
import cats.effect.unsafe.{IORuntime, IORuntimeConfig}
import cats.effect.{IO, Resource}
import com.typesafe.scalalogging.Logger
import japgolly.webapputil.general.ThreadUtils
import java.time.Duration
import java.util.concurrent.{Executors, TimeUnit}
import scala.concurrent.ExecutionContext
object ThreadUtilsIO {
import ThreadUtils.{ThreadGroups, ThreadPool, ThreadPool2, newThreadPool}
object Runtimes {
lazy val scheduledTasks = newDefaultRuntime("ScheduledTasks")
lazy val shutdown = newDefaultRuntime("Shutdown")
}
def newDefaultRuntime(threadPrefix: String): IORuntime = {
import IORuntime._
val (compute, _) = createWorkStealingComputeThreadPool(threadPrefix = s"$threadPrefix-compute")
val (blocking, _) = createDefaultBlockingExecutionContext(threadPrefix = s"$threadPrefix-blocking")
val (scheduler, _) = createDefaultScheduler(threadPrefix = s"$threadPrefix-scheduler")
IORuntime(compute, blocking, scheduler, () => (), IORuntimeConfig())
}
def runOnShutdown(name: String, proc: => Unit): Unit =
runOnShutdown(name, IO(proc))
def runOnShutdown(name: String, task: IO[Unit]): Unit = {
val t = new Thread(ThreadGroups.shutdown, task.toJavaRunnable(Runtimes.shutdown), "shutdown-" + name)
java.lang.Runtime.getRuntime.addShutdownHook(t)
}
// ===================================================================================================================
def threadPool(threadGroupName: String, logger: Logger)(f: ThreadPool => ThreadPool2): Resource[IO, ExecutionContext] =
Resource.make[IO, ThreadPool2](
IO(f(newThreadPool(threadGroupName, logger)))
)(
a => IO(a.threadPoolExecutor.shutdown())
).map(_.executionContext)
// ===================================================================================================================
def newScheduler(threadName: String, threadGroup: ThreadGroup): Scheduler =
new Scheduler(threadName, threadGroup)
final class Scheduler(threadName: String, threadGroup: ThreadGroup) {
val executorService =
Executors.newSingleThreadScheduledExecutor(new Thread(threadGroup, _, threadName))
def scheduleAtFixedRate[A](io: IO[A], period: Duration): Scheduler =
scheduleAtFixedRate(io, period, period)
def scheduleAtFixedRate[A](io: IO[A], initialDelay: Duration, period: Duration): Scheduler = {
executorService.scheduleAtFixedRate(
io.toJavaRunnable(Runtimes.scheduledTasks),
initialDelay.toMillis,
period.toMillis,
TimeUnit.MILLISECONDS)
this
}
def addShutdownHook(io: IO[Unit]): Scheduler = {
runOnShutdown(threadName, io)
this
}
}
}