Esempio n. 1
0
def test_dynamic_timeout(db):
    def get_timeout():
        return -1

    for n in await_pg_notifications(
            db,
        ["hello", "hello2"],
            timeout=get_timeout,
            yield_on_timeout=True,
            notifications_as_list=True,
            handle_signals=SIGNALS_TO_HANDLE,
    ):
        if n is None:
            with S(db) as s:
                s.execute("notify hello, 'here is my message'")

        elif isinstance(n, int):
            sig = signal.Signals(n)
            assert sig.name == "SIGINT"
            assert n == signal.SIGINT
            break

        else:
            assert len(n) == 1
            _n = n[0]
            assert _n.channel == "hello"
            assert _n.payload == "here is my message"
            os.kill(os.getpid(), signal.SIGINT)

    with raises(KeyboardInterrupt):
        for n in await_pg_notifications(db,
                                        "hello",
                                        timeout=0.1,
                                        yield_on_timeout=True):
            os.kill(os.getpid(), signal.SIGINT)
Esempio n. 2
0
    def listen():
        for notification in await_pg_notifications(
                f"postgresql://{db['USER']}:{db['PASSWORD']}@{db['HOST']}:{db['PORT']}/{db['NAME']}",
            ['short_url_created']):
            try:
                payload = json.loads(notification.payload)
            except json.JSONDecodeError as e:
                log.error(e)
                continue

            redis = get_redis_connection("short")
            try:
                with redis.lock('short'):
                    fetch = redis.get(payload['endpoint'])
                    timestamp = maya.now().epoch
                    if fetch and fetch['timestamp'] > timestamp:
                        continue
                    data = {
                        'url': payload['url'],
                        'timestamp': maya.now().epoch
                    }
                    redis.set(payload['endpoint'], json.dumps(data))
            except KeyError as e:
                log.error(e)
                continue
            log.debug(f'payload: {payload}')
Esempio n. 3
0
    def _start(self):
        for notification in await_pg_notifications(
                f'postgres://{self.configs["postgres_string"]}',
            [f'{self.configs["pg_notify_channel"]}']):
            payload = json.loads(notification.payload)
            table = payload['reg_tabela']
            valores = payload['reg_dados_pk'].split('|').pop(0)
            recnum = payload['reg_recnum']
            operation = payload['reg_evento']

            if table in ('ECFA209'):
                self.service.sync(table, recnum, operation)
Esempio n. 4
0
def test_pg_notify(tmpdb):
    db = results.db(tmpdb)

    for n in await_pg_notifications(tmpdb,
                                    channels=["channel"],
                                    timeout=1,
                                    yield_on_timeout=True):

        if not n:
            db.pg_notify("channel", "payload")

        else:
            n.channel == "channel"
            n.payload == "payload"
            break
def main():
    """Listen to channel prices from PostgresSQL database and print any price drops"""

    args = parse_args()
    setup_log()

    connection = psycopg2.connect(
        user='******',
        password='******',
        host=os.environ['JMC_PRICE_YAK_DB_SERVICE_HOST'],
        port=os.environ['JMC_PRICE_YAK_DB_SERVICE_PORT'],
        database='my_postgres_db')

    channel = 'prices'

    logging.info('Listening for price drops on channel {}...'.format(channel))

    for notification in await_pg_notifications(connection, [channel]):

        logging.debug(
            'Received notification on channel {} with payload {}'.format(
                notification.channel, notification.payload))

        asin = notification.payload

        last_two_offers = api.select_latest_offers(asin, 2)

        if not last_two_offers or len(last_two_offers) != 2:
            logging.debug(
                'Could not find two records for asin {}'.format(asin))
            continue

        curr_price = last_two_offers[0]['price']
        curr_currency = last_two_offers[0]['currency']
        prev_price = last_two_offers[1]['price']
        prev_currency = last_two_offers[1]['currency']

        if curr_price < prev_price:
            curr_dollars = int(curr_price / 100)
            curr_cents = int(curr_price % 100)
            prev_dollars = int(prev_price / 100)
            prev_cents = int(prev_price % 100)
            logging.info(
                'Price of {} dropped from ${}.{:02d} {} to ${}.{:02d} {}!'.
                format(asin, prev_dollars, prev_cents, prev_currency,
                       curr_dollars, curr_cents, curr_currency))
Esempio n. 6
0
def main():
    L = getLogger()
    channels = [
        "sysm_watch",
    ]
    signals_to_handle = [signal.SIGINT, signal.SIGTERM]
    conn = pgnotify.get_dbapi_connection(DB_URI)
    for ev in pgnotify.await_pg_notifications(
            conn,
            channels,
            timeout=5,
            yield_on_timeout=True,
            handle_signals=signals_to_handle):
        if isinstance(ev, int):
            sig = signal.Signals(ev)
            L.warning("Handling %s: stopping...", sig.name)
            break
        elif ev is None:
            L.debug("continue.")
        else:
            print(f"{ev.pid}:{ev.channel}:{ev.payload}")
    return 0
Esempio n. 7
0
    def _listen(self) -> None:
        """
        Подписка на список каналов

        Основыне задачи метода:
         - подписка на канал
         - ожидание сообщений
            - если ни одного сообщения пока не пришло, то производится проверка
              на признак запуска периодической/первичной задачи
            - если пришло сообщение от одного из каналов, то запускается функция обработки данного сообщения

        :return:
        """
        if self._table_list:
            is_continue = True
            while is_continue:
                try:
                    for n in await_pg_notifications(
                            self._e,
                        [self._channel_name],
                            timeout=10,
                            yield_on_timeout=True,
                            handle_signals=self._signals_to_handle,
                    ):
                        if isinstance(n, int):
                            sig = signal.Signals(n)
                            is_continue = False
                            break
                        elif n is None:
                            if self.is_period:
                                self.period_task()
                        elif n is not None:
                            if n.payload in self._table_list:
                                self.add_task(n.channel, n.payload)
                except KeyboardInterrupt:
                    if self.pool_task:
                        del self.pool_task
Esempio n. 8
0
import os

import psycopg2
from pgnotify import await_pg_notifications, get_dbapi_connection

DB_HOST = os.environ.get('DB_HOST', 'localhost')

CONNECT = f'postgresql://*****:*****@{DB_HOST}/study'

conn = psycopg2.connect(CONNECT)
cur = conn.cursor()

e = get_dbapi_connection(CONNECT)

print('Starting to wait notification')

for notification in await_pg_notifications(e, ['trait_change']):
    print(notification.channel)
    print(notification.payload)
    id = int(notification.payload)
    cur.execute("SELECT * FROM events where id=%s;", (id, ))
    print(cur.fetchone())
Esempio n. 9
0
from pgnotify import await_pg_notifications, get_dbapi_connection

# the first parameter of the await_pg_notifications
# loop is a dbapi connection in autocommit mode
CONNECT = "postgresql:///example"

# use this convenient method to create the right connection
# from a database URL
e = get_dbapi_connection(CONNECT)

SIGNALS_TO_HANDLE = [signal.SIGINT, signal.SIGTERM]

for n in await_pg_notifications(
        e,
    ["hello", "hello2"],
        timeout=10,
        yield_on_timeout=True,
        handle_signals=SIGNALS_TO_HANDLE,
):
    # the integer code of the signal is yielded on each
    # occurrence of a handled signal
    if isinstance(n, int):
        sig = signal.Signals(n)
        print(f"handling {sig.name}, stopping")
        break

    # the `yield_on_timeout` option makes the
    # loop yield `None` on timeout
    elif n is None:
        print("timeout, continuing")
Esempio n. 10
0
from pgnotify import await_pg_notifications
import websocket

ws = websocket.WebSocket()
ws.connect("ws://127.0.0.1:5000/api")

for notification in await_pg_notifications(
        'postgresql://*****:*****@localhost:5432/myproject', [
            'db_notifications',
        ]):
    ws.send(notification.payload)
    # print(notification.channel)
    # print()