# Path: overc/src/init.py
# def init_db_engine(connect):
#     """ Init Sqlalchemy engine
#     :param connect: Database connection string
#     :type connect: str
#     :rtype: sqlalchemy.engine.Engine
#     """
#     return create_engine(connect, convert_unicode=True)
# 
# def init_db_session(engine):
#     """ Init DB session
#     :param engine: Engine
#     :type engine: sqlalchemy.engine.Engine
#     :rtype: sqlalchemy.orm.scoped_session
#     """
#     Session = scoped_session(sessionmaker(autocommit=False, autoflush=False, bind=engine))
# 
#     # Models
#     from overc.lib.db.models import Base
#     Base.query = Session.query_property()
#     Base.metadata.create_all(bind=engine)  # TODO: remove automatic table creation
# 
#     return Session
# 
# Path: overc/lib/db/models.py
# class Server(Base):
# class Service(Base):
# class state_t(int):
# class ServiceState(Base):
# class Alert(Base):
#     def __str__(self):
#     def __unicode__(self):
#     def update_timed_out(self):
#     def __str__(self):
#     def __unicode__(self):
#     def __new__(cls, state):
#     def is_valid(cls, state):
#     def prev(self):
#     def __unicode__(self):
#     def severity(self):
#     OK = 0
#     WARN = 1
#     FAIL = 2
#     UNK = 3
# 
# Path: overc/lib/alerts.py
# class AlertPlugin(object):
#     def __init__(self, name, cwd, command):
#     def send(self, message):
# def send_alert_with_plugins(alert_plugins, message):


import logging
import os, tempfile
import signal, errno
import fcntl
from time import sleep
from overc.src.init import init_db_engine, init_db_session
from overc.lib.db import models
from overc.lib import alerts
from contextlib import contextmanager


logger = logging.getLogger(__name__)

# TODO: these routines should obtain an exclusive lock so multiple supervise processes does not issue alerts multiple times


def _check_service_states(ssn):
    """ Test all service states, raise alerts if necessary
    :param ssn: Database session
    :type ssn: sqlalchemy.orm.session.Session
    :returns: The number of new alerts reported
    :rtype: int
    """
    # Fetch all states that are not yet checked

# Path: overc/src/init.py
# def init_db_engine(connect):
#     """ Init Sqlalchemy engine
#     :param connect: Database connection string
#     :type connect: str
#     :rtype: sqlalchemy.engine.Engine
#     """
#     return create_engine(connect, convert_unicode=True)
# 
# def init_db_session(engine):
#     """ Init DB session
#     :param engine: Engine
#     :type engine: sqlalchemy.engine.Engine
#     :rtype: sqlalchemy.orm.scoped_session
#     """
#     Session = scoped_session(sessionmaker(autocommit=False, autoflush=False, bind=engine))
# 
#     # Models
#     from overc.lib.db.models import Base
#     Base.query = Session.query_property()
#     Base.metadata.create_all(bind=engine)  # TODO: remove automatic table creation
# 
#     return Session
# 
# Path: overc/lib/db/models.py
# class Server(Base):
# class Service(Base):
# class state_t(int):
# class ServiceState(Base):
# class Alert(Base):
#     def __str__(self):
#     def __unicode__(self):
#     def update_timed_out(self):
#     def __str__(self):
#     def __unicode__(self):
#     def __new__(cls, state):
#     def is_valid(cls, state):
#     def prev(self):
#     def __unicode__(self):
#     def severity(self):
#     OK = 0
#     WARN = 1
#     FAIL = 2
#     UNK = 3
# 
# Path: overc/lib/alerts.py
# class AlertPlugin(object):
#     def __init__(self, name, cwd, command):
#     def send(self, message):
# def send_alert_with_plugins(alert_plugins, message):


# Path: overc/lib/supervise.py
import logging
import os, tempfile
import signal, errno
import fcntl
from time import sleep
from overc.src.init import init_db_engine, init_db_session
from overc.lib.db import models
from overc.lib import alerts
from contextlib import contextmanager



logger = logging.getLogger(__name__)

# TODO: these routines should obtain an exclusive lock so multiple supervise processes does not issue alerts multiple times


def _check_service_states(ssn):
    """ Test all service states, raise alerts if necessary
    :param ssn: Database session
    :type ssn: sqlalchemy.orm.session.Session
    :returns: The number of new alerts reported
    :rtype: int
    """
    # Fetch all states that are not yet checked

    service_states = ssn.query(models.ServiceState)\