Skip to content

Commit

Permalink
fix(periodic): fix periodic not working
Browse files Browse the repository at this point in the history
  • Loading branch information
lyogev committed Aug 9, 2021
1 parent bc1e587 commit 4138b0a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/main/scala/com/yotpo/metorikku/Metorikku.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.yotpo.metorikku

import java.util.concurrent.{ScheduledThreadPoolExecutor, TimeUnit}

import com.yotpo.metorikku.configuration.job.{ConfigurationParser, Periodic}
import com.yotpo.metorikku.metric.MetricSet
import org.apache.log4j.LogManager
Expand Down Expand Up @@ -34,15 +32,22 @@ object Metorikku extends App {
}

private def executePeriodicTask(periodic: Periodic, job: Job) = {
val task = new Runnable {
def run() = {
sparkSession.catalog.clearCache()
runMetrics(job)
val duration = periodic.getTriggerDurationInMillis()

while(true) {
val start = System.currentTimeMillis
log.info(s"Starting a periodic task at ${start}")
sparkSession.catalog.clearCache()
runMetrics(job)

val period = System.currentTimeMillis - start

if (period < duration) {
val sleepTime = duration - period
log.info(s"Waiting for ${sleepTime} milliseconds before starting next run")
Thread.sleep(duration - period)
}
}
val ex = new ScheduledThreadPoolExecutor(1)
val initialDelay = 0
ex.scheduleAtFixedRate(task, initialDelay, periodic.getTriggerDurationInSeconds(), TimeUnit.SECONDS)
}

def runMetrics(job: Job): Unit = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,12 @@ case class Periodic(triggerDuration: Option[String]) {
case e: Exception => throw MetorikkuException("Invaiid periodic trigger duration", e)
}
}

def getTriggerDurationInMillis(): Long = {
try {
Duration(triggerDuration.get).toMillis
} catch {
case e: Exception => throw MetorikkuException("Invaiid periodic trigger duration", e)
}
}
}

0 comments on commit 4138b0a

Please sign in to comment.