2017-10-21 221 views
1

使用Akka的調度程序時,我遇到了一個奇怪的行爲。我的代碼看起來大致是這樣的:什麼會導致Akka的調度程序在預定時間之前執行預定任務?

val s = ActorSystem("scheduler") 

    import scala.concurrent.ExecutionContext.Implicits.global 

    def doSomething(): Future[Unit] = { 
     val now = new GregorianCalendar(TimeZone.getTimeZone("UTC")) 
     println(s"${now.get(Calendar.MINUTE)}:${now.get(Calendar.SECOND)}:${now.get(Calendar.MILLISECOND)}") 

     // Do many things that include an http request using "dispatch" and manipulation of the response and saving it in a file. 
    } 

    val futures: Seq[Future[Unit]] = for (i <- 1 to 500) yield { 
     println(s"$i : ${i*600}") 
     // AlphaVantage recommends 100 API calls per minute 
     akka.pattern.after(i * 600 milliseconds, s.scheduler) { doSomething() } 
    } 
    Future.sequence(futures).onComplete(_ => s.terminate()) 

當我執行我的代碼,doSomething最初反覆連續調用之間600毫秒叫,符合市場預期。但是,過了一段時間,所有剩餘的預定呼叫突然同時執行。

我懷疑我的doSomething裏面的東西可能會干擾調度,但我不知道是什麼。我的doSomething只是使用調度來處理http請求並操縱結果,並且不以任何方式直接與akka或調度程序進行交互。所以,我的問題是:

什麼會導致調度程序的調度失敗,並突然觸發立即執行所有剩餘的調度任務?

(PS:我試圖簡化我doSomething張貼在這裏最小非工作例子,但我的簡化導致工作例子。)

回答

0

確定。我想到了。只要其中一個期貨失敗,線路

Future.sequence(futures).onComplete(_ => s.terminate()) 

將終止角色系統,並且所有剩餘的計劃任務將被調用。

相關問題