title: Scala scheduling for a fixed interval between runs
body: <p>I would like to poll a database on an interval. To avoid server load a main concern is avoiding overlapping runs <em>as well as runs not evenly spaced on the time axis</em>; Which would otherwise occur with a naive timer scheme. </p>
  
  <p>Differently put: I would like to have a fixed interval between a run's end and the next one's start time.</p>
  
  <p>Trying to avoid Actors just because of the code boilerplate that they bring in, I have tried this below. Maybe there is a better or just simpler way.</p>
  
  <pre><code>  /* 
     * Infinite scheduling for acquiring a database snapshot at an almost fixed interval.
     * The actual effective acquisition interval is (duration of the acquisition + dbSyncIntervalSecs)  
     */
  
    import scala.concurrent.duration._
    import scala.concurrent.blocking
    import scala.concurrent.Future
    import scala.concurrent.ExecutionContext.Implicits.global
  
    val system = ActorSystem("MySystem") // make global if more things hinge on the actor system
  
    val interval = Configuration.dbSyncIntervalSecs
  
    private def acquireLoop: Unit = {
      Future { 
        blocking { 
          println("about to sync from database")
          acquire
        }
      }
      system.scheduler.scheduleOnce(interval seconds)(acquireLoop)
    }
  </code></pre>
  
  <p>Any advice?</p>
  
  <p>Thread pool looks healthy though:</p>
  
  <p><a href="https://i.stack.imgur.com/4Ob4D.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4Ob4D.jpg" alt="enter image description here"></a></p>
  
  <p>Also, should I better use a separate execution context of a certain kind for this ongoing process, rather than piggyback the default one with the <code>blocking</code> idiom?</p>
  
  <p>Thanks!</p>