# Path: pyca/utils.py
# @db.with_session
# def set_service_status(dbs, service, status):
#     '''Update the status of a particular service in the database.
#     '''
#     srv = db.ServiceStates()
#     srv.type = service
#     srv.status = status
# 
#     dbs.merge(srv)
#     dbs.commit()
# 
# def update_agent_state():
#     '''Update the current agent state in opencast.
#     '''
#     status = 'idle'
# 
#     # Determine reported agent state with priority list
#     if get_service_status(db.Service.SCHEDULE) == db.ServiceStatus.STOPPED:
#         status = 'offline'
#     elif get_service_status(db.Service.CAPTURE) == db.ServiceStatus.BUSY:
#         status = 'capturing'
#     elif get_service_status(db.Service.INGEST) == db.ServiceStatus.BUSY:
#         status = 'uploading'
# 
#     register_ca(status=status)
# 
# def timestamp():
#     '''Get current unix timestamp
#     '''
#     return int(datetime.now(tzutc()).timestamp())
# 
# Path: pyca/utils.py
# def terminate(shutdown=None):
#     '''Mark process as to be terminated.
#     '''
#     global _terminate
#     if shutdown is not None:
#         _terminate = shutdown
#     return '_terminate' in globals() and _terminate
# 
# Path: pyca/config.py
# def config(*args):
#     '''Get a specific configuration value or the whole configuration, loading
#     the configuration file if it was not before.
# 
#     :param key: optional configuration key to return
#     :type key: string
#     :return: Part of the configuration object containing the configuration
#              or configuration value.
#              If a part of the configuration object (e.g. configobj.Section) is
#              returned, it can be treated like a dictionary.
#              Returning None, if the configuration value is not found.
#     '''
#     cfg = __config or update_configuration()
#     for key in args:
#         if cfg is None:
#             return
#         cfg = cfg.get(key)
#     return cfg
# 
# Path: pyca/db.py
# class Service(Constants):
#     '''Service type definitions
#     '''
#     AGENTSTATE = 1
#     CAPTURE = 2
#     INGEST = 3
#     SCHEDULE = 4
# 
# class ServiceStatus(Constants):
#     '''Service status type definitions
#     '''
#     STOPPED = 1
#     IDLE = 2
#     BUSY = 3


from pyca.utils import set_service_status, update_agent_state, timestamp
from pyca.utils import terminate
from pyca.config import config
from pyca.db import Service, ServiceStatus
import logging
import sdnotify
import time
# -*- coding: utf-8 -*-
'''
    python-capture-agent
    ~~~~~~~~~~~~~~~~~~~~

    :copyright: 2014-2017, Lars Kiesow <lkiesow@uos.de>
    :license: LGPL – see license.lgpl for more details.
'''


logger = logging.getLogger(__name__)
notify = sdnotify.SystemdNotifier()


def control_loop():
    '''Main loop, updating the capture agent state.
    '''
    set_service_status(Service.AGENTSTATE, ServiceStatus.BUSY)
    notify.notify('READY=1')
    notify.notify('STATUS=Running')
    while not terminate():
        notify.notify('WATCHDOG=1')

        next_update = timestamp() + config('agent', 'update_frequency')

# Path: pyca/utils.py
# @db.with_session
# def set_service_status(dbs, service, status):
#     '''Update the status of a particular service in the database.
#     '''
#     srv = db.ServiceStates()
#     srv.type = service
#     srv.status = status
# 
#     dbs.merge(srv)
#     dbs.commit()
# 
# def update_agent_state():
#     '''Update the current agent state in opencast.
#     '''
#     status = 'idle'
# 
#     # Determine reported agent state with priority list
#     if get_service_status(db.Service.SCHEDULE) == db.ServiceStatus.STOPPED:
#         status = 'offline'
#     elif get_service_status(db.Service.CAPTURE) == db.ServiceStatus.BUSY:
#         status = 'capturing'
#     elif get_service_status(db.Service.INGEST) == db.ServiceStatus.BUSY:
#         status = 'uploading'
# 
#     register_ca(status=status)
# 
# def timestamp():
#     '''Get current unix timestamp
#     '''
#     return int(datetime.now(tzutc()).timestamp())
# 
# Path: pyca/utils.py
# def terminate(shutdown=None):
#     '''Mark process as to be terminated.
#     '''
#     global _terminate
#     if shutdown is not None:
#         _terminate = shutdown
#     return '_terminate' in globals() and _terminate
# 
# Path: pyca/config.py
# def config(*args):
#     '''Get a specific configuration value or the whole configuration, loading
#     the configuration file if it was not before.
# 
#     :param key: optional configuration key to return
#     :type key: string
#     :return: Part of the configuration object containing the configuration
#              or configuration value.
#              If a part of the configuration object (e.g. configobj.Section) is
#              returned, it can be treated like a dictionary.
#              Returning None, if the configuration value is not found.
#     '''
#     cfg = __config or update_configuration()
#     for key in args:
#         if cfg is None:
#             return
#         cfg = cfg.get(key)
#     return cfg
# 
# Path: pyca/db.py
# class Service(Constants):
#     '''Service type definitions
#     '''
#     AGENTSTATE = 1
#     CAPTURE = 2
#     INGEST = 3
#     SCHEDULE = 4
# 
# class ServiceStatus(Constants):
#     '''Service status type definitions
#     '''
#     STOPPED = 1
#     IDLE = 2
#     BUSY = 3


# Path: pyca/agentstate.py
from pyca.utils import set_service_status, update_agent_state, timestamp
from pyca.utils import terminate
from pyca.config import config
from pyca.db import Service, ServiceStatus
import logging
import sdnotify
import time

# -*- coding: utf-8 -*-
'''
    python-capture-agent
    ~~~~~~~~~~~~~~~~~~~~

    :copyright: 2014-2017, Lars Kiesow <lkiesow@uos.de>
    :license: LGPL – see license.lgpl for more details.
'''


logger = logging.getLogger(__name__)
notify = sdnotify.SystemdNotifier()


def control_loop():
    '''Main loop, updating the capture agent state.
    '''
    set_service_status(Service.AGENTSTATE, ServiceStatus.BUSY)
    notify.notify('READY=1')
    notify.notify('STATUS=Running')
    while not terminate():
        notify.notify('WATCHDOG=1')

        next_update = timestamp() + config('agent', 'update_frequency')

        while not terminate() and timestamp() < next_update: