caxap / redis-moment

A powerful analytics python library for Redis.
MIT License
36 stars 1 forks source link

Is it possible to have BaseSecond? #1

Open rafaelcapucho opened 8 years ago

rafaelcapucho commented 8 years ago

Hello,

I need to monitor which python server is online, I can send packages from those servers to redis like a heartbeat (pinging) but I can't filter in a BaseDay interval, when a server stop working it should me removed from the monitoring system asap.

It is a realtime distributed scrapy monitor, made with React: server

redis-moment can help me? Thank you

rafaelcapucho commented 8 years ago

I have solved with it:

from datetime import datetime, timezone, timedelta

import gevent

def heartbeat(redis_conn, ip, hostname):

    while True:

        future = datetime.now(timezone.utc) + timedelta(seconds=6)

        redis_conn.zadd(
            'servers',
            '{ip}-{hostname}'.format(ip=ip, hostname=hostname),
            int(future.timestamp())
        )

        now = datetime.now(timezone.utc)

        servers = redis_conn.zrangebyscore('servers', now.timestamp(), max='+inf')

        print('servers: ', servers)

        gevent.sleep(3)

Good enough, thank you