Commit 419876b5 authored by ibuler's avatar ibuler

Update heatbeat

parent 26352174
...@@ -2,5 +2,4 @@ from __future__ import absolute_import ...@@ -2,5 +2,4 @@ from __future__ import absolute_import
# This will make sure the app is always imported when # This will make sure the app is always imported when
# Django starts so that shared_task will use this app. # Django starts so that shared_task will use this app.
# from .celery import app as celery_app from .celery import app as celery_app
# # ~*~ coding: utf-8 ~*~ # ~*~ coding: utf-8 ~*~
#
# from __future__ import absolute_import, unicode_literals from __future__ import absolute_import, unicode_literals
# import os import os
# from datetime import timedelta
# from celery import Celery
# from celery import Celery
# # set the default Django settings module for the 'celery' program.
# os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'jumpserver.settings') # set the default Django settings module for the 'celery' program.
# os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'jumpserver.settings')
# from django.conf import settings
# from django.conf import settings
# app = Celery('jumpserver')
# app = Celery('jumpserver')
# # Using a string here means the worker will not have to
# # pickle the object when using Windows. # Using a string here means the worker will not have to
# app.config_from_object('django.conf:settings') # pickle the object when using Windows.
# app.config_from_object('django.conf:settings')
# app.autodiscover_tasks(lambda: [app_config.split('.')[0] for app_config in settings.INSTALLED_APPS]) app.autodiscover_tasks(lambda: [app_config.split('.')[0] for app_config in settings.INSTALLED_APPS])
#
from __future__ import absolute_import from __future__ import absolute_import
import os # from celery import shared_task
from celery import shared_task
from celery.schedules import crontab
from django.core.mail import send_mail from django.core.mail import send_mail
# from django.conf import settings
# from common import celery_app
from celery import Celery
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'jumpserver.settings')
from django.conf import settings from django.conf import settings
from common import celery_app as app
app = Celery('jumpserver')
# Using a string here means the worker will not have to
# pickle the object when using Windows.
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: [app_config.split('.')[0] for app_config in settings.INSTALLED_APPS])
@app.task @app.task
...@@ -44,17 +27,3 @@ def send_mail_async(*args, **kwargs): ...@@ -44,17 +27,3 @@ def send_mail_async(*args, **kwargs):
args = tuple(args) args = tuple(args)
send_mail(*args, **kwargs) send_mail(*args, **kwargs)
# @celery_app.task
# def test(arg):
# print(arg)
# celery_app.conf.beat_schedule = {
# 'add-every-30-seconds': {
# 'task': 'common.test',
# 'schedule': crontab(minute='*/1'),
# 'args': ('nihao',)
# }
# }
...@@ -14,6 +14,7 @@ import os ...@@ -14,6 +14,7 @@ import os
import sys import sys
from django.urls import reverse_lazy from django.urls import reverse_lazy
from datetime import timedelta
# Build paths inside the project like this: os.path.join(BASE_DIR, ...) # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
...@@ -284,6 +285,19 @@ BROKER_URL = 'redis://%(password)s%(host)s:%(port)s/3' % { ...@@ -284,6 +285,19 @@ BROKER_URL = 'redis://%(password)s%(host)s:%(port)s/3' % {
} }
CELERY_RESULT_BACKEND = BROKER_URL CELERY_RESULT_BACKEND = BROKER_URL
# TERMINAL_HEATBEAT_INTERVAL = CONFIG.TERMINAL_HEATBEAT_INTERVAL or 30
# crontab job
# CELERYBEAT_SCHEDULE = {
# Check terminal is alive every 10m
# 'check_terminal_alive': {
# 'task': 'terminal.tasks.check_terminal_alive',
# 'schedule': timedelta(seconds=TERMINAL_HEATBEAT_INTERVAL),
# 'args': (),
# },
# }
# Cache use redis # Cache use redis
CACHES = { CACHES = {
'default': { 'default': {
......
...@@ -4,17 +4,17 @@ ...@@ -4,17 +4,17 @@
from django.core.cache import cache from django.core.cache import cache
from django.conf import settings from django.conf import settings
from rest_framework.generics import ListCreateAPIView, RetrieveUpdateDestroyAPIView from rest_framework.generics import ListCreateAPIView, RetrieveUpdateDestroyAPIView
from rest_framework import viewsets
from rest_framework.views import APIView, Response from rest_framework.views import APIView, Response
from rest_framework.viewsets import ModelViewSet
from rest_framework.permissions import AllowAny from rest_framework.permissions import AllowAny
from common.utils import signer, get_object_or_none from common.utils import signer, get_object_or_none
from .models import Terminal, HeatbeatFailedLog from .models import Terminal, TerminalHeatbeat
from .serializers import TerminalSerializer, TerminalHeatbeatSerializer from .serializers import TerminalSerializer, TerminalHeatbeatSerializer
from .hands import IsSuperUserOrTerminalUser from .hands import IsSuperUserOrTerminalUser
class TerminalViewSet(ModelViewSet): class TerminalViewSet(viewsets.ModelViewSet):
queryset = Terminal.objects.all() queryset = Terminal.objects.all()
serializer_class = TerminalSerializer serializer_class = TerminalSerializer
permission_classes = (AllowAny,) permission_classes = (AllowAny,)
...@@ -46,18 +46,19 @@ class TerminalViewSet(ModelViewSet): ...@@ -46,18 +46,19 @@ class TerminalViewSet(ModelViewSet):
return Response(data={'msg': 'Secrete key invalid'}, status=401) return Response(data={'msg': 'Secrete key invalid'}, status=401)
class TerminalHeatbeatApi(APIView): class TerminalHeatbeatApi(ListCreateAPIView):
# model = HeatbeatFailedLog queryset = TerminalHeatbeat.objects.all()
# serializer_class = TerminalHeatbeatSerializer serializer_class = TerminalHeatbeatSerializer
permission_classes = (IsSuperUserOrTerminalUser,) permission_classes = (IsSuperUserOrTerminalUser,)
def put(self, request, *args, **kwargs):
terminal_id = request.user.id
cache.set('terminal_heatbeat_%s' % terminal_id, settings.CONFIG.TERMINAL_HEATBEAT_INTERVAL * 3)
return Response({'msg': 'Success'})
class TerminalHeatbeatViewSet(viewsets.ModelViewSet):
queryset = TerminalHeatbeat.objects.all()
serializer_class = TerminalHeatbeatSerializer
permission_classes = (IsSuperUserOrTerminalUser,)
def create(self, request, *args, **kwargs):
terminal = request.user
TerminalHeatbeat.objects.create(terminal=terminal)
return Response({'msg': 'Success'})
# class TerminalApiDetailUpdateDetailApi(RetrieveUpdateDestroyAPIView):
# queryset = Terminal.objects.all()
# serializer_class = TerminalSerializer
# permission_classes = (IsSuperUserOrTerminalUser,)
...@@ -36,10 +36,9 @@ class Terminal(models.Model): ...@@ -36,10 +36,9 @@ class Terminal(models.Model):
ordering = ['is_active'] ordering = ['is_active']
class HeatbeatFailedLog(models.Model): class TerminalHeatbeat(models.Model):
"""Terminal heatbeat failed log"""
terminal = models.ForeignKey(Terminal, on_delete=models.CASCADE) terminal = models.ForeignKey(Terminal, on_delete=models.CASCADE)
date_created = models.DateTimeField(auto_now_add=True) date_created = models.DateTimeField(auto_now_add=True)
class Meta: class Meta:
db_table = 'heatbeat_failed_log' db_table = 'terminal_heatbeat'
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# #
from django.utils import timezone
from rest_framework import serializers from rest_framework import serializers
from .models import Terminal, HeatbeatFailedLog from .models import Terminal, TerminalHeatbeat
from .hands import ProxyLog from .hands import ProxyLog
class TerminalSerializer(serializers.ModelSerializer): class TerminalSerializer(serializers.ModelSerializer):
proxy_online = serializers.SerializerMethodField() proxy_online = serializers.SerializerMethodField()
is_alive = serializers.SerializerMethodField()
class Meta: class Meta:
model = Terminal model = Terminal
fields = ['id', 'name', 'ip', 'type', 'url', 'comment', fields = ['id', 'name', 'ip', 'type', 'url', 'comment',
'is_active', 'get_type_display', 'proxy_online'] 'is_active', 'get_type_display', 'proxy_online', 'is_alive']
@staticmethod @staticmethod
def get_proxy_online(obj): def get_proxy_online(obj):
return ProxyLog.objects.filter(terminal=obj.name, is_finished=False).count() return ProxyLog.objects.filter(terminal=obj.name, is_finished=False).count()
@staticmethod
def get_is_alive(obj):
log = obj.terminalheatbeat_set.last()
if timezone.now() - log.date_created > timezone.timedelta(seconds=600):
return False
else:
return True
class TerminalHeatbeatSerializer(serializers.ModelSerializer): class TerminalHeatbeatSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = HeatbeatFailedLog model = TerminalHeatbeat
fields = ['terminal']
if __name__ == '__main__': if __name__ == '__main__':
......
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
...@@ -16,10 +16,10 @@ urlpatterns = [ ...@@ -16,10 +16,10 @@ urlpatterns = [
] ]
router = routers.DefaultRouter() router = routers.DefaultRouter()
router.register(r'v1/terminal/heatbeat', api.TerminalHeatbeatViewSet, 'api-terminal-heatbeat')
router.register(r'v1/terminal', api.TerminalViewSet, 'api-terminal') router.register(r'v1/terminal', api.TerminalViewSet, 'api-terminal')
# urlpatterns += [
urlpatterns += [ # url(r'v1/terminal/heatbeat/', api.TerminalHeatbeatApi.as_view(), name='api-terminal-heatbeat')
url(r'^v1/terminal/heatbeat/$', api.TerminalHeatbeatApi.as_view(), name='terminal-heatbeat-api'), # ]
]
urlpatterns += router.urls urlpatterns += router.urls
...@@ -30,7 +30,7 @@ def start_celery(): ...@@ -30,7 +30,7 @@ def start_celery():
os.chdir(apps_dir) os.chdir(apps_dir)
os.environ.setdefault('C_FORCE_ROOT', '1') os.environ.setdefault('C_FORCE_ROOT', '1')
print('start celery') print('start celery')
subprocess.call('celery -A common worker -l info', shell=True) subprocess.call('celery -A common worker -B -s /tmp/celerybeat-schedule -l info ', shell=True)
def main(): def main():
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment