Commits

Eric Larson  committed 4741bc2

Added a stop processes function and fixed an error generating port numbers

  • Participants
  • Parent commits d913f9d

Comments (0)

Files changed (1)

File dad/web/plugins/manager.py

         self.bus.subscribe('dad.apps_status', self.status)
 
     def port(self):
-        return self.start_port + (len(self.processes) * 100)
+        if not self.processes:
+            return self.start_port
+        return self.start_port + ((len(self.processes) - 1) * 100)
 
     def update_path(self, env, sbox):
         path = [sbox.path() + '/bin']
         pass
     
     def stop_app(self, name, conf, **kw):
-        pass
+        results = {}
+        for instance, proc in self.processes.get(name, {}).items():
+            if proc.poll():
+                results[instance] = {'returncode': None}
+            else:
+                proc.terminate()
+                proc.wait()
+                results[instance] = {'returncode': proc.poll()}
+
+        # nothing was running
+        if not results:
+            results = dict((instance, {'returncode': None})
+                           for instance in range(conf.get('instances', 1)))
+        return results
     
     def start_app(self, name, conf):
-        cmd = shlex.split(conf['command'])
+        cmd = shlex.split(conf['command'].encode('utf-8'))
 
         for instance in xrange(conf.get('instances', 1)):
 
 
             # add our generated port to the environ
             environ = {
+                'HOST': os.environ.get('HOST', '127.0.0.1'),
                 'PORT': str(port),
                 'PWD': sbox.path(),
             }
             self.update_path(environ, sbox)
 
             # check for replacements in the shell command
-            replacements = []
+            repl = []
+
             for index, token in enumerate(cmd):
                 if token.startswith('$'):
                     value = self.replace_env_vars(token, environ)
                     if value:
                         cmd[index] = value
 
+            # update our environ with any replacements from the dad environ
+            environ = dict((k, self.replace_env_vars(v, environ))
+                           for k, v in environ.items())
+
+            self.bus.log('Starting process: %s %s %s' %
+                         (cmd, environ['HOST'], environ['PORT']))
             # start the process
-            proc = Popen(cmd, cwd=sbox.path(),
-                         stdout=sys.stdout, stderr=STDOUT,
+            proc = Popen(cmd,
+                         cwd=sbox.path(),
+                         stdout=sys.stdout,
+                         stderr=STDOUT,
                          env=environ)
             if not self.processes.get(name):
                 self.processes[name] = {port: proc}