Skip to content

Commit

Permalink
Minor Agent State Code Cleanup
Browse files Browse the repository at this point in the history
This patch cleans up the code around the agent state patch a little bit,
avoiding code duplication and unnecessary database requests. It also
adds some tests for the agent state module.

Signed-off-by: Lars Kiesow <lkiesow@uos.de>
  • Loading branch information
lkiesow committed Mar 8, 2017
1 parent 7270be7 commit 8636c6a
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 43 deletions.
38 changes: 14 additions & 24 deletions pyca/db.py
Expand Up @@ -8,6 +8,7 @@

import json
import os.path
import string
from pyca.config import config
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, LargeBinary, create_engine
Expand Down Expand Up @@ -36,7 +37,17 @@ def get_session():
return Session()


class Status():
class Constants():

@classmethod
def str(cls, value):
'''Convert status (id) to its string name.'''
for k, v in cls.__dict__.items():
if k[0] in string.ascii_uppercase and v == value:
return k.lower().replace('_', ' ')


class Status(Constants):
'''Event status definitions
'''
UPCOMING = 1
Expand All @@ -47,44 +58,23 @@ class Status():
FAILED_UPLOADING = 6
FINISHED_UPLOADING = 7

@classmethod
def str(cls, status):
'''Convert status (id) to its string name.'''
for k, v in cls.__dict__.items():
if k[0] in 'FRSU' and v == status:
return k.lower().replace('_', ' ')


class ServiceStatus():
class ServiceStatus(Constants):
'''Service status type definitions
'''
STOPPED = 1
IDLE = 2
BUSY = 3

@classmethod
def str(cls, status):
'''Convert status (id) to its string name.'''
for k, v in cls.__dict__.items():
if k[0] in 'SIB' and v == status:
return k.lower().replace('_', ' ')


class Service():
class Service(Constants):
'''Service type definitions
'''
AGENTSTATE = 1
CAPTURE = 2
INGEST = 3
SCHEDULE = 4

@classmethod
def str(cls, service):
'''Convert service (id) to its string name.'''
for k, v in cls.__dict__.items():
if k[0] in 'ACIS' and v == service:
return k.lower().replace('_', ' ')


# Database Schema Definition
class BaseEvent():
Expand Down
37 changes: 19 additions & 18 deletions pyca/utils.py
Expand Up @@ -200,38 +200,39 @@ def update_event_status(event, status):
dbs.commit()


def set_service_status(type, status):
def set_service_status(service, status):
'''Update the status of a particular service in the database.
'''
dbs = db.get_session()
s = dbs.query(db.ServiceStates).filter(db.ServiceStates.type == type)
s = dbs.query(db.ServiceStates).filter(db.ServiceStates.type == service)
if s.count():
dbs.query(db.ServiceStates).filter(db.ServiceStates.type == type)\
.update({'status': status})
s.update({'status': status})
else:
srv = db.ServiceStates()
srv.type = type
srv.type = service
srv.status = status
dbs.add(srv)
dbs.commit()


def set_service_status_immediate(type, status):
set_service_status(type, status)
def set_service_status_immediate(service, status):
'''Update the status of a particular service in the database and send an
immediate signal to Opencast.
'''
set_service_status(service, status)
update_agent_state()


def get_service_status(type):
def get_service_status(service):
'''Update the status of a particular service in the database.
'''
dbs = db.get_session()
srvs = dbs.query(db.ServiceStates).filter(db.ServiceStates.type == type)
srvs = dbs.query(db.ServiceStates).filter(db.ServiceStates.type == service)

if srvs.count():
srv = srvs[0]
return srv.status
else:
return db.ServiceStatus.STOPPED
return srvs[0].status

return db.ServiceStatus.STOPPED


def update_agent_state():
Expand All @@ -240,12 +241,12 @@ def update_agent_state():
configure_service('capture.admin')
status = 'idle'

'''Determine reported agent state with priority list'''
if get_service_status(db.Service.INGEST) == db.ServiceStatus.BUSY:
status = 'uploading'
if get_service_status(db.Service.CAPTURE) == db.ServiceStatus.BUSY:
status = 'capturing'
# 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)
34 changes: 34 additions & 0 deletions tests/test_agentstate.py
@@ -0,0 +1,34 @@
# -*- coding: utf-8 -*-
'''
pyCA tests for the agents state handling
'''

import os
import os.path
import tempfile
import unittest

from pyca import agentstate, config, db, utils


class TestPycaAgentState(unittest.TestCase):

def setUp(self):
utils.http_request = lambda x, y=False: b'xxx'
_, self.dbfile = tempfile.mkstemp()
config.config()['agent']['database'] = 'sqlite:///' + self.dbfile
config.config()['service-scheduler'] = ['']

# Mock event
db.init()

def tearDown(self):
os.remove(self.dbfile)

def test_run(self):
agentstate.terminate = True
agentstate.run()


if __name__ == '__main__':
unittest.main()
21 changes: 20 additions & 1 deletion tests/test_utils.py
Expand Up @@ -4,9 +4,11 @@
Tests for basic capturing
'''

import os
import tempfile
import unittest

from pyca import utils, config
from pyca import utils, config, db
from tests.tools import should_fail

import sys
Expand All @@ -24,6 +26,14 @@ def setUp(self):
reload(config)
config.config()['service-capture.admin'] = ['']

# db
_, self.dbfile = tempfile.mkstemp()
config.config()['agent']['database'] = 'sqlite:///' + self.dbfile
db.init()

def tearDown(self):
os.remove(self.dbfile)

def test_get_service(self):
res = '''{"services":{
"service":{
Expand Down Expand Up @@ -78,6 +88,15 @@ def test_recording_state(self):
config.config()['agent']['backup_mode'] = True
utils.recording_state('123', 'recording')

def test_set_service_status_immediate(self):
utils.http_request = lambda x, y=False: b''
utils.set_service_status_immediate(db.Service.SCHEDULE,
db.ServiceStatus.IDLE)
utils.set_service_status_immediate(db.Service.INGEST,
db.ServiceStatus.BUSY)
utils.set_service_status_immediate(db.Service.CAPTURE,
db.ServiceStatus.BUSY)


if __name__ == '__main__':
unittest.main()

0 comments on commit 8636c6a

Please sign in to comment.