Skip to content

Commit

Permalink
Ony save time when result contains timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
lenguyenthanh committed Dec 4, 2024
1 parent b6db210 commit 6ed509b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
11 changes: 6 additions & 5 deletions modules/ingestor/src/main/scala/ingestor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ object Ingestor:
.eval(startAt)
.flatMap(repo.watch)
.evalMap: result =>
updateElastic(result, false) *> saveLastIndexedTimestamp(index, result.timestamp)
updateElastic(result, false) *> saveLastIndexedTimestamp(result.timestamp)
.compile
.drain

Expand Down Expand Up @@ -87,7 +87,8 @@ object Ingestor:
.whenA(sources.nonEmpty)
*> Logger[IO].info(s"Indexed ${sources.size} ${index.value}s")

private def saveLastIndexedTimestamp(index: Index, time: Option[Instant]): IO[Unit] =
val savedTime = time.getOrElse(Instant.now())
store.put(index.value, savedTime)
*> Logger[IO].info(s"Stored last indexed time ${savedTime.getEpochSecond} for $index")
private val saveLastIndexedTimestamp: Option[Instant] => IO[Unit] =
_.traverse_(time =>
store.put(index.value, time)
*> Logger[IO].info(s"Stored last indexed time ${time.getEpochSecond} for $index")
)
5 changes: 3 additions & 2 deletions modules/ingestor/src/main/scala/mongo.study.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ object StudyRepo:
fs2.Stream.eval(info"Fetching studies from $since to $until") *>
pullForIndex(since, until)
.merge(pullForDelete(since, until))
++ fs2.Stream(Result(Nil, Nil, until.some))

def pullForIndex(since: Instant, until: Instant): fs2.Stream[IO, Result[StudySource]] =
val filter = range(F.createdAt)(since, until.some)
Expand All @@ -56,7 +57,7 @@ object StudyRepo:
.map(_.toList)
// .evalTap(_.traverse_(x => debug"received $x"))
.evalMap(_.toSources)
.map(Result(_, Nil, until.some))
.map(Result(_, Nil, none))

def pullForDelete(since: Instant, until: Instant): fs2.Stream[IO, Result[StudySource]] =
val filter =
Expand All @@ -72,7 +73,7 @@ object StudyRepo:
.chunkN(config.batchSize)
.map(_.toList.flatMap(extractId))
.evalTap(xs => info"Deleting $xs")
.map(Result(Nil, _, until.some))
.map(Result(Nil, _, none))

def extractId(doc: Document): Option[Id] =
doc.getNestedAs[String](F.oplogId).map(Id.apply)
Expand Down

0 comments on commit 6ed509b

Please sign in to comment.