Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Source.repeatEval, improve exception handling in sources #96

Merged
merged 3 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions core/src/main/scala/ox/channels/SourceCompanionOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,31 @@ trait SourceCompanionOps:
}
c

def repeat[T](element: T = ())(using Ox, StageCapacity): Source[T] =
/** Creates a channel, to which the given `element` is sent repeatedly.
*
* @param element
* The element to send
* @return
* A source to which the given element is sent repeatedly.
*/
def repeat[T](element: T = ())(using Ox, StageCapacity): Source[T] = repeatEval(element)

/** Creates a channel, to which the result of evaluating `f` is sent repeatedly. As the parameter is passed by-name, the evaluation is
* deferred until the element is sent, and happens multiple times.
*
* @param f
* The code block, computing the element to send
* @return
* A source to which the result of evaluating `f` is sent repeatedly.
*/
def repeatEval[T](f: => T)(using Ox, StageCapacity): Source[T] =
val c = StageCapacity.newChannel[T]
fork {
forever {
c.sendSafe(element)
}
try
forever {
c.sendSafe(f)
}
catch case t: Throwable => c.errorSafe(t)
}
c

Expand Down
19 changes: 19 additions & 0 deletions core/src/test/scala/ox/channels/SourceOpsRepeatEvalTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package ox.channels

import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
import ox.supervised
import ox.channels.Source

class SourceOpsRepeatEvalTest extends AnyFlatSpec with Matchers {
behavior of "SourceOps.repeatEval"

it should "evaluate the element before each send" in supervised {
var i = 0
val s = Source.repeatEval {
i += 1
i
}
s.take(3).toList shouldBe List(1, 2, 3)
}
}
23 changes: 11 additions & 12 deletions kafka/src/main/scala/ox/kafka/KafkaConsumerActor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import ox.*
import ox.channels.*

import scala.jdk.CollectionConverters.*
import scala.util.control.NonFatal

object KafkaConsumerActor:
private val logger = LoggerFactory.getLogger(classOf[KafkaConsumerActor.type])
Expand All @@ -30,30 +29,30 @@ object KafkaConsumerActor:
consumer.subscribe(topics.asJava)
true
catch
case NonFatal(e) =>
logger.error(s"Exception when subscribing to $topics", e)
c.errorSafe(e)
case t: Throwable =>
logger.error(s"Exception when subscribing to $topics", t)
c.errorSafe(t)
false
case KafkaConsumerRequest.Poll(results) =>
try
results.send(consumer.poll(java.time.Duration.ofMillis(100)))
true
catch
case NonFatal(e) =>
logger.error("Exception when polling for records in Kafka", e)
results.errorSafe(e)
c.errorSafe(e)
case t: Throwable =>
logger.error("Exception when polling for records in Kafka", t)
results.errorSafe(t)
c.errorSafe(t)
false
case KafkaConsumerRequest.Commit(offsets, result) =>
try
consumer.commitSync(offsets.view.mapValues(o => new OffsetAndMetadata(o + 1)).toMap.asJava)
result.sendSafe(())
true
catch
case NonFatal(e) =>
logger.error("Exception when committing offsets", e)
result.errorSafe(e)
c.errorSafe(e)
case t: Throwable =>
logger.error("Exception when committing offsets", t)
result.errorSafe(t)
c.errorSafe(t)
false
}
finally
Expand Down
8 changes: 3 additions & 5 deletions kafka/src/main/scala/ox/kafka/KafkaSource.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import org.slf4j.LoggerFactory
import ox.*
import ox.channels.*

import scala.util.control.NonFatal

object KafkaSource:
private val logger = LoggerFactory.getLogger(classOf[KafkaSource.type])

Expand Down Expand Up @@ -38,9 +36,9 @@ object KafkaSource:
records.forEach(r => c.send(ReceivedMessage(kafkaConsumer, r)))
}
catch
case NonFatal(e) =>
logger.error("Exception when polling for records", e)
c.errorSafe(e)
case t: Throwable =>
logger.error("Exception when polling for records", t)
c.errorSafe(t)
}

c
Loading