Esempio n. 1
0
class RATaskStatusChangedListener(RADBBusListener):
    def __init__(self,
                 busname=RA_NOTIFICATION_BUSNAME,
                 subject=RA_NOTIFICATION_PREFIX + 'TaskUpdated',
                 broker=None,
                 propagator=None, ## TODO also give translator?
                 **kwargs):
        """
        RATaskScheduledListener listens on the lofar ?? bus and calls onTaskScheduled or onTaskConclict
        :param busname: valid Qpid address (default: lofar.ra.notification)
        :param broker: valid Qpid broker host (default: None, which means localhost)
        additional parameters in kwargs:
                options=     <dict>    Dictionary of options passed to QPID
                exclusive= <bool>    Create an exclusive binding so no other services can consume duplicate messages (default: False)
                numthreads= <int>    Number of parallel threads processing messages (default: 1)
                verbose=     <bool>    Output extra logging over stdout (default: False)
        """
        super(RATaskStatusChangedListener, self).__init__(busname=busname, subjects=subject, broker=broker, **kwargs)

        self.propagator = propagator
        if not self.propagator:
            self.propagator =  RAtoOTDBPropagator()

    def onTaskUpdated(self, old_task, new_task):
        # override super onTaskUpdated
        # check for status change, and call either onTaskScheduled or onTaskScheduled
        if old_task['status_id'] != new_task['status_id']:
            if new_task['status'] == 'scheduled':
                self.onTaskScheduled(new_task['id'], new_task['otdb_id'], new_task['mom_id'])
            elif new_task['status'] == 'conflict':
                self.onTaskConflict(new_task['id'], new_task['otdb_id'], new_task['mom_id'])

    def onTaskInserted(self, new_task):
        # override super onTaskInserted
        # check for status, and call either onTaskScheduled or onTaskScheduled
        if new_task['status'] == 'scheduled':
            self.onTaskScheduled(new_task['id'], new_task['otdb_id'], new_task['mom_id'])
        elif new_task['status'] == 'conflict':
            self.onTaskConflict(new_task['id'], new_task['otdb_id'], new_task['mom_id'])

    def onTaskScheduled(self, ra_id, otdb_id, mom_id):
        logger.info('onTaskScheduled: ra_id=%s otdb_id=%s mom_id=%s' % (ra_id, otdb_id, mom_id))

        self.propagator.doTaskScheduled(ra_id, otdb_id, mom_id)

    def onTaskConflict(self, ra_id, otdb_id, mom_id):
        logger.info('onTaskConflict: ra_id=%s otdb_id=%s mom_id=%s' % (ra_id, otdb_id, mom_id))

        self.propagator.doTaskConflict(ra_id, otdb_id, mom_id)
Esempio n. 2
0
    def test_doTaskScheduled_calls_CreateParset_with_correct_info(self):

        # test values
        raid = 1
        otdbid = 2
        momid = 3
        rainfo = {'ra': 'info'}
        mominfo = Specification(None, None, None)
        projectname = 'myproject'
        parset = 'par-set'

        # setup mocks
        prop = RAtoOTDBPropagator()
        prop.getRAinfo = MagicMock(return_value=rainfo)
        prop.getMoMinfo = MagicMock(return_value=mominfo)
        prop.momrpc.getObjectDetails = MagicMock(return_value={momid:{'project_name':projectname}})
        prop.translator.CreateParset = MagicMock(return_value=parset)
        prop.setOTDBinfo = MagicMock()

        # trigger test action
        prop.doTaskScheduled(raid, otdbid, momid)

        # assert info was gathered with correct id and createparsec is called with the returned info
        prop.getRAinfo.assert_called_once_with(raid)
        prop.getMoMinfo.assert_called_once_with(momid)
        prop.momrpc.getObjectDetails.assert_called_once_with(momid)
        prop.translator.CreateParset.assert_called_once_with(otdbid, rainfo, projectname, mominfo)
        prop.setOTDBinfo.assert_called_once_with(otdbid, parset, 'scheduled')
Esempio n. 3
0
    def test_getMoMinfo_returns_storagemanager_from_MoM(self):

        # test values
        momid = 3
        storagemanager = "d.y.s.c.o"

        # setup mocks
        prop = RAtoOTDBPropagator()
        prop.momrpc = MagicMock()
        prop.momrpc.get_storagemanager.return_value = storagemanager

        # trigger test action
        mominfo = prop.getMoMinfo(momid)

        # assert momrpc is called with correct id
        prop.momrpc.get_storagemanager.assert_called_once_with(momid)
        # assert returned value by mom is part of spec (yes, that should strictly not be tested here)
        self.assertEqual(mominfo.storagemanager, storagemanager)
Esempio n. 4
0
    def __init__(self,
                 busname=RA_NOTIFICATION_BUSNAME,
                 subject=RA_NOTIFICATION_PREFIX + 'TaskUpdated',
                 broker=None,
                 propagator=None, ## TODO also give translator?
                 **kwargs):
        """
        RATaskScheduledListener listens on the lofar ?? bus and calls onTaskScheduled or onTaskConclict
        :param busname: valid Qpid address (default: lofar.ra.notification)
        :param broker: valid Qpid broker host (default: None, which means localhost)
        additional parameters in kwargs:
                options=     <dict>    Dictionary of options passed to QPID
                exclusive= <bool>    Create an exclusive binding so no other services can consume duplicate messages (default: False)
                numthreads= <int>    Number of parallel threads processing messages (default: 1)
                verbose=     <bool>    Output extra logging over stdout (default: False)
        """
        super(RATaskStatusChangedListener, self).__init__(busname=busname, subjects=subject, broker=broker, **kwargs)

        self.propagator = propagator
        if not self.propagator:
            self.propagator =  RAtoOTDBPropagator()
Esempio n. 5
0
class RATaskStatusChangedHandler(RAEventMessageHandler):
    def __init__(self, propagator=None):
        super().__init__()

        self.propagator = propagator
        if not self.propagator:
            self.propagator = RAtoOTDBPropagator()

    def onTaskApproved(self, task_ids):
        radb_id = task_ids.get('radb_id')
        otdb_id = task_ids.get('otdb_id')
        mom_id = task_ids.get('mom_id')
        logger.info('onTaskApproved: radb_id=%s otdb_id=%s mom_id=%s', radb_id,
                    otdb_id, mom_id)

        self.propagator.doTaskApproved(otdb_id, mom_id)

    def onTaskScheduled(self, task_ids):
        radb_id = task_ids.get('radb_id')
        otdb_id = task_ids.get('otdb_id')
        mom_id = task_ids.get('mom_id')
        logger.info('onTaskScheduled: radb_id=%s otdb_id=%s mom_id=%s',
                    radb_id, otdb_id, mom_id)

        self.propagator.doTaskScheduled(radb_id, otdb_id, mom_id)

    def onTaskConflict(self, task_ids):
        radb_id = task_ids.get('radb_id')
        otdb_id = task_ids.get(
            'otdb_id')  #Does this work if one of the Id's is not set?
        mom_id = task_ids.get('mom_id')
        logger.info('onTaskConflict: radb_id=%s otdb_id=%s mom_id=%s', radb_id,
                    otdb_id, mom_id)

        self.propagator.doTaskConflict(otdb_id)

    def onTaskError(self, task_ids):
        radb_id = task_ids.get('radb_id')
        otdb_id = task_ids.get(
            'otdb_id')  #Does this work if one of the Id's is not set?
        mom_id = task_ids.get('mom_id')
        logger.info('onTaskError: radb_id=%s otdb_id=%s mom_id=%s', radb_id,
                    otdb_id, mom_id)

        self.propagator.doTaskError(otdb_id)
Esempio n. 6
0
def main():
    # make sure we run in UTC timezone
    import os
    os.environ['TZ'] = 'UTC'

    from optparse import OptionParser
    from lofar.common.util import waitForInterrupt

    # Check the invocation arguments
    parser = OptionParser(
        "%prog [options]",
        description='runs the RAtoOTDBTaskSpecificationPropagator service')
    parser.add_option('-b',
                      '--broker',
                      dest='broker',
                      type='string',
                      default=DEFAULT_BROKER,
                      help='Address of the qpid broker, default: %default')
    parser.add_option(
        '-e',
        "--exchange",
        dest="exchange",
        type="string",
        default=DEFAULT_BUSNAME,
        help=
        "Name of the bus on which messages are published, default: %default")
    parser.add_option('-V',
                      '--verbose',
                      dest='verbose',
                      action='store_true',
                      help='verbose logging')
    (options, args) = parser.parse_args()

    logging.getLogger('lofar.sas.resourceassignment.database.radbbuslistener'
                      ).level = logging.WARN
    logging.basicConfig(
        format='%(asctime)s %(levelname)s %(message)s',
        level=logging.DEBUG if options.verbose else logging.INFO)

    with RAtoOTDBPropagator(exchange=options.exchange,
                            broker=options.broker) as propagator:
        with RABusListener(handler_type=RATaskStatusChangedHandler,
                           handler_kwargs={'propagator': propagator},
                           exchange=options.exchange,
                           broker=options.broker):
            waitForInterrupt()
Esempio n. 7
0
def main():
    from optparse import OptionParser
    from lofar.messaging import setQpidLogLevel
    from lofar.common.util import waitForInterrupt

    from lofar.sas.resourceassignment.resourceassignmentservice.config import DEFAULT_BUSNAME as RADB_BUSNAME
    from lofar.sas.resourceassignment.resourceassignmentservice.config import DEFAULT_SERVICENAME as RADB_SERVICENAME
    from lofar.sas.otdb.config import DEFAULT_OTDB_SERVICE_BUSNAME, DEFAULT_OTDB_SERVICENAME

    from lofar.mom.momqueryservice.config import DEFAULT_MOMQUERY_BUSNAME, DEFAULT_MOMQUERY_SERVICENAME


    # Check the invocation arguments
    parser = OptionParser("%prog [options]",
                          description='runs the RAtoOTDBTaskSpecificationPropagator service')
    parser.add_option('-q', '--broker', dest='broker', type='string', default=None, help='Address of the qpid broker, default: localhost')
    parser.add_option("--notification_busname", dest="notification_busname", type="string", default=RA_NOTIFICATION_BUSNAME, help="Name of the notification bus on which messages are published, default: %default")
    parser.add_option("--notification_subject", dest="notification_subject", type="string", default=RA_NOTIFICATION_PREFIX+'TaskUpdated', help="Subject of the published messages to listen for, default: %default")
    parser.add_option("--radb_busname", dest="radb_busname", type="string", default=RADB_BUSNAME, help="Name of the bus on which the RADB service listens, default: %default")
    parser.add_option("--radb_servicename", dest="radb_servicename", type="string", default=RADB_SERVICENAME, help="Name of the RADB service, default: %default")
    parser.add_option("--otdb_busname", dest="otdb_busname", type="string", default=DEFAULT_OTDB_SERVICE_BUSNAME, help="Name of the bus on which the OTDB service listens, default: %default")
    parser.add_option("--otdb_servicename", dest="otdb_servicename", type="string", default=DEFAULT_OTDB_SERVICENAME, help="Name of the OTDB service, default: %default")
    parser.add_option("--mom_busname", dest="mom_busname", type="string", default=DEFAULT_MOMQUERY_BUSNAME, help="Name of the bus on which the MoM service listens, default: %default")
    parser.add_option("--mom_servicename", dest="mom_servicename", type="string", default=DEFAULT_MOMQUERY_SERVICENAME, help="Name of the MoM service, default: %default")
    parser.add_option('-V', '--verbose', dest='verbose', action='store_true', help='verbose logging')
    (options, args) = parser.parse_args()

    logging.getLogger('lofar.sas.resourceassignment.database.radbbuslistener').level = logging.WARN
    setQpidLogLevel(logging.INFO)
    logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s',
                        level=logging.DEBUG if options.verbose else logging.INFO)

    with RAtoOTDBPropagator(radb_busname=options.radb_busname,
                            radb_servicename=options.radb_servicename,
                            otdb_busname=options.otdb_busname,
                            otdb_servicename=options.otdb_servicename,
                            mom_busname=options.mom_busname,
                            mom_servicename=options.mom_servicename,
                            broker=options.broker) as propagator:
        with RATaskStatusChangedListener(busname=options.notification_busname,
                                         subject=options.notification_subject,
                                         broker=options.broker,
                                         propagator=propagator) as listener:
            waitForInterrupt()
Esempio n. 8
0
    def __init__(self, propagator=None):
        super().__init__()

        self.propagator = propagator
        if not self.propagator:
            self.propagator = RAtoOTDBPropagator()