mirror of https://github.com/openspug/spug
U 优化通知信息写入
parent
8d264fe3fe
commit
4a3b47f2c2
|
@ -18,7 +18,6 @@ import json
|
|||
import time
|
||||
|
||||
logger = logging.getLogger("django.apps.monitor")
|
||||
counter = dict()
|
||||
|
||||
|
||||
class Scheduler:
|
||||
|
@ -55,32 +54,13 @@ class Scheduler:
|
|||
obj = SimpleLazyObject(lambda: Detection.objects.filter(pk=event.job_id).first())
|
||||
if event.code == events.EVENT_SCHEDULER_SHUTDOWN:
|
||||
logger.info(f'EVENT_SCHEDULER_SHUTDOWN: {event}')
|
||||
Notify.objects.create(
|
||||
title='调度器已关闭',
|
||||
source='monitor',
|
||||
content='调度器意外关闭,你可以在github上提交issue',
|
||||
type='1',
|
||||
)
|
||||
Notify.make_notify('monitor', '1', '调度器已关闭', '调度器意外关闭,你可以在github上提交issue', False)
|
||||
elif event.code == events.EVENT_JOB_MAX_INSTANCES:
|
||||
logger.info(f'EVENT_JOB_MAX_INSTANCES: {event}')
|
||||
if time.time() - counter.get(event.job_id, time.time()) > 3600:
|
||||
counter[event.job_id] = time.time()
|
||||
Notify.objects.create(
|
||||
title=f'{obj.name} - 达到调度实例上限',
|
||||
source='monitor',
|
||||
content='一般为上个周期的执行任务还未结束,请增加调度间隔或减少任务执行耗时',
|
||||
type='1',
|
||||
)
|
||||
Notify.make_notify('monitor', '1', f'{obj.name} - 达到调度实例上限', '一般为上个周期的执行任务还未结束,请增加调度间隔或减少任务执行耗时')
|
||||
elif event.code == events.EVENT_JOB_ERROR:
|
||||
logger.info(f'EVENT_JOB_ERROR: job_id {event.job_id} exception: {event.exception}')
|
||||
if time.time() - counter.get(event.job_id, time.time()) > 3600:
|
||||
counter[event.job_id] = time.time()
|
||||
Notify.objects.create(
|
||||
title=f'{obj.name} - 执行异常',
|
||||
source='monitor',
|
||||
content=f'{event.exception}',
|
||||
type='1',
|
||||
)
|
||||
Notify.make_notify('monitor', '1', f'{obj.name} - 执行异常', f'{event.exception}')
|
||||
elif event.code == events.EVENT_JOB_EXECUTED:
|
||||
obj = Detection.objects.filter(pk=event.job_id).first()
|
||||
old_status = obj.latest_status
|
||||
|
|
|
@ -2,7 +2,9 @@
|
|||
# Copyright: (c) <spug.dev@gmail.com>
|
||||
# Released under the MIT License.
|
||||
from django.db import models
|
||||
from django.core.cache import cache
|
||||
from libs import ModelMixin, human_datetime
|
||||
import time
|
||||
|
||||
|
||||
class Notify(models.Model, ModelMixin):
|
||||
|
@ -23,6 +25,12 @@ class Notify(models.Model, ModelMixin):
|
|||
|
||||
created_at = models.CharField(max_length=20, default=human_datetime)
|
||||
|
||||
@classmethod
|
||||
def make_notify(cls, source, title, type, content=None, with_quiet=True):
|
||||
if not with_quiet or time.time() - cache.get('spug:notify_quiet', 0) > 3600:
|
||||
cache.set('spug:notify_quiet', time.time())
|
||||
cls.objects.create(source=source, title=title, type=type, content=content)
|
||||
|
||||
def __repr__(self):
|
||||
return '<Notify %r>' % self.title
|
||||
|
||||
|
|
|
@ -41,32 +41,13 @@ class Scheduler:
|
|||
obj = SimpleLazyObject(lambda: Task.objects.filter(pk=event.job_id).first())
|
||||
if event.code == events.EVENT_SCHEDULER_SHUTDOWN:
|
||||
logger.info(f'EVENT_SCHEDULER_SHUTDOWN: {event}')
|
||||
Notify.objects.create(
|
||||
title='调度器已关闭',
|
||||
source='schedule',
|
||||
content='调度器意外关闭,你可以在github上提交issue',
|
||||
type='1',
|
||||
)
|
||||
Notify.make_notify('schedule', '1', '调度器已关闭', '调度器意外关闭,你可以在github上提交issue')
|
||||
elif event.code == events.EVENT_JOB_MAX_INSTANCES:
|
||||
logger.info(f'EVENT_JOB_MAX_INSTANCES: {event}')
|
||||
if time.time() - counter.get(event.job_id, 0) > 3600:
|
||||
counter[event.job_id] = time.time()
|
||||
Notify.objects.create(
|
||||
title=f'{obj.name} - 达到调度实例上限',
|
||||
source='schedule',
|
||||
content='一般为上个周期的执行任务还未结束,请增加调度间隔或减少任务执行耗时',
|
||||
type='1',
|
||||
)
|
||||
Notify.make_notify('monitor', '1', f'{obj.name} - 达到调度实例上限', '一般为上个周期的执行任务还未结束,请增加调度间隔或减少任务执行耗时')
|
||||
elif event.code == events.EVENT_JOB_ERROR:
|
||||
logger.info(f'EVENT_JOB_ERROR: job_id {event.job_id} exception: {event.exception}')
|
||||
if time.time() - counter.get(event.job_id, 0) > 3600:
|
||||
counter[event.job_id] = time.time()
|
||||
Notify.objects.create(
|
||||
title=f'{obj.name} - 执行异常',
|
||||
source='schedule',
|
||||
content=f'{event.exception}',
|
||||
type='1',
|
||||
)
|
||||
Notify.make_notify('monitor', '1', f'{obj.name} - 执行异常', f'{event.exception}')
|
||||
elif event.code == events.EVENT_JOB_EXECUTED:
|
||||
if event.retval:
|
||||
score = 0
|
||||
|
@ -79,12 +60,7 @@ class Scheduler:
|
|||
)
|
||||
if score != 0 and time.time() - counter.get(event.job_id, 0) > 3600:
|
||||
counter[event.job_id] = time.time()
|
||||
Notify.objects.create(
|
||||
title=f'{obj.name} - 执行失败',
|
||||
source='schedule',
|
||||
content='请在任务计划中查看失败详情',
|
||||
type='1',
|
||||
)
|
||||
Notify.make_notify('schedule', '1', f'{obj.name} - 执行失败', '请在任务计划中查看失败详情')
|
||||
|
||||
def _init_builtin_jobs(self):
|
||||
self.scheduler.add_job(auto_clean_records, 'cron', hour=0, minute=0)
|
||||
|
|
Loading…
Reference in New Issue