def handle(self, source_spec, target_spec, *args, **options): # Set up logger logger = logging.getLogger('relax.couchdb.replicate') handler = logging.StreamHandler() handler.setFormatter(DEFAULT_FORMATTER) logger.addHandler(handler) if settings._('DEBUG', True): logger.setLevel(logging.DEBUG) logger.propagate = False # Replicate the databases logger.info('Beginning replication.') try: replicate.replicate(source_spec, target_spec) except replicate.ReplicationFailure, exc: # If there was an error, write the error output from CouchDB to a # rotated replication log file. log_filename = logrotate('replication.log') logger.error('Dumping JSON error information to "%s"' % ( log_filename,)) fp = open(log_filename, 'w') try: fp.write('response_headers = %s' % ( json.dumps(exc.response_headers),)) fp.write(os.linesep * 2) fp.write('result = %s' % ( json.dumps(exc.response_headers),)) fp.write(os.linesep) finally: fp.close()
def handle(self): """The main function called to handle a request.""" while True: try: line = self.rfile.readline() try: # All input data are lines of JSON like the following: # ["<cmd_name>" "<cmd_arg1>" "<cmd_arg2>" ...] # So I handle this by dispatching to various methods. cmd = json.loads(line) except Exception, exc: # Sometimes errors come up. Once again, I can't predict # anything, but can at least tell CouchDB about the error. self.wfile.write(repr(exc) + NEWLINE) continue else: # Automagically get the command handler. handler = getattr(self, 'handle_' + cmd[0], None) if not handler: # We are ready to not find commands. It probably won't # happen, but fortune favours the prepared. self.wfile.write( repr(CommandNotFound(cmd[0])) + NEWLINE) continue return_value = handler(*cmd[1:]) if not return_value: continue # We write the output back to CouchDB. self.wfile.write( one_lineify(json.dumps(return_value)) + NEWLINE)
def js_error(exc): """Transform a Python exception into a CouchDB JSON error.""" # This is the format in which CouchDB interprets errors. return json.dumps({ 'error': type(exc).__name__, 'reason': str(exc)})
def log(self, string): """Log an event on the CouchDB server.""" self.wfile.write(json.dumps({'log': string}) + NEWLINE)