hnaderi / edomata

Production ready event-driven automata for Scala, Scala.js and scala native
http://edomata.ir/
Apache License 2.0
84 stars 9 forks source link

How does event listening work? #114

Open burakakca opened 1 year ago

burakakca commented 1 year ago

I'm using two ZIO scope blocks in my codebase one of them is for creating commands and the other for listening if an event published. When the command and the listener are in the same code block and run sequentially, there is no problem, but when they are run in different time intervals, the listening side does not receive information. How can I solve this?

There is a pub/sub mechanism or it's reading from the database?

Command code block like this example; https://github.com/hnaderi/edomata-example/blob/05609b7f54388dae89b9a925eea21b794db9fd39/core/src/main/scala/dev/hnaderi/example/Main.scala#L47

Listening code block

def mySink(app: Application[Task]) = ZSink.foreach((item: OutboxItem[Notification]) =>
  for {
    _ <- printLine("mySink: " + item)
    _ <- app.accounts.storage.outbox.markAsSent(item)
  } yield ()
)

def printOutbox(app: Application[Task]) = app.accounts.storage.outbox.read.toZStream().run(mySink(app))

ZIO.scoped {
 for {
   app <- Application[Task]().toScopedZIO
   _ <- printOutbox(app)
  } yield ()
 }..fork
hnaderi commented 1 year ago

Basically storage methods are all providing data from database itself, there are also notifications for updates (both in journal and outbox), so you can listen to them and read when necessary. outbox.read reads all of the available outboxed messages.
You can also use edomata.backend.OutboxConsumer which handles all of these, so you don't need to do it by hand, as most of the storage methods are low-level. You can see how OutboxConsumer works in the following code: https://github.com/hnaderi/edomata/blob/ea3a7549b961a107c0aaadda2a8308cc34d6794e/modules/backend/src/main/scala/OutboxConsumer.scala#L47-L56

hnaderi commented 1 year ago

but when they are run in different time intervals, the listening side does not receive information

What do you mean by different time intervals? Are they running in parallel? Can you provide an example and its results?

burakakca commented 1 year ago

What do you mean by different time intervals? Are they running in parallel?

Yes, running on different zio fibers.

hnaderi commented 1 year ago

Is your problem solved?