# 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
    # by the caller, so accessing it is safe
    def stopProcess(self, jobid, locked):
        pprint.pprint("STOP job"+str(jobid))
        if not locked:
            self.lock.acquire()
        for p in self.processes:
            if p.jobid == jobid:
                p.terminate()
        if not locked:
            self.lock.release()
        return

    def updateJob(self, jobid, status):
        try:
            job = Job.by_id(jobid)
        except NoResultFound as e:
            print "Error %s:" % e.args[0]
            raise Exception(e.args[0])

        try:
            job.status = status
            transaction.commit()
        except exc.SQLAlchemyError as e:
            print "Error %s:" % e.args[0]
            raise Exception(e.args[0])


    def getJob(self, jobid):
        job = None
        try:

# 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

    # by the caller, so accessing it is safe
    def stopProcess(self, jobid, locked):
        pprint.pprint("STOP job"+str(jobid))
        if not locked:
            self.lock.acquire()
        for p in self.processes:
            if p.jobid == jobid:
                p.terminate()
        if not locked:
            self.lock.release()
        return

    def updateJob(self, jobid, status):
        try:
            job = Job.by_id(jobid)
        except NoResultFound as e:
            print "Error %s:" % e.args[0]
            raise Exception(e.args[0])

        try:
            job.status = status
            transaction.commit()
        except exc.SQLAlchemyError as e:
            print "Error %s:" % e.args[0]
            raise Exception(e.args[0])


    def getJob(self, jobid):
        job = None
        try:

            job = Job.by_id(jobid)