2016-11-13 14:34:38 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2017-12-04 08:41:00 +00:00
|
|
|
import threading
|
|
|
|
import time
|
2016-11-13 14:34:38 +00:00
|
|
|
|
2017-11-14 01:44:16 +00:00
|
|
|
from celery import shared_task
|
2017-12-04 08:41:00 +00:00
|
|
|
from django.core.cache import cache
|
|
|
|
|
|
|
|
from .models import Session
|
|
|
|
|
|
|
|
|
|
|
|
ASSETS_CACHE_KEY = "terminal__session__assets"
|
|
|
|
USERS_CACHE_KEY = "terminal__session__users"
|
|
|
|
SYSTEM_USER_CACHE_KEY = "terminal__session__system_users"
|
|
|
|
CACHE_REFRESH_INTERVAL = 10
|
2017-12-04 12:15:47 +00:00
|
|
|
RUNNING = False
|
2016-11-13 14:34:38 +00:00
|
|
|
|
2017-11-14 01:44:16 +00:00
|
|
|
|
|
|
|
# Todo: 定期清理上报history
|
|
|
|
@shared_task
|
|
|
|
def clean_terminal_history():
|
|
|
|
pass
|
2017-12-04 08:41:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_session_asset_list():
|
|
|
|
return set(list(Session.objects.values_list('asset', flat=True)))
|
|
|
|
|
|
|
|
|
|
|
|
def get_session_user_list():
|
|
|
|
return set(list(Session.objects.values_list('user', flat=True)))
|
|
|
|
|
|
|
|
|
|
|
|
def get_session_system_user_list():
|
|
|
|
return set(list(Session.objects.values_list('system_user', flat=True)))
|
|
|
|
|
|
|
|
|
|
|
|
def set_cache():
|
|
|
|
while True:
|
|
|
|
assets = get_session_asset_list()
|
|
|
|
users = get_session_user_list()
|
|
|
|
system_users = get_session_system_user_list()
|
|
|
|
|
|
|
|
cache.set(ASSETS_CACHE_KEY, assets)
|
|
|
|
cache.set(USERS_CACHE_KEY, users)
|
|
|
|
cache.set(SYSTEM_USER_CACHE_KEY, system_users)
|
|
|
|
time.sleep(10)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2017-12-04 12:15:47 +00:00
|
|
|
global RUNNING
|
|
|
|
if RUNNING:
|
|
|
|
return
|
|
|
|
threads = []
|
2017-12-04 08:41:00 +00:00
|
|
|
thread = threading.Thread(target=set_cache)
|
2017-12-04 12:15:47 +00:00
|
|
|
threads.append(thread)
|
|
|
|
|
|
|
|
for t in threads:
|
|
|
|
t.daemon = True
|
|
|
|
t.start()
|
|
|
|
RUNNING = True
|
2017-12-04 08:41:00 +00:00
|
|
|
|
|
|
|
main()
|