old_console/devices/services/endpoint/endpoint_redis.py
2024-11-02 14:12:45 +03:00

27 lines
922 B
Python

import datetime
import redis
from django.conf import settings
class RedisInterface:
""" Helper class, to set and get keepalive for Endpoint model """
endpoint_field_name = "ENDPOINT_STATUS_{}"
endpoint_timeout = 120
def __init__(self):
self.redis_instance = redis.StrictRedis(host=getattr(settings, 'REDIS_HOST', 'redis'),
port=getattr(settings, 'REDIS_PORT', 6379))
def get_keepalive(self, pk):
timestamp = self.redis_instance.get(self.endpoint_field_name.format(pk))
if timestamp is None:
return False
delta = datetime.datetime.now() - datetime.datetime.fromtimestamp(float(timestamp.decode("utf-8")))
return delta.seconds < self.endpoint_timeout
def set_keepalive(self, pk):
self.redis_instance.set(self.endpoint_field_name.format(pk), str(datetime.datetime.now().timestamp()))