-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from dvgica/add-runner
Add runner and docs
- Loading branch information
Showing
11 changed files
with
158 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
periodic-core/src/main/scala/ca/dvgi/periodic/FnRunner.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package ca.dvgi.periodic | ||
|
||
import ca.dvgi.periodic.jdk._ | ||
import scala.concurrent.duration.FiniteDuration | ||
import org.slf4j.LoggerFactory | ||
import scala.concurrent.duration._ | ||
import scala.concurrent.Future | ||
|
||
/** A FnRunner executes a side-effecting function periodically. The `FiniteDuration` returned from | ||
* the function determines the delay before the next run. | ||
* | ||
* Failed runs may be retried with various configurations. | ||
* | ||
* @param periodic | ||
* A Periodic instance used to run the function | ||
* @param fn | ||
* The side-effecting function to run periodically | ||
* @param fnAttemptStrategy | ||
* Configuration for retrying runs on failure | ||
* @param fnName | ||
* A human-friendly description of the function, used in logging | ||
* @param initialDelay | ||
* If specified, the first run of the function will be delayed this much | ||
*/ | ||
class FnRunner[F[_], R[_]](periodic: Periodic[F, R])( | ||
fn: => F[FiniteDuration], | ||
fnAttemptStrategy: AttemptStrategy, | ||
fnName: String, | ||
initialDelay: FiniteDuration = 0.seconds | ||
) extends AutoCloseable { | ||
|
||
private val log = LoggerFactory.getLogger(s"FnRunner[$fnName]") | ||
|
||
log.info(s"Starting. ${fnAttemptStrategy.description}") | ||
|
||
periodic.scheduleRecurring[FiniteDuration]( | ||
log, | ||
fnName, | ||
initialDelay, | ||
() => fn, | ||
_ => (), | ||
identity, | ||
fnAttemptStrategy | ||
) | ||
|
||
override def close(): Unit = { | ||
periodic.close() | ||
log.info(s"Shut down sucessfully") | ||
} | ||
} | ||
|
||
object FnRunner { | ||
def apply[F[_], R[_]](periodic: Periodic[F, R])( | ||
fn: => F[FiniteDuration], | ||
fnAttemptStrategy: AttemptStrategy, | ||
fnName: String, | ||
initialDelay: FiniteDuration = 0.seconds | ||
): FnRunner[F, R] = new FnRunner(periodic)(fn, fnAttemptStrategy, fnName, initialDelay) | ||
|
||
def jdk[F[_]]( | ||
fn: => FiniteDuration, | ||
fnAttemptStrategy: AttemptStrategy, | ||
fnName: String, | ||
initialDelay: FiniteDuration = 0.seconds | ||
): FnRunner[Identity, Future] = | ||
new FnRunner(JdkPeriodic[Identity]())( | ||
fn, | ||
fnAttemptStrategy, | ||
fnName, | ||
initialDelay | ||
) | ||
|
||
def jdkFuture[F[_]]( | ||
fn: => Future[FiniteDuration], | ||
fnAttemptStrategy: AttemptStrategy, | ||
fnName: String, | ||
initialDelay: FiniteDuration = 0.seconds | ||
): FnRunner[Future, Future] = | ||
new FnRunner(JdkPeriodic[Future]())( | ||
fn, | ||
fnAttemptStrategy, | ||
fnName, | ||
initialDelay | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.