Exemplo n.º 1
0
 def redirect_or_pass_by_id(self, host_id):
     """
     Check if given host id is own host, if not then raise exception
     to redirect message to proper destination.
     """
     if host_id is None:
         raise exceptions.ServiceBusException("Unknown address")
     ownip = host_id == self.DAEMON.ID
     if not ownip:
         raise RedirectRequiredEx(host_id)
Exemplo n.º 2
0
    def handle_sync_call(self, msgdata):
        """
        This is main function to process all requests.
        """
        name = msgdata['service']
        # not our task
        if name != self.servicename:
            raise exceptions.ServiceBusException(
                "Wrong service task received %s" % str(service))

        # not in working state
        if self.status != 2:
            raise exceptions.ServiceBusException("Worker is currently offline")

        #msgdata = self.prepare_message(msgdata)
        result = self.run_task(funcname=msgdata['method'],
                               args=msgdata['args'],
                               kwargs=msgdata['kwargs'])
        #result = self.postprocess_message(result)
        return result
Exemplo n.º 3
0
 def _find_worker(self, service_name, method=None):
     """
     Ask kasaya daemon where is service
     """
     kasaya = KasayaLocalClient()
     msg = kasaya.query( service_name )
     if not msg['message']==messages.WORKER_ADDR:
         raise exceptions.ServiceBusException("Wrong response from sync server")
     addr = msg['addr']
     if addr is None:
         raise exceptions.ServiceNotFound("No service '%s' found" % service_name)
     return addr
Exemplo n.º 4
0
def send_and_receive_response(address, message, timeout=10):
    """
    j.w. ale dekoduje wynik i go zwraca, lub rzuca otrzymany w wiadomości exception
    """
    result = send_and_receive(address, message, timeout)
    typ = result['message']
    if typ == messages.RESULT:
        return result['result']

    elif typ == messages.ERROR:
        e = exception_deserialize(result)
        if e is None:
            raise exceptions.MessageCorrupted()
        raise e
    else:
        raise exceptions.ServiceBusException("Wrong message type received")
Exemplo n.º 5
0
    def decode_header(self, packet):
        if len(packet) < self.header.size:
            raise exceptions.MessageCorrupted()

        busname, ver, psize, iv, cmpr, trim, resreq = self.header.unpack(
            packet)

        # not our service bus
        if busname != self._busname:
            raise exceptions.NotOurMessage()

        # check protocol version
        if ver != self._version:
            raise exceptions.ServiceBusException(
                "Unknown service bus protocol version")

        return (psize, iv, cmpr, trim, resreq)
Exemplo n.º 6
0
    def CTL_service_stop(self, name, ip=None):
        """
        Stop all workers serving given service.
        name - name of service to stop
        """
        if ip is None:
            ip = self.own_ip
        self.redirect_or_pass_by_id(ip)

        services = []
        for wrk in self.DB.worker_list_local():
            if wrk['service'] == name:
                services.append(wrk['ID'])
        if len(services) == 0:
            raise exceptions.ServiceBusException(
                "There is no [%s] service running" % name)
        for u in services:
            self.CTL_worker_stop(u)
Exemplo n.º 7
0
    def CTL_service_start(self, name, ip=None):
        """
        Start service on host, or locally if host is not given.
        name - name of service to start
        ip - ip address of host on which service shoult be started,
             if not given then worker will be started on localhost.
        """
        if ip is None:
            ip = self.own_ip
        self.redirect_or_pass_by_id(ip)

        try:
            svc = self.get_service_ctl(name)
        except KeyError:
            raise exceptions.ServiceBusException(
                "There is no service [%s] on this host" % name)

        svc.start_service()
        return True
Exemplo n.º 8
0
def exception_deserialize(msg):
    """
    Deserialize exception from message into exception object which can be raised.
    #If message doesn't contains exception, then result will be None.
    """
    if msg['internal']:
        e = exceptions.ServiceBusException(msg['description'])
    else:
        e = Exception(msg['description'])
    try:
        e.name = msg['name']
    except KeyError:
        e.name = "Exception"
    try:
        tb = msg['traceback']
    except KeyError:
        tb = None
    if not tb is None:
        e.traceback = tb
    return e