<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -6,12 +6,20 @@ import datetime;
 import threading;
 import os
 import sys
+from Queue import Queue
 
 CIPY_FOLDER = &quot;.ci&quot;
 
 repo_path = None
 repo_type = None
 
+finished_jobs = Queue();
+
+def save_pending_jobs():
+ while not finished_jobs.empty():
+     w = finished_jobs.get()
+     w.save();
+     finished_jobs.task_done()
 
 def get_repo_type(path):
   &quot;&quot;&quot; return repo type based on configuration folder, i.e .git, .svn &quot;&quot;&quot;
@@ -21,16 +29,10 @@ def get_repo_type(path):
     return &quot;svn&quot;;
   return None
 
-scm_cmds = { 'git': 
-                  {   
-                    'reset':[&quot;git&quot;, &quot;reset&quot;, &quot;--hard&quot;], 
-                    'rev':[&quot;git&quot;, &quot;rev-parse&quot;, &quot;HEAD&quot;] 
-                  },
-                'svn': 
-                  {   
-                    'reset':[&quot;svn&quot;, &quot;update&quot;], 
-                    'rev':[&quot;svnversion&quot;] 
-                  }
+scm_cmds = { 'git': {'reset':[&quot;git&quot;, &quot;reset&quot;, &quot;--hard&quot;], 
+                     'rev':[&quot;git&quot;, &quot;rev-parse&quot;, &quot;HEAD&quot;]},
+                'svn': {'reset':[&quot;svn&quot;, &quot;update&quot;], 
+                        'rev':[&quot;svnversion&quot;] }
               };
 
 #init juno
@@ -52,19 +54,8 @@ def exec_ci_cmd(c):
     build_cmd = join(&quot;.&quot;, CIPY_FOLDER, c);
     return cmd([build_cmd], repo_path);
   return (None, None)
-    
   
-@route('/build')
-def build(web):
-
-  kill_zombies(); #hack
-
-  data, ret = cmd(scm_cmds[repo_type]['rev'], repo_path);
-  b = Build(date=datetime.datetime.now().strftime(&quot;%b%d %H:%M&quot;), finished=False, rev=data[:6])
-  b.save();
-  # i was using a thread before but sqlite doesn't support access to same object from different threads
-  pid = os.fork();
-  if pid == 0:
+def build_work(b):
     cmd(scm_cmds[repo_type]['reset'], repo_path);
     data, ret = exec_ci_cmd(&quot;build&quot;);
     if ret != None:
@@ -73,27 +64,31 @@ def build(web):
     else:
       b.output = &quot;%s file not found, i don't know how to build&quot; % join(CIPY_FOLDER, &quot;build&quot;);
     b.finished = True;
-    b.save();
-
+    finished_jobs.put(b)
     # hooks
     if ret == 0:
       exec_ci_cmd(&quot;build_pass&quot;);
     else:
       exec_ci_cmd(&quot;build_failed&quot;);
-    os._exit(0);
+  
+  
+@route('/build')
+def build(web):
+  save_pending_jobs();
+
+  #get revision
+  data, ret = cmd(scm_cmds[repo_type]['rev'], repo_path);
+  b = Build(date=datetime.datetime.now().strftime(&quot;%b%d %H:%M&quot;), finished=False, rev=data[:6])
+  b.save();
+
+  # launch build thread
+  threading.Thread(target=build_work, args=(b,)).start();
   return &quot;scheduled!&quot;
  
-def kill_zombies():
-  try:
-    while 1:
-            os.waitpid(0, os.WNOHANG)
-  except:
-    pass
 
 @route('/')
 def index(web):
-  # terminate pending forked process
-  kill_zombies();
+  save_pending_jobs();
   builds = find(Build).order_by(Build.id.desc()).limit(10).all();
   template(&quot;index.html&quot;, { 'builds': builds, 'project_path': repo_path })
   </diff>
      <filename>run.py</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>8846268b6fe09d2771c8c6e2d369ab36b28129ab</id>
    </parent>
  </parents>
  <author>
    <name>javi</name>
    <email>globalx@localhost.(none)</email>
  </author>
  <url>http://github.com/javisantana/cipy/commit/b70a1127219bbf4ed8619e41bf5c72860d483f3b</url>
  <id>b70a1127219bbf4ed8619e41bf5c72860d483f3b</id>
  <committed-date>2009-10-22T08:21:56-07:00</committed-date>
  <authored-date>2009-10-22T08:21:56-07:00</authored-date>
  <message>changed to a thread model</message>
  <tree>6113582c544f2a2caaf4066d579221aa71099c21</tree>
  <committer>
    <name>javi</name>
    <email>globalx@localhost.(none)</email>
  </committer>
</commit>
