mirror of
https://github.com/TheAnachronism/docspell.git
synced 2025-06-22 02:18:26 +00:00
Hooking the new pubsub impl into the application
This commit is contained in:
@ -155,23 +155,16 @@ final class NaivePubSub[F[_]: Async](
|
||||
|
||||
for {
|
||||
_ <- logger.trace(s"Find all nodes subscribed to topic ${msg.head.topic.name}")
|
||||
urls <- store.transact(RPubSub.findSubs(msg.head.topic.name))
|
||||
urls <- store.transact(RPubSub.findSubs(msg.head.topic.name, cfg.nodeId))
|
||||
_ <- logger.trace(s"Publishing to remote urls ${urls.map(_.asString)}: $msg")
|
||||
reqs = urls
|
||||
.map(u => Uri.unsafeFromString(u.asString))
|
||||
.map(uri => POST(List(msg), uri))
|
||||
res <- reqs.traverse(req => client.status(req)).attempt
|
||||
_ <- res match {
|
||||
resList <- reqs.traverse(req => client.status(req).attempt)
|
||||
_ <- resList.traverse {
|
||||
case Right(s) =>
|
||||
if (s.forall(_.isSuccess)) ().pure[F]
|
||||
else if (s.size == urls.size)
|
||||
logger.warn(
|
||||
s"No nodes was be reached! Reason: $s, message: $msg"
|
||||
)
|
||||
else
|
||||
logger.warn(
|
||||
s"Some nodes were not reached! Reason: $s, message: $msg"
|
||||
)
|
||||
if (s.isSuccess) ().pure[F]
|
||||
else logger.warn(s"A node was not reached! Reason: $s, message: $msg")
|
||||
case Left(ex) =>
|
||||
logger.error(ex)(s"Error publishing ${msg.head.topic.name} message remotely")
|
||||
}
|
||||
|
@ -13,8 +13,8 @@ import cats.implicits._
|
||||
import fs2.concurrent.SignallingRef
|
||||
|
||||
import docspell.common._
|
||||
import docspell.pubsub.api.Topics.{JobDoneMsg, JobSubmittedMsg}
|
||||
import docspell.pubsub.api._
|
||||
import docspell.pubsub.naive.Topics._
|
||||
|
||||
import munit.CatsEffectSuite
|
||||
|
||||
@ -104,7 +104,7 @@ class NaivePubSubTest extends CatsEffectSuite with Fixtures {
|
||||
}
|
||||
|
||||
pubsubEnv.test("do not receive remote message from other topic") { env =>
|
||||
val msg = JobDoneMsg("job-1".id, "task-2".id)
|
||||
val msg = JobCancelMsg("job-1".id)
|
||||
|
||||
// Create two pubsub instances connected to the same database
|
||||
conntectedPubsubs(env).use { case (ps1, ps2) =>
|
||||
@ -112,7 +112,7 @@ class NaivePubSubTest extends CatsEffectSuite with Fixtures {
|
||||
// subscribe to ps1 and send via ps2
|
||||
res <- subscribe(ps1, Topics.jobSubmitted)
|
||||
(received, halt, subFiber) = res
|
||||
_ <- ps2.publish1(Topics.jobDone, msg)
|
||||
_ <- ps2.publish1(Topics.jobCancel, msg)
|
||||
_ <- IO.sleep(100.millis)
|
||||
_ <- halt.set(true)
|
||||
outcome <- subFiber.join
|
||||
|
@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright 2020 Eike K. & Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
package docspell.pubsub.naive
|
||||
|
||||
import cats.data.NonEmptyList
|
||||
|
||||
import docspell.common.Ident
|
||||
import docspell.pubsub.api._
|
||||
|
||||
import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder}
|
||||
import io.circe.{Decoder, Encoder}
|
||||
|
||||
object Topics {
|
||||
val jobSubmitted: TypedTopic[JobSubmittedMsg] =
|
||||
TypedTopic[JobSubmittedMsg](Topic("test-job-submitted"))
|
||||
|
||||
final case class JobSubmittedMsg(task: Ident)
|
||||
object JobSubmittedMsg {
|
||||
implicit val encode: Encoder[JobSubmittedMsg] = deriveEncoder[JobSubmittedMsg]
|
||||
implicit val decode: Decoder[JobSubmittedMsg] = deriveDecoder[JobSubmittedMsg]
|
||||
}
|
||||
|
||||
val jobCancel: TypedTopic[JobCancelMsg] =
|
||||
TypedTopic[JobCancelMsg](Topic("test-job-done"))
|
||||
final case class JobCancelMsg(id: Ident)
|
||||
object JobCancelMsg {
|
||||
implicit val encode: Encoder[JobCancelMsg] = deriveEncoder[JobCancelMsg]
|
||||
implicit val decode: Decoder[JobCancelMsg] = deriveDecoder[JobCancelMsg]
|
||||
}
|
||||
|
||||
def all: NonEmptyList[TypedTopic[_]] =
|
||||
NonEmptyList.of(jobSubmitted, jobCancel)
|
||||
}
|
Reference in New Issue
Block a user