Esempio n. 1
0
 def execute(self, query, parameters):
     self.momoko_connection = adb._get_connection()
     if not self.momoko_connection:
         if self.state == 'start':
             log.info('No connection')
             self.write_message('BUSY|Server busy, waiting for slot')
             self.state = 'busy'
         if self.ws_connection:
             return adb._ioloop.add_callback(partial(
                 self.execute, query, parameters))
     if not self.ws_connection:
         return
     self.state = 'executing'
     self.write_message('BEGIN|Searching')
     self.connection = self.momoko_connection.connection
     self.cursor = self.connection.cursor(
         cursor_factory=psycopg2.extras.NamedTupleCursor)
     self.cursor.execute(
         'BEGIN;'
         'DECLARE visit_cur SCROLL CURSOR FOR '
         '%s;'
         'SELECT null as id;' % query, parameters)
         # 'FETCH FORWARD 1 FROM visit_cur;' % query, parameters)
     self.momoko_connection.ioloop.add_handler(
         self.momoko_connection.fileno,
         self.io_callback,
         IOLoop.WRITE)
Esempio n. 2
0
 def on_close(self):
     log.info('Closing %r' % self)
     if self.state is None or self.state == 'terminated':
         return
     if self.state == 'paused':
         self.state = 'terminated'
         self.cursor.execute('CLOSE visit_cur; ROLLBACK; -- ONCLOSE')
     elif hasattr(self, 'connection'):
         self.connection.cancel()
Esempio n. 3
0
 def open(self):
     log.info('Opening query websocket')
     site = self.get_secure_cookie('_pystil_site')
     site = site and site.decode('utf-8').split('|')[0]
     self.query = None
     self.state = None
     if site is None:
         log.warn('Query websocket open without secure cookie')
         self.close()
         return
Esempio n. 4
0
 def open(self):
     log.info('Opening last visits websocket')
     site = self.get_secure_cookie('_pystil_site')
     site = site and site.decode('utf-8').split('|')[0]
     if site is not None:
         self.site = site
         LastVisitsWebSocket.waiters.add(self)
     else:
         log.warn('Last visits websocket open without secure cookie')
         self.close()
Esempio n. 5
0
    def io_callback(self, fd=None, events=None):
        try:
            state = self.connection.poll()
        except psycopg2.extensions.QueryCanceledError:
            log.info('Canceling request %r' % self, exc_info=True)
            self.cursor.execute('ROLLBACK -- CANCEL')
            self.state = 'terminated'
        except (psycopg2.Warning, psycopg2.Error):
            log.exception('Poll error')
            self.momoko_connection.ioloop.remove_handler(
                self.momoko_connection.fileno)
            raise
        else:
            if state == POLL_OK:
                if self.state == 'terminated':
                    self.momoko_connection.ioloop.remove_handler(
                        self.momoko_connection.fileno)
                    return

                rows = self.cursor.fetchmany()

                if not rows:
                    self.state = 'terminated'
                    self.cursor.execute('CLOSE visit_cur; ROLLBACK; -- NOROWS')
                    try:
                        self.write_message('END|Done found %d visit%s' % (
                            self.count, 's' if self.count > 1 else ''))
                    except:
                        pass
                else:
                    try:
                        for row in rows:
                            if row.id:
                                self.count += 1
                                self.write_message(
                                    'VISIT|' + visit_to_table_line(row))
                    except Exception as e:
                        log.warn('During write', exc_info=True)
                        self.state = 'terminated'
                        self.cursor.execute(
                            'CLOSE visit_cur; ROLLBACK; -- WSERROR')
                        try:
                            self.write_message(
                                'END|%s: %s' % (type(e), str(e)))
                        except:
                            pass
                    else:
                        if self.count < self.stop:
                            self.cursor.execute(
                                'FETCH FORWARD 1 FROM visit_cur;')
                        else:
                            self.state = 'paused'
                            try:
                                self.write_message(
                                    'PAUSE|Paused on %d visits' % self.count)
                            except:
                                pass
            elif state == POLL_READ:
                self.momoko_connection.ioloop.update_handler(
                    self.momoko_connection.fileno, IOLoop.READ)
            elif state == POLL_WRITE:
                self.momoko_connection.ioloop.update_handler(
                    self.momoko_connection.fileno, IOLoop.WRITE)
            else:
                raise psycopg2.OperationalError(
                    'poll() returned {0}'.format(state))