Skip to content

Commit

Permalink
Use IO.blocking wherever code can block
Browse files Browse the repository at this point in the history
  • Loading branch information
nafg committed May 10, 2021
1 parent cb4653d commit c0f87cd
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,24 @@ trait AgiIvrCommandInterpreter extends IOIvrCommandInterpreter {
hangup *>
IO.raiseError(new AgiHangupException)

override def dial(to: String, ringTimeout: Int, flags: String) = IO {
override def dial(to: String, ringTimeout: Int, flags: String) = IO.blocking {
channel.exec("Dial", s"$to,$ringTimeout|$flags")
}

override def amd = IO {
override def amd = IO.blocking {
channel.exec("AMD")
}

override def getVar(name: String) = IO {
override def getVar(name: String) = IO.blocking {
Option(channel.getFullVariable("${" + name + "}"))
}

override def callerId: IO[String] = IO {
override def callerId: IO[String] = IO.blocking {
channel.getFullVariable("$" + "{CALLERID(num)}")
}

override def waitForDigit(timeout: Int): IO[Option[DTMF]] =
IO {
IO.blocking {
channel.waitForDigit(timeout)
}
.flatMap {
Expand All @@ -46,18 +46,18 @@ trait AgiIvrCommandInterpreter extends IOIvrCommandInterpreter {
}


override def waitForSilence(ms: Int, repeat: Int = 1, timeoutSec: Option[Int] = None) = IO {
override def waitForSilence(ms: Int, repeat: Int = 1, timeoutSec: Option[Int] = None) = IO.blocking {
channel.exec("WaitForSilence", s"$ms,$repeat" + timeoutSec.map("," + _).getOrElse(""))
()
}

override def monitor(file: File) = IO {
override def monitor(file: File) = IO.blocking {
channel.exec("System", s"mkdir -p ${file.getParentFile.getAbsolutePath}")
channel.exec("MixMonitor", file.getAbsolutePath)
()
}

override def hangup = IO {
override def hangup = IO.blocking {
channel.hangup()
}

Expand All @@ -67,20 +67,20 @@ trait AgiIvrCommandInterpreter extends IOIvrCommandInterpreter {
timeLimitMillis: Int,
offset: Int,
beep: Boolean,
maxSilenceSecs: Int) = IO {
maxSilenceSecs: Int) = IO.blocking {
val parent = Paths.get(pathAndName).getParent.toString
channel.exec("System", s"mkdir -p $parent")
val ch =
channel.recordFile(pathAndName, format, interruptDtmfs.mkString, timeLimitMillis, offset, beep, maxSilenceSecs)
DTMF.fromChar.get(ch)
}

override def setAutoHangup(seconds: Int): IO[Unit] = IO {
override def setAutoHangup(seconds: Int): IO[Unit] = IO.blocking {
channel.setAutoHangup(seconds)
}

override def streamFile(pathAndName: String, interruptDtmfs: Set[DTMF]) =
IO {
IO.blocking {
channel.streamFile(pathAndName, interruptDtmfs.mkString)
}
.flatMap {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ trait AmiIvrCommandInterpreter extends IOIvrCommandInterpreter {
def ami: Ami

override def originate(dest: String, script: String, args: Seq[String]): IO[Unit] =
IO {
IO.blocking {
ami.originate(dest, script, args)
}
}
6 changes: 3 additions & 3 deletions core/src/main/scala/simpleivr/DefaultSayer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package simpleivr

import cats.effect.IO

import scala.concurrent.duration.DurationInt


class DefaultSayer(interp: IvrCommand.Interpreter[IO], interruptDtmfs: Set[DTMF])
extends Sayable.Folder[IO[Option[DTMF]]] {
Expand All @@ -12,7 +14,7 @@ class DefaultSayer(interp: IvrCommand.Interpreter[IO], interruptDtmfs: Set[DTMF]
if (ms <= 0)
IO.pure(None)
else if (interruptDtmfs.isEmpty)
IO(Thread.sleep(ms.toLong)).map(_ => None)
IO.sleep(ms.millis).as(None)
else
for {
startTime <- curTime
Expand Down Expand Up @@ -45,5 +47,3 @@ class DefaultSayer(interp: IvrCommand.Interpreter[IO], interruptDtmfs: Set[DTMF]
}
}
}


15 changes: 10 additions & 5 deletions core/src/main/scala/simpleivr/LocalAudioFileBackend.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,28 @@ import cats.effect.IO
case class LocalAudioFile(path: Path) extends AudioFile {
override def name = path.toString

override def exists = IO {
override def exists = IO.blocking {
Files.exists(path)
}

override def write(f: WritableByteChannel => IO[Unit]): IO[Either[Throwable, Unit]] =
for {
fileChan <- IO {
fileChan <- IO.blocking {
Files.createDirectories(path.getParent)
FileChannel.open(path, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE)
FileChannel.open(
path,
StandardOpenOption.WRITE,
StandardOpenOption.TRUNCATE_EXISTING,
StandardOpenOption.CREATE
)
}
res <- f(fileChan).attempt
_ <- IO {
_ <- IO.blocking {
fileChan.close()
}
} yield res

override def lastModified = IO {
override def lastModified = IO.blocking {
Files.getLastModifiedTime(path).toInstant
}
}
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/scala/simpleivr/Text2waveSpeakGenerator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ object Text2waveSpeakGenerator extends SpeakGenerator {
case false =>
speak.files.wavFile
.write { writeChan =>
IO {
IO.blocking {
println("Falling back to text2wave because audio file does not exist: " + speak.files.supportedAudioFiles)
val tmpFile = Files.createTempFile("chavrusa-text2wave", ".wav")
val text2wave = Runtime.getRuntime.exec("/usr/bin/text2wave -scale 1.5 -F 8000 -o " + tmpFile.toString)
Expand All @@ -30,7 +30,7 @@ object Text2waveSpeakGenerator extends SpeakGenerator {
}
}
.flatMap { either =>
IO {
IO.blocking {
either.left.foreach(_.printStackTrace())
}
}
Expand Down

0 comments on commit c0f87cd

Please sign in to comment.