feat: 根据测点采集监测设备是否掉线使用celery
This commit is contained in:
parent
c1d55ce618
commit
54fc93de73
|
@ -0,0 +1,28 @@
|
||||||
|
# Create your tasks here
|
||||||
|
from __future__ import absolute_import, unicode_literals
|
||||||
|
from apps.utils.tasks import CustomTask
|
||||||
|
from celery import shared_task
|
||||||
|
from django.utils import timezone
|
||||||
|
from apps.em.models import Equipment, RuningState
|
||||||
|
from django.core.cache import cache
|
||||||
|
|
||||||
|
|
||||||
|
@shared_task(base=CustomTask)
|
||||||
|
def check_equipment_offline(seconds=20):
|
||||||
|
"""监测设备是否掉线(根据测点采集)
|
||||||
|
|
||||||
|
监测设备是否掉线
|
||||||
|
"""
|
||||||
|
now = timezone.now()
|
||||||
|
equips = Equipment.objects.filter(mp_ep_monitored__is_rep_ep_running_state=True, mp_ep_monitored__enabled=True)
|
||||||
|
for equip in equips:
|
||||||
|
is_offline = True
|
||||||
|
cache_key = f"equipment_{equip.id}"
|
||||||
|
cache_value = cache.get(cache_key, None)
|
||||||
|
if cache_value:
|
||||||
|
last_timex = cache_value.get("running_state_timex", None)
|
||||||
|
if last_timex and (now - last_timex).total_seconds() <= seconds:
|
||||||
|
is_offline = False
|
||||||
|
if is_offline and equip.running_state != RuningState.OFFLINE:
|
||||||
|
equip.running_state = RuningState.OFFLINE
|
||||||
|
equip.save(update_fields=["running_state"])
|
|
@ -1,38 +0,0 @@
|
||||||
import os
|
|
||||||
import sys
|
|
||||||
import django
|
|
||||||
import logging
|
|
||||||
from django.core.cache import cache
|
|
||||||
from django.utils import timezone
|
|
||||||
import time
|
|
||||||
|
|
||||||
CUR_DIR = os.path.dirname(os.path.abspath(__file__))
|
|
||||||
BASE_DIR = os.path.dirname(CUR_DIR)
|
|
||||||
sys.path.insert(0, BASE_DIR)
|
|
||||||
|
|
||||||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "server.settings")
|
|
||||||
django.setup()
|
|
||||||
|
|
||||||
from apps.em.models import Equipment, RuningState
|
|
||||||
|
|
||||||
|
|
||||||
def run():
|
|
||||||
while True:
|
|
||||||
now = timezone.now()
|
|
||||||
equips = Equipment.objects.all()
|
|
||||||
for equip in equips:
|
|
||||||
is_offline = True
|
|
||||||
cache_key = f"equipment_{equip.id}"
|
|
||||||
cache_value = cache.get(cache_key, None)
|
|
||||||
if cache_value:
|
|
||||||
last_timex = cache_value.get("running_state_timex", None)
|
|
||||||
if last_timex and (now - last_timex).total_seconds() <= 20:
|
|
||||||
is_offline = False
|
|
||||||
if is_offline:
|
|
||||||
equip.running_state = RuningState.OFFLINE
|
|
||||||
equip.save(update_fields=["running_state"])
|
|
||||||
time.sleep(5)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
run()
|
|
Loading…
Reference in New Issue