redis-moment icon indicating copy to clipboard operation
redis-moment copied to clipboard

Is it possible to have BaseSecond?

Open rafaelcapucho opened this issue 8 years ago • 1 comments

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 avatar May 24 '16 16:05 rafaelcapucho

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

rafaelcapucho avatar May 24 '16 19:05 rafaelcapucho