Skip to content

Commit

Permalink
sigterm handle
Browse files Browse the repository at this point in the history
  • Loading branch information
djnzx committed Aug 9, 2024
1 parent cdd894e commit 4e18768
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 0 deletions.
40 changes: 40 additions & 0 deletions ce3/src/main/scala/fss101/d12sigterm/SigTermApp.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package fss101.d12sigterm

import cats.effect.Deferred
import cats.effect.IO
import cats.effect.IOApp
import cats.implicits.catsSyntaxEitherId
import scala.concurrent.duration.DurationInt

object SigTermApp extends IOApp.Simple {

val hook: IO[Deferred[IO, Unit]] = IO.deferred[Unit]

def shutdownHook = hook
.flatMap(_.complete(()))
.unsafeRunSync()(cats.effect.unsafe.implicits.global)

/** JVM platform-related */
sys.addShutdownHook(shutdownHook)

val haltSignal: IO[Either[Throwable, Unit]] =
hook
.flatMap(_.get)
.map(_.asRight[Throwable])

def infiniteStream = fs2.Stream
.awakeEvery[IO](1.second)
.evalTap(IO.println)
.drain

val finalizer: IO[Unit] =
IO.println("SIGTERM! handled")

override def run: IO[Unit] =
infiniteStream
.interruptWhen(haltSignal)
.onFinalize(finalizer)
.compile
.drain

}
64 changes: 64 additions & 0 deletions ce3/src/main/scala/fss101/d12sigterm/SigTermRefApp.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package fss101.d12sigterm

import cats.effect.Deferred
import cats.effect.IO
import cats.effect.IOApp
import cats.effect.Ref
import cats.implicits.catsSyntaxEitherId
import scala.concurrent.duration.DurationInt
import scala.concurrent.duration.FiniteDuration

object SigTermRefApp extends IOApp.Simple {

val hook: IO[Deferred[IO, Unit]] = IO.deferred[Unit]

def shutdownHook = hook
.flatMap(_.complete(()))
.unsafeRunSync()(cats.effect.unsafe.implicits.global)

sys.addShutdownHook(shutdownHook)

val haltSignal: IO[Either[Throwable, Unit]] =
hook
.flatMap(_.get)
.map(_.asRight[Throwable])

/** allocate cell */
def mkRef: IO[Ref[IO, List[FiniteDuration]]] =
IO.ref(List.empty[FiniteDuration])

/** collect data to cell */
def collect(d: FiniteDuration)(ref: Ref[IO, List[FiniteDuration]]): IO[Unit] =
ref.update(d :: _)

/** report all things collected */
def report(ref: Ref[IO, List[FiniteDuration]]): IO[Unit] =
ref.get.flatMap { ds =>
IO.blocking {
println("collected:")
ds.foreach(x => pprint.log(x))
}
}

def infiniteStream(ref: Ref[IO, List[FiniteDuration]]): fs2.Stream[IO, Nothing] =
fs2.Stream
.awakeEvery[IO](1.second)
.evalTap(x => IO.println(x))
.evalTap(x => collect(x)(ref))
.drain

override def run: IO[Unit] =
mkRef
.flatMap { ref =>

val finalizer =
IO.println("SIGTERM! handled") >> report(ref)

infiniteStream(ref)
.interruptWhen(haltSignal)
.onFinalize(finalizer)
.compile
.drain
}

}
31 changes: 31 additions & 0 deletions ce3/src/main/scala/lit/Port.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package lit

import org.typelevel.literally.Literally

case class Port(value: Int) extends AnyVal

object Port {
/** code to validate */
def fromString(raw: String): Either[String, Port] =
Option(raw)
.flatMap(_.toIntOption)
.filter(_ > 0)
.filter(_ <= 65535)
.map(Port.apply)
.toRight("invalid port - must be integer between 0 and 65535")

object PortLiteral extends Literally[Port] {
def validate(c: Context)(s: String): Either[String, c.Expr[Port]] = {
import c.universe._
Port.fromString(s)
.map(_ => c.Expr(q"Port.fromString($s).get"))
}

def make(c: Context)(args: c.Expr[Any]*): c.Expr[Port] = this.apply(c)(args: _*)
}

implicit class port(val sc: StringContext) extends AnyVal {
def port(args: Any*): Port = macro PortLiteral.make
}

}
15 changes: 15 additions & 0 deletions munitx/src/main/scala/eff/EffectsTesting.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package eff

import cats.effect._
import munit.CatsEffectSuite
import skunkx.Tools

class EffectsTesting extends CatsEffectSuite with Tools {

test("1") {
IO.realTime
.map(_.toMillis)
.log
}

}

0 comments on commit 4e18768

Please sign in to comment.