Permalink
Browse files

redeploy

  • Loading branch information...
purplefox committed Jul 27, 2012
1 parent bd3626e commit 8ee849068020d263703a1e2b4f56198f03d9dc2a
@@ -0,0 +1,7 @@
+load("vertx.js")
+
+console.log("in test-mod1");
+
+vertx.createHttpServer().requestHandler(function(req) {
+ req.response.end("<html><body><h1>Hello from vert.x!</h1></body></html>");
+}).listen(8080, 'localhost');
@@ -0,0 +1,4 @@
+{
+ "main": "app.js",
+ "auto-redeploy": true
+}
@@ -60,6 +60,9 @@ private boolean isJavaSource(String main) {
}
public Verticle createVerticle(String main, ClassLoader loader) throws Exception {
+
+ System.out.println("creating verticle " + main);
+
ClassLoader cl = loader;
String className = main;
if (isJavaSource(main)) {
@@ -89,7 +89,7 @@ public void deployWorkerVerticle(String main, JsonObject config, int instances)
public void deployWorkerVerticle(String main, JsonObject config, int instances, Handler<String> doneHandler) {
URL[] currURLs = mgr.getDeploymentURLs();
File modDir = mgr.getDeploymentModDir();
- mgr.deploy(true, main, config, currURLs, instances, modDir, doneHandler);
+ mgr.deployVerticle(true, main, config, currURLs, instances, modDir, doneHandler);
}
/**
@@ -186,7 +186,7 @@ public void deployVerticle(String main, JsonObject config, int instances) {
public void deployVerticle(String main, JsonObject config, int instances, Handler<String> doneHandler) {
URL[] currURLs = mgr.getDeploymentURLs();
File modDir = mgr.getDeploymentModDir();
- mgr.deploy(false, main, config, currURLs, instances, modDir, doneHandler);
+ mgr.deployVerticle(false, main, config, currURLs, instances, modDir, doneHandler);
}
/**
@@ -38,9 +38,11 @@
public final List<VerticleHolder> verticles = new ArrayList<>();
public final List<String> childDeployments = new ArrayList<>();
public final String parentDeploymentName;
+ public final boolean autoRedeploy;
public Deployment(String name, String modName, int instances, VerticleFactory factory, JsonObject config,
- URL[] urls, File modDir, String parentDeploymentName) {
+ URL[] urls, File modDir, String parentDeploymentName,
+ boolean autoRedeploy) {
this.name = name;
this.modName = modName;
this.instances = instances;
@@ -49,5 +51,6 @@ public Deployment(String name, String modName, int instances, VerticleFactory fa
this.urls = urls;
this.modDir = modDir;
this.parentDeploymentName = parentDeploymentName;
+ this.autoRedeploy = autoRedeploy;
}
}
@@ -56,7 +56,6 @@
private final File modRoot;
private final ModuleReloader reloader;
- private final Map<String, Deployment> deployments = new HashMap<>();
private final Map<Path, Set<Deployment>> watchedDeployments = new HashMap<>();
private final Map<WatchKey, Path> watchKeys = new HashMap<>();
private final Map<Path, Path> moduleDirs = new HashMap<>();
@@ -68,24 +67,6 @@
private final Queue<Deployment> toUndeploy = new ConcurrentLinkedQueue<>();
private Context ctx;
- private void dumpSizes() {
- log.info("watchkeys: " + watchKeys.size());
- log.info("moduleDirs: " + moduleDirs.size());
- log.info("changing: " + changing.size());
- int size = 0;
- for (Set<Deployment> s: this.watchedDeployments.values()) {
- size += s.size();
- }
- log.info("watcheddeployments:" + size);
- }
-
- private void checkContext() {
- //Sanity check
- if (Context.getContext() != ctx) {
- throw new IllegalStateException("Got context: " + Context.getContext() + " expected " + ctx);
- }
- }
-
public Redeployer(Vertx vertx, File modRoot, ModuleReloader reloader) {
this.modRoot = modRoot;
this.reloader = reloader;
@@ -118,12 +99,10 @@ public void close() {
}
public void moduleDeployed(Deployment deployment) {
- log.info("module deployed " + deployment.name);
toDeploy.add(deployment);
}
public void moduleUndeployed(Deployment deployment) {
- log.info("module undeployed " + deployment.name);
toUndeploy.add(deployment);
}
@@ -134,7 +113,6 @@ public void moduleUndeployed(Deployment deployment) {
private void processDeployments() {
Deployment dep;
while ((dep = toDeploy.poll()) != null) {
- deployments.put(dep.name, dep);
File fmodDir = new File(modRoot, dep.modName);
Path modDir = fmodDir.toPath();
Set<Deployment> deps = watchedDeployments.get(modDir);
@@ -156,14 +134,11 @@ private void processDeployments() {
private void processUndeployments() {
Deployment dep;
while ((dep = toUndeploy.poll()) != null) {
- log.info("processing undeployment");
- deployments.remove(dep.name);
File modDir = new File(modRoot, dep.modName);
Path pModDir = modDir.toPath();
Set<Deployment> deps = watchedDeployments.get(pModDir);
deps.remove(dep);
if (deps.isEmpty()) {
- log.info("unwatching module " + dep.modName);
watchedDeployments.remove(pModDir);
Set<Path> modPaths = new HashSet<>();
for (Map.Entry<Path, Path> entry: moduleDirs.entrySet()) {
@@ -186,7 +161,6 @@ private void processUndeployments() {
watchKeys.remove(key);
}
}
- this.dumpSizes();
}
}
@@ -210,35 +184,20 @@ void checkEvents() {
if (now - entry.getValue() > GRACE_PERIOD) {
// Module has changed but no changes for GRACE_PERIOD ms
// we can assume the redeploy has finished
- log.info("module hasn't changed for 1000 ms so redeploy it");
toRedeploy.add(entry.getKey());
}
}
if (!toRedeploy.isEmpty()) {
- Set<Deployment> parents = new HashSet<>();
+ Set<Deployment> deployments = new HashSet<>();
for (Path moduleDir: toRedeploy) {
+ log.info("Module has changed - redeploying module from directory " + moduleDir.toString());
changing.remove(moduleDir);
- computeParents(moduleDir, parents);
+ deployments.addAll(watchedDeployments.get(moduleDir));
}
- reloader.reloadModules(parents);
+ reloader.reloadModules(deployments);
}
}
- // Some of the modules that need to be redeployed might have been programmatically
- // deployed so we don't redeploy them directly. Instead we need to compute the
- // set of parent modules which are the ones we need to redeploy
- private void computeParents(Path modulePath, Set<Deployment> parents) {
- Set<Deployment> deps = watchedDeployments.get(modulePath);
- for (Deployment d: deps) {
- if (d.parentDeploymentName != null) {
- Deployment parent = deployments.get(d.parentDeploymentName);
- File f = new File(modRoot, parent.modName);
- computeParents(f.toPath(), parents);
- } else {
- parents.add(d);
- }
- }
- }
private void handleEvent(WatchKey key, Set<Path> changed) {
Path dir = watchKeys.get(key);
@@ -254,21 +213,15 @@ private void handleEvent(WatchKey key, Set<Path> changed) {
continue;
}
- log.info("got event in directory " + dir);
-
Path moduleDir = moduleDirs.get(dir);
- log.info("module dir is " + moduleDir);
@SuppressWarnings("unchecked")
WatchEvent<Path> ev = (WatchEvent<Path>) event;
Path name = ev.context();
Path child = dir.resolve(name);
- if (kind == StandardWatchEventKinds.ENTRY_MODIFY) {
- log.info("entry modified: " + child);
- } else if (kind == StandardWatchEventKinds.ENTRY_CREATE) {
- log.info("entry created: " + child);
+ if (kind == StandardWatchEventKinds.ENTRY_CREATE) {
if (Files.isDirectory(child, LinkOption.NOFOLLOW_LINKS)) {
try {
registerAll(moduleDir, child);
@@ -278,7 +231,6 @@ private void handleEvent(WatchKey key, Set<Path> changed) {
}
}
} else if (kind == StandardWatchEventKinds.ENTRY_DELETE) {
- log.info("entry deleted: " + child);
moduleDirs.remove(child);
}
changed.add(moduleDir);
@@ -291,15 +243,13 @@ private void handleEvent(WatchKey key, Set<Path> changed) {
}
private void register(Path modDir, Path dir) throws IOException {
- log.info("registering " + dir);
WatchKey key = dir.register(watchService, StandardWatchEventKinds.ENTRY_CREATE,
StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE);
watchKeys.put(key, dir);
moduleDirs.put(dir, modDir);
}
private void registerAll(final Path modDir, final Path dir) throws IOException {
- log.info("registering all " + modDir);
Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
@@ -308,4 +258,23 @@ public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) th
}
});
}
+
+// private void dumpSizes() {
+// log.info("watchkeys: " + watchKeys.size());
+// log.info("moduleDirs: " + moduleDirs.size());
+// log.info("changing: " + changing.size());
+// int size = 0;
+// for (Set<Deployment> s: this.watchedDeployments.values()) {
+// size += s.size();
+// }
+// log.info("watcheddeployments:" + size);
+// }
+
+ private void checkContext() {
+ //Sanity check
+ if (Context.getContext() != ctx) {
+ throw new IllegalStateException("Got context: " + Context.getContext() + " expected " + ctx);
+ }
+ }
+
}
Oops, something went wrong.

0 comments on commit 8ee8490

Please sign in to comment.