~verterok/ubuntuone-client/fix-646112

« back to all changes in this revision

Viewing changes to ubuntuone/syncdaemon/action_queue.py

  • Committer: Tarmac
  • Author(s): facundo at com
  • Date: 2010-09-21 20:22:13 UTC
  • mfrom: (713.1.1 aq-remove-def)
  • Revision ID: tarmac-20100921202213-20rtkzwutx0vy027
Removed AQ.deferred that was only used for testing.

Show diffs side-by-side

added added

removed removed

Lines of Context:
74
74
 
75
75
    @wraps(func)
76
76
    def wrapper(a):
77
 
        """
78
 
        Do it.
79
 
        """
 
77
        """Do it."""
80
78
        func(a)
81
79
        return a
82
80
 
112
110
 
113
111
 
114
112
class LoggingStorageClient(ThrottlingStorageClient):
115
 
    """ A subclass of StorageClient that adds logging to
116
 
    processMessage and sendMessage.
 
113
    """A subclass of StorageClient that logs.
 
114
 
 
115
    Specifically, it adds logging to processMessage and sendMessage.
117
116
    """
118
117
 
119
118
    def __init__(self):
120
 
        """ setup logging and create the instance. """
121
119
        ThrottlingStorageClient.__init__(self)
122
120
        self.log = logging.getLogger('ubuntuone.SyncDaemon.StorageClient')
123
121
        # configure the handler level to be < than DEBUG
125
123
        self.log.debug = partial(self.log.log, TRACE)
126
124
 
127
125
    def processMessage(self, message):
128
 
        """ wrapper that logs the message and result. """
 
126
        """Wrapper that logs the message and result."""
129
127
        # don't log the full message if it's of type BYTES
130
128
        if message.type == protocol_pb2.Message.BYTES:
131
129
            self.log.debug('start - processMessage: id: %s, type: %s',
142
140
        return result
143
141
 
144
142
    def log_error(self, failure):
145
 
        """ logging errback for requests """
 
143
        """Logging errback for requests."""
146
144
        self.log.debug('request error: %s', failure)
147
145
        return failure
148
146
 
149
147
    def log_success(self, result):
150
 
        """ logging callback for requests """
 
148
        """Logging callback for requests."""
151
149
        self.log.debug('request finished: %s', result)
152
150
        if getattr(result, '__dict__', None):
153
151
            self.log.debug('result.__dict__: %s', result.__dict__)
154
152
        return result
155
153
 
156
154
    def sendMessage(self, message):
157
 
        """ wrapper that logs the message and result. """
 
155
        """Wrapper that logs the message and result."""
158
156
        # don't log the full message if it's of type BYTES
159
157
        if message.type == protocol_pb2.Message.BYTES:
160
158
            self.log.debug('start - sendMessage: id: %s, type: %s',
615
613
        self.consumer = None
616
614
 
617
615
        self.client = None # an instance of self.protocol
618
 
        self.deferred = None # only used for testing, :(
619
616
 
620
617
        # is a twisted.internet.tcp/ssl.Connector instance
621
618
        self.connector = None # created on reactor.connectTCP/SSL
761
758
 
762
759
    def connect(self):
763
760
        """Start the circus going."""
764
 
        self.deferred = defer.Deferred() # only used for testing, :(
765
761
        # avoid multiple connections
766
762
        if self.connect_in_progress:
767
763
            msg = "Discarding new connection attempt, there is a connector!"
820
816
        self._cleanup_connection_state()
821
817
        self.event_queue.push('SYS_CONNECTION_FAILED')
822
818
        logger.info('Connection failed: %s', reason.getErrorMessage())
823
 
        self.deferred.errback(reason)
824
819
 
825
820
    def clientConnectionLost(self, connector, reason):
826
821
        """The client connection went down."""
831
826
    @defer.inlineCallbacks
832
827
    def _send_request_and_handle_errors(self, request, request_error,
833
828
                                        event_error, event_ok,
834
 
                                        fire_deferred=True,
835
829
                                        handle_exception=True,
836
830
                                        args=(), kwargs={}):
837
831
        """Send 'request' to the server, using params 'args' and 'kwargs'.
855
849
            finally:
856
850
                # common handling for all cases
857
851
                if client is not self.client:
858
 
                    client_mismatch_msg = "Client mismatch while processing " \
859
 
                                          "the request '%s', " \
860
 
                                          "client (%r) is not self.client (%r)."
861
 
                    logger.warning(client_mismatch_msg, req_name,
862
 
                                   client, self.client)
 
852
                    msg = "Client mismatch while processing the request '%s'" \
 
853
                          ", client (%r) is not self.client (%r)."
 
854
                    logger.warning(msg, req_name, client, self.client)
863
855
                    return
864
856
        except request_error, failure:
865
857
            event = event_error
888
880
            logger.info("The request '%s' failed with the error: %s "
889
881
                        "and was handled with the event: %s",
890
882
                         req_name, failure, event)
891
 
            if fire_deferred:
892
 
                # it looks like we won't be authenticating, so hook up the
893
 
                # for-testing deferred now
894
 
                self.deferred.callback(Failure(failure))
895
883
        else:
896
884
            defer.returnValue(result)
897
885
 
911
899
 
912
900
        @defer.inlineCallbacks
913
901
        def caps_raising_if_not_accepted(capability_method, caps, msg):
 
902
            """Discuss capabilities with the server."""
914
903
            client_caps = getattr(self.client, capability_method)
915
904
            req = yield client_caps(caps)
916
905
            if not req.accepted:
917
906
                raise StandardError(msg)
 
907
            defer.returnValue(req)
918
908
 
919
909
        error_msg = "The server doesn't have the requested capabilities"
920
910
        query_caps_d = self._send_request_and_handle_errors(
924
914
            event_ok=None,
925
915
            args=('query_caps', caps, error_msg)
926
916
        )
927
 
        yield query_caps_d
 
917
        req = yield query_caps_d
928
918
 
929
 
        if self.deferred.called:
930
 
            # the query_caps call finished with failure
 
919
        # req can be None if set capabilities failed, error is handled by
 
920
        # _send_request_and_handle_errors
 
921
        if not req:
931
922
            return
932
923
 
933
924
        error_msg = "The server denied setting '%s' capabilities" % caps
958
949
            # log the session_id
959
950
            logger.note('Session ID: %r', str(req.session_id))
960
951
 
961
 
        if not self.deferred.called:
962
 
            # callback the deferred if everything went ok
963
 
            self.deferred.callback(self.client)
964
 
 
965
952
    @defer.inlineCallbacks
966
953
    def query_volumes(self):
967
954
        """Get the list of volumes.
972
959
        result = yield self._send_request_and_handle_errors(
973
960
            request=self.client.list_volumes,
974
961
            request_error=None, event_error=None,
975
 
            event_ok=None, fire_deferred=False,
976
 
            handle_exception=False)
 
962
            event_ok=None, handle_exception=False)
977
963
        defer.returnValue(result.volumes)
978
964
 
979
965
    def make_file(self, share_id, parent_id, name, marker):