-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add helper UI for running batch processing tasks #34
- Loading branch information
Showing
8 changed files
with
699 additions
and
2 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
74 changes: 74 additions & 0 deletions
74
...as-demos/src/main/scala-3/org/scalafx/extras/batch/BatchRunnerProgressHelperDemoApp.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,74 @@ | ||
/* | ||
* Copyright (c) 2000-2023 Jarek Sacha. All Rights Reserved. | ||
* Author's e-mail: jpsacha at gmail.com | ||
*/ | ||
|
||
package org.scalafx.extras.batch | ||
|
||
import org.scalafx.extras.BusyWorker | ||
import org.scalafx.extras.BusyWorker.SimpleTask | ||
import scalafx.application.JFXApp3 | ||
import scalafx.application.JFXApp3.PrimaryStage | ||
import scalafx.geometry.Insets | ||
import scalafx.scene.Scene | ||
import scalafx.scene.control.{Button, Label} | ||
import scalafx.scene.layout.VBox | ||
import scalafx.stage.Window | ||
|
||
import scala.util.Random | ||
|
||
object BatchRunnerProgressHelperDemoApp extends JFXApp3: | ||
|
||
private lazy val busyWorker = new BusyWorker(Title, parentWindow) | ||
private val Title = "Batch Processing / Progress Dialog Demo" | ||
|
||
private val nTasks = 100 | ||
private val minTime = 500 | ||
private val maxTime = 1000 | ||
|
||
override def start(): Unit = | ||
stage = new PrimaryStage: | ||
title = Title | ||
scene = new Scene: | ||
content = new VBox: | ||
padding = Insets(21) | ||
spacing = 14 | ||
children = Seq( | ||
new Label( | ||
s"""Press "Run" to initiate processing. | ||
|Wait till processing finished or press "Abort". | ||
|There will be $nTasks processed. | ||
|Tasks will have randomly generated time between $minTime and $maxTime ms. | ||
|If task's time is divisible by 6, that task will fail. | ||
|""".stripMargin | ||
), | ||
new Button("Run Sequential"): | ||
onAction = (_) => onStart(false) | ||
prefWidth = 120 | ||
, | ||
new Button(" Run Parallel "): | ||
onAction = (_) => onStart(false) | ||
prefWidth = 120 | ||
) | ||
|
||
private def onStart(useParallelProcessing: Boolean): Unit = busyWorker.doTask("Start") { | ||
new SimpleTask[Unit]: | ||
override def call(): Unit = | ||
val helper = | ||
new BatchRunnerProgressHelper[String]("Sample batch processing", parentWindow, useParallelProcessing): | ||
override def createSampleTasks(): Seq[ItemTask[String]] = | ||
val r = new Random() | ||
(1 to nTasks).map { i => | ||
new ItemTask[String]: | ||
override val name = s"Task #$i" | ||
|
||
override def run(): String = | ||
val t = minTime + r.nextInt(maxTime - minTime) | ||
Thread.sleep(t) | ||
if t % 6 == 0 then throw new Exception(s"Do not like ${t}") | ||
s"name t = $t" | ||
} | ||
helper.run() | ||
} | ||
|
||
def parentWindow: Option[Window] = Option(stage) |
80 changes: 80 additions & 0 deletions
80
scalafx-extras-demos/src/main/scala-3/org/scalafx/extras/batch/ParallelBatchRunnerDemo.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,80 @@ | ||
/* | ||
* Copyright (c) 2011-2024, ScalaFX Project | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* * Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* * Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* * Neither the name of the ScalaFX Project nor the | ||
* names of its contributors may be used to endorse or promote products | ||
* derived from this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | ||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
* DISCLAIMED. IN NO EVENT SHALL THE SCALAFX PROJECT OR ITS CONTRIBUTORS BE LIABLE | ||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | ||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
package org.scalafx.extras.batch | ||
|
||
import scala.util.{Failure, Success} | ||
|
||
|
||
object ParallelBatchRunnerDemo { | ||
class DemoTaskItem(n: Int) extends ItemTask[Int] { | ||
val name = s"Demo TaskItem $n" | ||
|
||
def run(): Int = { | ||
// println(s"Item ${n} - start") | ||
Thread.sleep(300) | ||
if n == 7 then | ||
// println(s"Item ${n} - error") | ||
throw new IllegalArgumentException(s"Don't give me $n") | ||
|
||
// println(s"Item ${n} - end") | ||
n | ||
} | ||
} | ||
|
||
|
||
def main(args: Array[String]): Unit = { | ||
val items: Seq[DemoTaskItem] = Range(0, 10).map { i => new DemoTaskItem(i) } | ||
|
||
val batchHelper = new ParallelBatchRunner(items, progressUpdate, useParallelProcessing = true) | ||
|
||
val results = batchHelper.execute() | ||
|
||
println() | ||
println("Summarize processing") | ||
results.foreach { | ||
case (name, Success(r)) => println(s"$name: SUCCESS: $r") | ||
case (name, Failure(e)) => println(s"$name: ERROR : ${e.getMessage}") | ||
} | ||
} | ||
|
||
def progressUpdate(running : Long, | ||
successful: Long, | ||
failed : Long, | ||
canceled : Long, | ||
executed : Long, | ||
total : Long, | ||
isCanceled : Boolean, | ||
perc : Double, | ||
message : String) : Unit = { | ||
val m = | ||
f"R:$running%2d, S:$successful%2d, F:$failed%2d, E:$executed%2d, C:$canceled, T:$total%d, " + | ||
f"C:$isCanceled, perc:${perc.toInt}%3d, $message" | ||
println(m) | ||
} | ||
|
||
} |
55 changes: 55 additions & 0 deletions
55
scalafx-extras/src/main/scala-3/org/scalafx/extras/batch/BatchRunner.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,55 @@ | ||
/* | ||
* Copyright (c) 2011-2024, ScalaFX Project | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* * Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* * Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* * Neither the name of the ScalaFX Project nor the | ||
* names of its contributors may be used to endorse or promote products | ||
* derived from this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | ||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
* DISCLAIMED. IN NO EVENT SHALL THE SCALAFX PROJECT OR ITS CONTRIBUTORS BE LIABLE | ||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | ||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
package org.scalafx.extras.batch | ||
|
||
object BatchRunner { | ||
|
||
@FunctionalInterface | ||
trait ProgressUpdater: | ||
def update( | ||
running: Long, | ||
successful: Long, | ||
failed: Long, | ||
canceled: Long, | ||
executed: Long, | ||
total: Long, | ||
isCanceled: Boolean, | ||
perc: Double, | ||
message: String | ||
): Unit | ||
} | ||
trait BatchRunner[T, I <: ItemTask[T]] { | ||
|
||
import BatchRunner.ProgressUpdater | ||
|
||
// TODO: Is this trait needed | ||
|
||
protected def itemTasks: Seq[I] | ||
|
||
protected def progressUpdater: ProgressUpdater | ||
} |
Oops, something went wrong.