# Path: scribeui_pyramid/modules/plugins/mapcache/models.py
# class Job(Base, BaseMixin):
#     __tablename__ = 'jobs'
# 
#     id = sa.Column(sa.Integer, primary_key=True, autoincrement=True)
#     title = sa.Column(sa.Unicode(255), nullable=False)
#     status = sa.Column(sa.Integer, nullable=False)
# 
#     map_id = sa.Column(sa.Integer, sa.ForeignKey('maps.id'),
#                              nullable=False)
# 
#     def __repr__(self):
#         return u"<Job('{0}')>".format(self.title)


from subprocess import Popen
from sqlalchemy.orm.exc import NoResultFound
from sqlalchemy import exc
from .models import Job
import pprint
import time
import threading
import os
import transaction
# Pokes the processes to see their exit status. Stops when there is no running thread left.
class processManager(Borg):
    class pollProcesses (threading.Thread):
        def __init__(self, pManager):
            threading.Thread.__init__(self)
            self.pManager = pManager
        def run(self):
            while True:
                #poll exit codes:
                #None   Running
                #0      Finished nicely 
                #-n     terminated by signal n
                time.sleep(30)
                pprint.pprint("-----------")
                stop = True
                self.pManager.lock.acquire()
                updatedProcesses = []
                for p in self.pManager.processes:
                    keep = True
                    status = p.poll()
                    pprint.pprint(str(p.jobid)+" "+str(p.pid)+" "+str(status))
                    # If the job is running
                    if status is None:
                        stop = False # keep the thread alive
                        job = self.pManager.getJob(p.jobid)
                        if job.status == 2:
                            self.pManager.stopProcess(p.jobid, True)
                            continue;
                    else:
                        if status != 0:

# Path: scribeui_pyramid/modules/plugins/mapcache/models.py
# class Job(Base, BaseMixin):
#     __tablename__ = 'jobs'
# 
#     id = sa.Column(sa.Integer, primary_key=True, autoincrement=True)
#     title = sa.Column(sa.Unicode(255), nullable=False)
#     status = sa.Column(sa.Integer, nullable=False)
# 
#     map_id = sa.Column(sa.Integer, sa.ForeignKey('maps.id'),
#                              nullable=False)
# 
#     def __repr__(self):
#         return u"<Job('{0}')>".format(self.title)


# Path: scribeui_pyramid/modules/plugins/mapcache/processManager.py
from subprocess import Popen
from sqlalchemy.orm.exc import NoResultFound
from sqlalchemy import exc
from .models import Job
import pprint
import time
import threading
import os
import transaction

# Pokes the processes to see their exit status. Stops when there is no running thread left.
class processManager(Borg):
    class pollProcesses (threading.Thread):
        def __init__(self, pManager):
            threading.Thread.__init__(self)
            self.pManager = pManager
        def run(self):
            while True:
                #poll exit codes:
                #None   Running
                #0      Finished nicely 
                #-n     terminated by signal n
                time.sleep(30)
                pprint.pprint("-----------")
                stop = True
                self.pManager.lock.acquire()
                updatedProcesses = []
                for p in self.pManager.processes:
                    keep = True
                    status = p.poll()
                    pprint.pprint(str(p.jobid)+" "+str(p.pid)+" "+str(status))
                    # If the job is running
                    if status is None:
                        stop = False # keep the thread alive
                        job = self.pManager.getJob(p.jobid)
                        if job.status == 2:
                            self.pManager.stopProcess(p.jobid, True)
                            continue;
                    else:
                        if status != 0:

                            status = 2 