~facundo/ubuntuone-client/lr-dont-delete-partial-nopartialfile

« 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-24 20:52:03 UTC
  • mfrom: (720.1.3 ping-it-baby)
  • Revision ID: tarmac-20100924205203-ql0vtr0qezvuwwoc
Client periodically pings the server to check connection.

Show diffs side-by-side

added added

removed removed

Lines of Context:
43
43
from urlparse import urljoin
44
44
 
45
45
from zope.interface import implements
46
 
from twisted.internet import reactor, defer, threads
 
46
from twisted.internet import reactor, defer, threads, task
47
47
from twisted.internet import error as twisted_errors
48
48
from twisted.names import client as dns_client
49
49
from twisted.python.failure import Failure, DefaultException
120
120
        self.log = logging.getLogger('ubuntuone.SyncDaemon.StorageClient')
121
121
        # configure the handler level to be < than DEBUG
122
122
        self.log.setLevel(TRACE)
123
 
        self.log.debug = partial(self.log.log, TRACE)
 
123
        self.log_trace = partial(self.log.log, TRACE)
124
124
 
125
125
    def processMessage(self, message):
126
126
        """Wrapper that logs the message and result."""
127
127
        # don't log the full message if it's of type BYTES
128
128
        if message.type == protocol_pb2.Message.BYTES:
129
 
            self.log.debug('start - processMessage: id: %s, type: %s',
 
129
            self.log_trace('start - processMessage: id: %s, type: %s',
130
130
                           message.id, message.type)
131
131
        else:
132
 
            self.log.debug('start - processMessage: %s',
 
132
            self.log_trace('start - processMessage: %s',
133
133
                          str(message).replace("\n", " "))
134
134
        if message.id in self.requests:
135
135
            req = self.requests[message.id]
136
136
            req.deferred.addCallbacks(self.log_success, self.log_error)
137
137
        result = ThrottlingStorageClient.processMessage(self, message)
138
 
        self.log.debug('end - processMessage: id: %s - result: %s',
 
138
        self.log_trace('end - processMessage: id: %s - result: %s',
139
139
                       message.id, result)
140
140
        return result
141
141
 
142
142
    def log_error(self, failure):
143
143
        """Logging errback for requests."""
144
 
        self.log.debug('request error: %s', failure)
 
144
        self.log_trace('request error: %s', failure)
145
145
        return failure
146
146
 
147
147
    def log_success(self, result):
148
148
        """Logging callback for requests."""
149
 
        self.log.debug('request finished: %s', result)
 
149
        self.log_trace('request finished: %s', result)
150
150
        if getattr(result, '__dict__', None):
151
 
            self.log.debug('result.__dict__: %s', result.__dict__)
 
151
            self.log_trace('result.__dict__: %s', result.__dict__)
152
152
        return result
153
153
 
154
154
    def sendMessage(self, message):
155
155
        """Wrapper that logs the message and result."""
156
156
        # don't log the full message if it's of type BYTES
157
157
        if message.type == protocol_pb2.Message.BYTES:
158
 
            self.log.debug('start - sendMessage: id: %s, type: %s',
 
158
            self.log_trace('start - sendMessage: id: %s, type: %s',
159
159
                           message.id, message.type)
160
160
        else:
161
 
            self.log.debug('start - sendMessage: %s',
 
161
            self.log_trace('start - sendMessage: %s',
162
162
                          str(message).replace("\n", " "))
163
163
        result = ThrottlingStorageClient.sendMessage(self, message)
164
 
        self.log.debug('end - sendMessage: id: %s', message.id)
 
164
        self.log_trace('end - sendMessage: id: %s', message.id)
165
165
        return result
166
166
 
167
167
 
169
169
    """This is the Action Queue version of the StorageClient protocol."""
170
170
 
171
171
    factory = None
 
172
    _looping_ping = None
 
173
    _ping_delay = 600  # 10 minutes
172
174
 
173
175
    def connectionMade(self):
174
176
        """A new connection was made."""
175
177
        self.log.info('Connection made.')
176
178
        self.factory.event_queue.push('SYS_CONNECTION_MADE')
 
179
        if self._looping_ping is None:
 
180
            self._looping_ping = task.LoopingCall(self._do_ping)
 
181
            self._looping_ping.start(self._ping_delay, now=False)
177
182
 
178
183
    def connectionLost(self, reason):
179
184
        """The connection was lost."""
180
185
        self.log.info('Connection lost, reason: %s.', reason)
 
186
        if self._looping_ping is not None:
 
187
            self._looping_ping.stop()
 
188
            self._looping_ping = None
 
189
 
 
190
    @defer.inlineCallbacks
 
191
    def _do_ping(self):
 
192
        """Ping the server just to use the network."""
 
193
        req = yield self.ping()
 
194
        self.log.debug("Ping! rtt: %.3f segs", req.rtt)
181
195
 
182
196
 
183
197
class Marker(str):