Last active
May 25, 2024 10:20
-
-
Save dacr/27daaf2a373442e96621dc44a507b1fe to your computer and use it in GitHub Desktop.
ZIO learning - timeout on sub command process / published by https://github.com/dacr/code-examples-manager #018a0d20-3b52-4d9b-94fe-eca1f94bbb76/90a14ee9864b5a847d311badac4d4aa8a5c9a472
This file contains hidden or 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
// summary : ZIO learning - timeout on sub command process | |
// keywords : scala, zio, learning, process, commands, pure-functional, @testable | |
// publish : gist | |
// authors : David Crosson | |
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2) | |
// id : 018a0d20-3b52-4d9b-94fe-eca1f94bbb76 | |
// created-on : 2022-01-15T14:08:48+01:00 | |
// managed-by : https://github.com/dacr/code-examples-manager | |
// run-with : scala-cli $file | |
// --------------------- | |
//> using scala "3.4.2" | |
//> using dep "dev.zio::zio:2.0.13" | |
//> using dep "dev.zio::zio-process:0.7.2" | |
// --------------------- | |
import zio.* | |
import zio.process.* | |
import zio.stream.ZPipeline.{utf8Decode, splitLines} | |
object Encapsulated extends ZIOAppDefault: | |
def run = | |
for | |
process <- Command("vmstat", "1", "60").redirectErrorStream(true).run | |
stream = process.stdout.stream.via(utf8Decode >>> splitLines) | |
outputFiber <- stream.runCollect.fork | |
status <- process.exitCode.timeout(5.seconds) | |
_ <- process.killTree.ignore | |
output <- outputFiber.join.catchAll(err => ZIO.succeed(Chunk(err.toString))) | |
text = output.mkString("\n") | |
_ <- Console.printLine(s"Gotten content = $text") | |
yield () | |
Encapsulated.main(Array.empty) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment