-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
} |