【问题标题】:How to make monix fixed rate Scheduler continue upon failure如何使 monix 固定速率调度程序在失败时继续
【发布时间】:2019-11-18 16:54:34
【问题描述】:

我刚刚开始使用 monix,部分原因是为了在长时间运行的应用程序中安排重复工作。我将管理异常,但我希望 monix 继续调用给定的函数,即使我让它们通过其中的一些。

现在,从一个简单的测试来看,一旦安排了重复调用,一旦出现异常就不会继续调用它:

// will print "Hi" repeatedly
scheduler.scheduleAtFixedRate(5.milliseconds, 2.milliseconds) {
  println("Hi")
}

// will print "Hi" only once
scheduler.scheduleAtFixedRate(5.milliseconds, 2.milliseconds) {
  println("Hi")
  throw new RuntimeException("oups, forgot to catch that one")
}

注意:我创建调度程序来记录异常和错误

编辑:

我意识到在失败时简单地重复该任务是一个糟糕的设计。相反,我实际上应该设置一个适当的异常管理系统,并延​​迟重启。

现在,我看不到 Monix 中有任何功能可以做到这一点。所以我必须自己做。如果有人遇到同样的问题,或者有人知道有用的 monix 工具,我会提出这个问题。

【问题讨论】:

    标签: scala exception monix


    【解决方案1】:

    另一种方法是使用Observable,这样可以更轻松地编写。它还具有许多内置功能,因此您无需手动处理自己的功能。

    val myTask: Task[Unit] = Task.delay(println("Execute Task")
    
    // Note that the period will tick after you've completed the task
    // So, if you have a long running task, just make sure you are aware of that
    Observable
      .intervalAtFixedRate(1.seconds, 1.seconds)
      .mapEval(_ => myTask.onErrorRestart(maxRetries = 5))
      .completedL
      .startAndForget // Optional, will run in a separate fiber, so it doesn't block main thread
      .runToFuture
    

    【讨论】:

      【解决方案2】:

      您始终可以利用 scala.util.Try 或简单的 try-catch 块。在任何失败的情况下,您只需登录并继续前进。您甚至可以有如下失败重试策略。

      import scala.util._
      
      def taskExceptionProne() = ???
      
      var failures = 0
      val maxRetries = 10
      
      scheduler.scheduleAtFixedRate(5.milliseconds, 2.milliseconds) {
          Try(taskExceptionProne) match {
              Success(result) =>
                  //do something with the result
                  failures = 0
                  println("Task executed.")
              Failure(throwable) =>
                  if (failures>=maxRetries) throw throwable else {
                      failures = failures + 1
                      //log failure
                      println(throwable.getMessage)
                  }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-09-23
        • 1970-01-01
        • 2018-08-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多