Exemplo n.º 1
0
class TrunkTest(TestCase):
    def setUp(self):
        dsn = os.environ.get("DATABASE_URL")
        self.listener = Trunk(dsn or "postgres://localhost/trunk")
        self.notifier = Trunk(dsn or "postgres://localhost/trunk")

    def test_get(self):
        self.listener.listen("trunk_get")
        self.notifier. notify("trunk_get", "payload")
        channel, payload = self.listener.get("trunk_get")
        self.assertEqual(channel, "trunk_get")
        self.assertEqual(payload, "payload")

    def test_listen(self):
        self.listener.listen("trunk_listen")
        self.assertTrue("trunk_listen" in self.listener.channels())

    def test_unlisten(self):
        self.listener.listen("trunk_channels")
        self.assertTrue("trunk_channels" in self.listener.channels())
        self.listener.unlisten("trunk_channels")
        self.assertTrue("trunk_channels" not in self.listener.channels())

    def tearDown(self):
        self.listener.close()
        self.notifier.close()
Exemplo n.º 2
0
class TrunkTest(TestCase):
    def setUp(self):
        dsn = os.environ.get("DATABASE_URL")
        self.listener = Trunk(dsn or "postgres://localhost/trunk")
        self.notifier = Trunk(dsn or "postgres://localhost/trunk")

    def test_get(self):
        self.listener.listen("trunk_get")
        self.notifier.notify("trunk_get", "payload")
        channel, payload = self.listener.get("trunk_get")
        self.assertEqual(channel, "trunk_get")
        self.assertEqual(payload, "payload")

    def test_listen(self):
        self.listener.listen("trunk_listen")
        self.assertTrue("trunk_listen" in self.listener.channels())

    def test_unlisten(self):
        self.listener.listen("trunk_channels")
        self.assertTrue("trunk_channels" in self.listener.channels())
        self.listener.unlisten("trunk_channels")
        self.assertTrue("trunk_channels" not in self.listener.channels())

    def tearDown(self):
        self.listener.close()
        self.notifier.close()
Exemplo n.º 3
0
class PGQueue(object):
    def __init__(self, dsn):
        self.trunk = Trunk(dsn)

    def create(self, name):
        self.trunk.listen(name)
        # This forces db trigger to resend notifications about pending messages.
        with self.trunk.cursor() as cursor:
            cursor.execute("UPDATE trunk_queue SET locked_at = NULL WHERE name = %s AND locked_at IS NULL", (name, ))

    def get(self, name, block=True, timeout=None):
        channel, payload = self.trunk.get(name, block=block, timeout=timeout)
        with self.trunk.cursor() as cursor:
            cursor.execute('UPDATE trunk_queue SET locked_at = (CURRENT_TIMESTAMP) '
                           'WHERE id = %s AND name = %s AND locked_at is NULL', (payload, name))
            row_count = cursor.rowcount
            if not row_count:
                raise Empty()
            cursor.execute("SELECT id, message FROM trunk_queue WHERE id = %s", (payload, ))
            row = cursor.fetchone()
            if row is None:
                raise Empty()
            return row

    def get_nowait(self, name):
        return self.get(name, block=False)

    def put(self, name, message):
        with self.trunk.cursor() as cursor:
            cursor.execute("INSERT INTO trunk_queue (name, message) VALUES (%s, %s)", (name, message))

    def empty(self, name):
        return 0 == self.qsize(name)

    def qsize(self, name):
        with self.trunk.cursor() as cursor:
            cursor.execute("SELECT COUNT(*) FROM trunk_queue WHERE name = %s", (name,))
            row = cursor.fetchone()
            return row[0]

    def purge(self, name):
        size = self.qsize(name)
        with self.trunk.cursor() as cursor:
            cursor.execute("DELETE FROM trunk_queue WHERE name = %s", (name,))
        return size

    def restore(self, message):
        message_id = message._raw['message_id']
        with self.trunk.cursor() as cursor:
            cursor.execute('UPDATE trunk_queue SET locked_at = NULL WHERE id = %s', (message_id, ))

    def close(self):
        self.trunk.close()
Exemplo n.º 4
0
class PGQueue(object):
    def __init__(self, dsn):
        self.trunk = Trunk(dsn)

    def create(self, name):
        self.trunk.listen(name)

    def get(self, name, block=True, timeout=None):
        try:
            channel, payload = self.trunk.get(name,
                                              block=block,
                                              timeout=timeout)
        except Empty:
            pass
        with self.trunk.cursor() as cursor:
            cursor.execute("SELECT id, message FROM public.pop_lock(%s)",
                           (name, ))
            row = cursor.fetchone()
            if row is None:
                raise Empty()
            return row

    def get_nowait(self, name):
        return self.get(name, block=False)

    def put(self, name, message):
        with self.trunk.cursor() as cursor:
            cursor.execute(
                "INSERT INTO public.trunk_queue (name, message) VALUES (%s, %s)",
                (name, message))
        self.trunk.notify(name)

    def empty(self, name):
        return 0 == self.qsize(name)

    def qsize(self, name):
        with self.trunk.cursor() as cursor:
            cursor.execute(
                "SELECT COUNT(*) FROM public.trunk_queue WHERE name = %s",
                (name, ))
            row = cursor.fetchone()
            return row[0]

    def purge(self, name):
        size = self.qsize(name)
        with self.trunk.cursor() as cursor:
            cursor.execute("DELETE FROM public.trunk_queue WHERE name = %s",
                           (name, ))
        return size

    def close(self):
        self.trunk.close()
Exemplo n.º 5
0
class PGQueue(object):
    def __init__(self, dsn):
        self.trunk = Trunk(dsn)

    def create(self, name):
        self.trunk.listen(name)

    def get(self, name, block=True, timeout=None):
        channel, payload = self.trunk.get(name, block=block, timeout=timeout)
        with self.trunk.cursor() as cursor:
            cursor.execute("SELECT id, message FROM pop_lock(%s)", (name,))
            row = cursor.fetchone()
            if row is None:
                raise Empty()
            return row

    def get_nowait(self, name):
        return self.get(name, block=False)

    def put(self, name, message):
        with self.trunk.cursor() as cursor:
            cursor.execute("INSERT INTO trunk_queue (name, message) VALUES (%s, %s)", (name, message))
        self.trunk.notify(name)

    def empty(self, name):
        return 0 == self.qsize(name)

    def qsize(self, name):
        with self.trunk.cursor() as cursor:
            cursor.execute("SELECT COUNT(*) FROM trunk_queue WHERE name = %s", (name,))
            row = cursor.fetchone()
            return row[0]

    def purge(self, name):
        size = self.qsize(name)
        with self.trunk.cursor() as cursor:
            cursor.execute("DELETE FROM trunk_queue WHERE name = %s", (name,))
        return size

    def close(self):
        self.trunk.close()