Discussion:
[akka-user][deprecated] How to create a Source that restarts if it was not empty
'Mr B' via Akka User List
2018-08-21 09:08:19 UTC
Permalink
Hi,

I am an akka streams newbie and was wondering how to create a Source that
restarts if it was not empty (Source would be dynamic eg the result of a
database fetch)?

My initial thoughts were to broadcast the source to say Sink.optionFirst
and if the materialised value of Sink.optionFirst is Some(element), then
restart the source, if it is None then complete as normal. However I am
struggling with how I would do this.

Thanks
--
*****************************************************************************************************
** New discussion forum: https://discuss.akka.io/ replacing akka-user google-group soon.
** This group will soon be put into read-only mode, and replaced by discuss.akka.io
** More details: https://akka.io/blog/news/2018/03/13/discuss.akka.io-announced
*****************************************************************************************************
Read the docs: http://akka.io/docs/
Check the FAQ: http://doc.akka.io/docs/akka/current/additional/faq.html
Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+***@googlegroups.com.
To post to this group, send email to akka-***@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.
'Mr B' via Akka User List
2018-09-03 06:49:56 UTC
Permalink
Hi,

I came up with a solution (below) however it is sub optimal - it has to
materialise the graph every time to get Sink.lastOption - ideally what I
want is to avoid materialisation on each iteration:

"Loop on Source on Sink.optionLast" in {
def dynamicSource = Source(1 to Random.nextInt(4)) // represents a
cursor from a database that we want to re-run if not empty


def maybeLastFromMaterialisedGraph: Future[Option[Int]] = {
val source = dynamicSource
val sink = Sink.foreach(println)
val sink2 = Sink.lastOption[Int]
val dataFlow =
source.alsoToMat(sink)(Keep.right)
.toMat(sink2)(Keep.both)
val (fIgnore, fInt) = dataFlow.run()
fInt
}


// TODO: make this tail recursive
def loop(eventuallyMaybeInt: Future[Option[Int]]): Future[Option[Int]]
= {
eventuallyMaybeInt.flatMap { o =>
o match {
case None => eventuallyMaybeInt
case _ => loop(maybeLastFromMaterialisedGraph)
}
}
}


val i = await(loop(maybeLastFromMaterialisedGraph))
println(s"Sink.last i=$i")
}
Post by 'Mr B' via Akka User List
Hi,
I am an akka streams newbie and was wondering how to create a Source that
restarts if it was not empty (Source would be dynamic eg the result of a
database fetch)?
My initial thoughts were to broadcast the source to say Sink.optionFirst
and if the materialised value of Sink.optionFirst is Some(element), then
restart the source, if it is None then complete as normal. However I am
struggling with how I would do this.
Thanks
--
*****************************************************************************************************
** New discussion forum: https://discuss.akka.io/ replacing akka-user google-group soon.
** This group will soon be put into read-only mode, and replaced by discuss.akka.io
** More details: https://akka.io/blog/news/2018/03/13/discuss.akka.io-announced
*****************************************************************************************************
Post by 'Mr B' via Akka User List
Read the docs: http://akka.io/docs/
Check the FAQ: http://doc.akka.io/docs/akka/current/additional/faq.html
Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+***@googlegroups.com.
To post to this group, send email to akka-***@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.
Loading...