Esempio n. 1
0
def handleCmd(srvObj, reqPropsObj, httpRef):
    """
    Handle SUBSCRIBE Command.

    srvObj:         Reference to NG/AMS server class object (ngamsServer).

    reqPropsObj:    Request Property object to keep track of actions done
                    during the request handling (ngamsReqProps).

    httpRef:        Reference to the HTTP request handler
                    object (ngamsHttpRequestHandler).

    Returns:        Void.
    """
    T = TRACE()
    """
    if (srvObj.getDataMoverOnlyActive() and len(srvObj.getSubscriberDic()) > 0):
        srvObj.reply(reqPropsObj, httpRef, NGAMS_HTTP_SUCCESS, NGAMS_FAILURE,
                 "Data Mover NGAS server can have only one subscriber. Use command USUBSCRIBE to update an existing subscriber.")
        return
    """
    priority = 10
    url = ""
    startDate = time.time()
    filterPi = ""
    filterPiPars = ""
    if (reqPropsObj.hasHttpPar("priority")):
        priority = reqPropsObj.getHttpPar("priority")
    if (reqPropsObj.hasHttpPar("url")):
        url = reqPropsObj.getHttpPar("url")
        ngamsSubscriber.validate_url(url)
    else:
        errMsg = genLog("NGAMS_ER_CMD_SYNTAX",
                        [NGAMS_SUBSCRIBE_CMD, "Missing parameter: url"])
        raise Exception, errMsg
    if (reqPropsObj.hasHttpPar("start_date")):
        tmpStartDate = reqPropsObj.getHttpPar("start_date").strip()
        if tmpStartDate:
            startDate = fromiso8601(tmpStartDate, local=True)
    if (reqPropsObj.hasHttpPar("filter_plug_in")):
        filterPi = reqPropsObj.getHttpPar("filter_plug_in")
    if (reqPropsObj.hasHttpPar("plug_in_pars")):
        filterPiPars = reqPropsObj.getHttpPar("plug_in_pars")
    if (reqPropsObj.hasHttpPar("subscr_id")):
        id = reqPropsObj.getHttpPar("subscr_id")
    else:
        id = ngamsLib.getSubscriberId(url)

    logger.info("Creating subscription for files >= %s", toiso8601(startDate))
    subscrObj = ngamsSubscriber.ngamsSubscriber(srvObj.getHostId(),
                                                srvObj.getCfg().getPortNo(),
                                                priority,
                                                url,
                                                startDate,
                                                filterPi,
                                                filterPiPars,
                                                subscrId=id)
    # supports concurrent file transfer, added by [email protected]
    if (reqPropsObj.hasHttpPar("concurrent_threads")):
        concurthrds = reqPropsObj.getHttpPar("concurrent_threads")
        subscrObj.setConcurrentThreads(concurthrds)

    # If the Start Date given in before the Last Ingestion Date, we
    # reset the Last Ingestion Date
    subscrStat = srvObj.getDb().getSubscriberStatus([subscrObj.getId()],
                                                    subscrObj.getHostId(),
                                                    subscrObj.getPortNo())
    if subscrStat:
        lastIngDate = subscrStat[0][1]
        if startDate < lastIngDate:
            subscrObj.setLastFileIngDate(None)
        else:
            subscrObj.setLastFileIngDate(lastIngDate)

    # Register the Subscriber.
    addSubscriber(srvObj, subscrObj)

    # Trigger the Data Susbcription Thread to make it check if there are
    # files to deliver to the new Subscriber.
    srvObj.addSubscriptionInfo([], [subscrObj]).triggerSubscriptionThread()

    return "Handled SUBSCRIBE command"
Esempio n. 2
0
def handleCmd(srvObj,
              reqPropsObj,
              httpRef):
    """
    Handle the update subscriber (USUBSCRIBE) Command.

    srvObj:         Reference to NG/AMS server class object (ngamsServer).

    reqPropsObj:    Request Property object to keep track of actions done
                    during the request handling (ngamsReqProps).

    httpRef:        Reference to the HTTP request handler
                    object (ngamsHttpRequestHandler).

    Returns:        Void.
    """
    T = TRACE()
    errMsg = ''
    err = 0
    if (not reqPropsObj.hasHttpPar("subscr_id")):
        httpRef.send_status("USUBSCRIBE command failed: 'subscr_id' is not specified",
                            status=NGAMS_FAILURE, code=NGAMS_HTTP_SUCCESS)
        return

    subscrId = reqPropsObj.getHttpPar("subscr_id")
    if (not srvObj.getSubscriberDic().has_key(subscrId)):
        httpRef.send_status("USUBSCRIBE command failed: Cannot find subscriber '%s'" % subscrId,
                            status=NGAMS_FAILURE, code=NGAMS_HTTP_SUCCESS)
        return

    if (reqPropsObj.hasHttpPar("suspend")):
        suspend = int(reqPropsObj.getHttpPar("suspend"))
        suspend_processed = 0
        # could use locks, but the race condition should not really matter here if only one request of suspend is issued at a time (by a system admin?)
        if (suspend == 1 and srvObj._subscrSuspendDic[subscrId].is_set()): # suspend condition met
            srvObj._subscrSuspendDic[subscrId].clear()
            suspend_processed = 1
            action = 'SUSPENDED'
        elif (suspend == 0 and (not srvObj._subscrSuspendDic[subscrId].is_set())): # resume condition met
            srvObj._subscrSuspendDic[subscrId].set()
            suspend_processed = 1
            action = 'RESUMED'
        if (suspend_processed):
            httpRef.send_status("Successfully %s for the subscriber %s" % (action, subscrId))
        else:
            reMsg = "No suspend/resume action is taken for the subscriber %s" % subscrId
            httpRef.send_status(reMsg, status=NGAMS_FAILURE, code=NGAMS_HTTP_SUCCESS)
        return

    subscriber = srvObj.getSubscriberDic()[subscrId]

    if (reqPropsObj.hasHttpPar("priority")):
        priority = int(reqPropsObj.getHttpPar("priority"))
        subscriber.setPriority(priority)

    if (reqPropsObj.hasHttpPar("url")):
        url = reqPropsObj.getHttpPar("url")
        ngamsSubscriber.validate_url(url)
        subscriber.setUrl(url)

    if (reqPropsObj.hasHttpPar("start_date")):
        startDate = reqPropsObj.getHttpPar("start_date").strip()
        if (startDate):
            subscriber.setStartDate(fromiso8601(startDate))
            lastIngDate = subscriber.getLastFileIngDate()
            if lastIngDate is not None: # either re-check past files or skip unchecked files
                subscriber.setLastFileIngDate(None)
            if (srvObj._subscrScheduledStatus.has_key(subscrId)):
                #if (startDate < srvObj._subscrScheduledStatus[subscrId] and srvObj._subscrScheduledStatus[subscrId]): # enables trigger re-delivering files that have been previously delivered
                del srvObj._subscrScheduledStatus[subscrId]
            if (srvObj._subscrCheckedStatus.has_key(subscrId)):
                del srvObj._subscrCheckedStatus[subscrId]
                #if (srvObj._subscrScheduledStatus[subscrId]):# either re-check past files or skip unchecked files
                    #del srvObj._subscrScheduledStatus[subscrId]
                    #srvObj._subscrScheduledStatus[subscrId] = None

    if (reqPropsObj.hasHttpPar("filter_plug_in")):
        filterPi = reqPropsObj.getHttpPar("filter_plug_in")
        subscriber.setFilterPi(filterPi)

    if (reqPropsObj.hasHttpPar("plug_in_pars")):
        pipars = reqPropsObj.getHttpPar("plug_in_pars")
        subscriber.setFilterPiPars(pipars)

    if (reqPropsObj.hasHttpPar("concurrent_threads")):
        ccthrds = int(reqPropsObj.getHttpPar("concurrent_threads"))
        origthrds = int(subscriber.getConcurrentThreads())
        if (ccthrds != origthrds):
            subscriber.setConcurrentThreads(ccthrds)
            try:
                changeNumThreads(srvObj, subscrId, origthrds, ccthrds)
            except Exception, e:
                msg = " Exception updating subscriber's concurrent threads: %s." % str(e)
                logger.warning(msg)
                err += 1
                errMsg += msg
Esempio n. 3
0
def handleCmd(srvObj, reqPropsObj, httpRef):
    """
    Handle SUBSCRIBE Command.

    srvObj:         Reference to NG/AMS server class object (ngamsServer).

    reqPropsObj:    Request Property object to keep track of actions done
                    during the request handling (ngamsReqProps).

    httpRef:        Reference to the HTTP request handler
                    object (ngamsHttpRequestHandler).

    Returns:        Void.
    """
    """
    if (srvObj.getDataMoverOnlyActive() and len(srvObj.getSubscriberDic()) > 0):
        srvObj.reply(reqPropsObj, httpRef, NGAMS_HTTP_SUCCESS, NGAMS_FAILURE,
                 "Data Mover NGAS server can have only one subscriber. Use command USUBSCRIBE to update an existing subscriber.")
        return
    """
    priority = 10
    url = ""
    startDate = time.time()
    filterPi = ""
    filterPiPars = ""
    if (reqPropsObj.hasHttpPar("priority")):
        priority = reqPropsObj.getHttpPar("priority")
    if (reqPropsObj.hasHttpPar("url")):
        url = reqPropsObj.getHttpPar("url")
        ngamsSubscriber.validate_url(url)
    else:
        errMsg = genLog("NGAMS_ER_CMD_SYNTAX",
                        [NGAMS_SUBSCRIBE_CMD, "Missing parameter: url"])
        raise Exception(errMsg)
    if (reqPropsObj.hasHttpPar("start_date")):
        tmpStartDate = reqPropsObj.getHttpPar("start_date").strip()
        if tmpStartDate:
            startDate = fromiso8601(tmpStartDate, local=True)
    if (reqPropsObj.hasHttpPar("filter_plug_in")):
        filterPi = reqPropsObj.getHttpPar("filter_plug_in")
    if (reqPropsObj.hasHttpPar("plug_in_pars")):
        filterPiPars = reqPropsObj.getHttpPar("plug_in_pars")
    if (reqPropsObj.hasHttpPar("subscr_id")):
        id = reqPropsObj.getHttpPar("subscr_id")
    else:
        id = ngamsLib.getSubscriberId(url)

    logger.info("Creating subscription for files >= %s", toiso8601(startDate))
    subscrObj = ngamsSubscriber.ngamsSubscriber(srvObj.getHostId(),
                                                srvObj.getCfg().getPortNo(),
                                                priority,
                                                url,
                                                startDate,
                                                filterPi,
                                                filterPiPars,
                                                subscrId=id)
    # supports concurrent file transfer, added by [email protected]
    if (reqPropsObj.hasHttpPar("concurrent_threads")):
        concurthrds = reqPropsObj.getHttpPar("concurrent_threads")
        subscrObj.setConcurrentThreads(concurthrds)

    # If the Start Date given in before the Last Ingestion Date, we
    # reset the Last Ingestion Date
    #
    # TODO (rtobar, May 2021):
    #     This bit of logic seems to be in contention with the fact that adding
    #     a subscription with an ID that is already taken would be otherwise a
    #     failure. I'm leaving it here for the time being in order to avoid
    #     breaking anything, but it must probably be removed (or clarification
    #     should be sought otherwise).
    subscrStat = srvObj.getDb().getSubscriberStatus([subscrObj.getId()],
                                                    subscrObj.getHostId(),
                                                    subscrObj.getPortNo())
    if subscrStat and subscrStat[0][1]:
        lastIngDate = fromiso8601(subscrStat[0][1], local=True)
        if startDate < lastIngDate:
            subscrObj.setLastFileIngDate('')
        else:
            subscrObj.setLastFileIngDate(lastIngDate)

    # Register the Subscriber.
    existence_test = addSubscriber(srvObj, subscrObj)
    if existence_test == 'equal':
        return 201, NGAMS_SUCCESS, "Identical subscription with ID '%s' existed" % (
            id, )
    elif existence_test == 'unequal':
        return 409, NGAMS_FAILURE, "Different subscription with ID '%s' existed" % (
            id, )

    return "Handled SUBSCRIBE command"