Skip to content
This repository was archived by the owner on Aug 18, 2020. It is now read-only.

Commit 0475ce0

Browse files
committed
Wait till all dependencies are ready before running plugin instantiation tests
1 parent 2b9d6ab commit 0475ce0

File tree

3 files changed

+26
-15
lines changed

3 files changed

+26
-15
lines changed

src/main/scala/org/codeoverflow/chatoverflow/framework/PluginFramework.scala

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import org.codeoverflow.chatoverflow.framework.helper.PluginLoader
77
import org.codeoverflow.chatoverflow.framework.manager.PluginManagerStub
88

99
import scala.collection.mutable.ListBuffer
10+
import scala.concurrent.ExecutionContext.Implicits.global
11+
import scala.concurrent.duration._
12+
import scala.concurrent.{Await, Future}
13+
import scala.util.Success
1014

1115
/**
1216
* The plugin framework holds all plugin types important from the jar files in the plugin folder.
@@ -62,6 +66,8 @@ class PluginFramework(pluginDirectoryPath: String) extends WithLogger {
6266
logger warn s"PluginType directory '$pluginDirectory' does not exist!"
6367
} else {
6468

69+
val futures = ListBuffer[Future[_]]()
70+
6571
// Get (new) jar file urls
6672
val jarFiles = getNewJarFiles(pluginDirectory)
6773
logger info s"Found ${jarFiles.length} new plugins."
@@ -81,22 +87,27 @@ class PluginFramework(pluginDirectoryPath: String) extends WithLogger {
8187
} else {
8288

8389
// Try to test the initiation of the plugin
84-
try {
85-
plugin.createPluginInstance(new PluginManagerStub)
86-
logger info s"Successfully tested instantiation of plugin '${plugin.getName}'"
87-
pluginTypes += plugin
88-
} catch {
89-
// Note that we catch not only exceptions, but also errors like NoSuchMethodError. Deep stuff
90-
case _: Error => logger warn s"Error while test init of plugin '${plugin.getName}'."
91-
case _: Exception => logger warn s"Exception while test init of plugin '${plugin.getName}'."
90+
futures += plugin.getDependencyFuture andThen {
91+
case Success(_) =>
92+
try {
93+
plugin.createPluginInstance(new PluginManagerStub)
94+
logger info s"Successfully tested instantiation of plugin '${plugin.getName}'"
95+
pluginTypes += plugin
96+
} catch {
97+
// Note that we catch not only exceptions, but also errors like NoSuchMethodError. Deep stuff
98+
case _: Error => logger warn s"Error while test init of plugin '${plugin.getName}'."
99+
case _: Exception => logger warn s"Exception while test init of plugin '${plugin.getName}'."
100+
}
92101
}
93102
}
94103
}
95104
}
96-
}
97105

98-
logger info s"Loaded ${pluginTypes.length} plugin types in total: " +
99-
s"${pluginTypes.map(pt => s"${pt.getName} (${pt.getAuthor})").mkString(", ")}"
106+
futures.foreach(f => Await.ready(f, 10.seconds))
107+
108+
logger info s"Loaded ${pluginTypes.length} plugin types in total: " +
109+
s"${pluginTypes.map(pt => s"${pt.getName} (${pt.getAuthor})").mkString(", ")}"
110+
}
100111
}
101112

102113
/**

src/main/scala/org/codeoverflow/chatoverflow/instance/PluginInstance.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ class PluginInstance(val instanceName: String, pluginType: PluginType) extends W
100100
} else {
101101

102102
if (!areDependenciesAvailable) {
103-
logger error "Dependencies have either failed to resolve and fetch or aren't done yet."
103+
logger error "Dependencies have failed to resolve and fetch."
104104
return false
105105
}
106106

@@ -230,9 +230,9 @@ class PluginInstance(val instanceName: String, pluginType: PluginType) extends W
230230
}
231231

232232
/**
233-
* Returns whether all dependencies are resolved and fetch which is required for the plugin to start.
233+
* Returns whether all dependencies are resolved and fetched which is required for the plugin to start.
234234
*
235-
* @return true if all dependencies are available, true if not done of failed.
235+
* @return true if all dependencies are available, true if it has failed.
236236
*/
237237
def areDependenciesAvailable: Boolean = {
238238
val opt = pluginType.getDependencyFuture.value

src/main/scala/org/codeoverflow/chatoverflow/ui/web/rest/plugin/PluginInstanceController.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class PluginInstanceController(implicit val swagger: Swagger) extends JsonServle
4747
ResultMessage(success = false, "Not all required requirements have been set.")
4848

4949
} else if (!pluginInstance.get.areDependenciesAvailable) {
50-
ResultMessage(success = false, "Dependencies have either failed to resolve and fetch or aren't done yet. Check logs for further information.")
50+
ResultMessage(success = false, "Dependencies have failed to resolve and fetch. Check logs for further information.")
5151

5252
} else if (!pluginInstance.get.start()) {
5353
ResultMessage(success = false, "Unable to start plugin.")

0 commit comments

Comments
 (0)