Skip to content

Commit

Permalink
U 优化服务进程首次启动
Browse files Browse the repository at this point in the history
  • Loading branch information
vapao committed Mar 12, 2022
1 parent f421729 commit 5bd1f48
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
26 changes: 15 additions & 11 deletions spug_api/apps/monitor/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from django_redis import get_redis_connection
from django.conf import settings
from django.db import connections
from django.db.utils import DatabaseError
from apps.monitor.models import Detection
from libs import AttrDict, human_datetime
from datetime import datetime, timedelta
Expand All @@ -32,17 +33,20 @@ def _dispatch(self, task_id, tp, targets, extra, threshold, quiet):

def _init(self):
self.scheduler.start()
for item in Detection.objects.filter(is_active=True):
now = datetime.now()
trigger = IntervalTrigger(minutes=int(item.rate), timezone=self.timezone)
self.scheduler.add_job(
self._dispatch,
trigger,
id=str(item.id),
args=(item.id, item.type, item.targets, item.extra, item.threshold, item.quiet),
next_run_time=now + timedelta(seconds=randint(0, 60))
)
connections.close_all()
try:
for item in Detection.objects.filter(is_active=True):
now = datetime.now()
trigger = IntervalTrigger(minutes=int(item.rate), timezone=self.timezone)
self.scheduler.add_job(
self._dispatch,
trigger,
id=str(item.id),
args=(item.id, item.type, item.targets, item.extra, item.threshold, item.quiet),
next_run_time=now + timedelta(seconds=randint(0, 60))
)
connections.close_all()
except DatabaseError:
pass

def run(self):
rds_cli = get_redis_connection()
Expand Down
22 changes: 13 additions & 9 deletions spug_api/apps/schedule/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from apscheduler.triggers.cron import CronTrigger
from django_redis import get_redis_connection
from django.db import connections
from django.db.utils import DatabaseError
from apps.schedule.models import Task, History
from apps.schedule.builtin import auto_run_by_day, auto_run_by_minute
from django.conf import settings
Expand Down Expand Up @@ -78,15 +79,18 @@ def _dispatch(self, task_id, command, targets):
def _init(self):
self.scheduler.start()
self._init_builtin_jobs()
for task in Task.objects.filter(is_active=True):
trigger = self.parse_trigger(task.trigger, task.trigger_args)
self.scheduler.add_job(
self._dispatch,
trigger,
id=str(task.id),
args=(task.id, task.command, json.loads(task.targets)),
)
connections.close_all()
try:
for task in Task.objects.filter(is_active=True):
trigger = self.parse_trigger(task.trigger, task.trigger_args)
self.scheduler.add_job(
self._dispatch,
trigger,
id=str(task.id),
args=(task.id, task.command, json.loads(task.targets)),
)
connections.close_all()
except DatabaseError:
pass

def run(self):
rds_cli = get_redis_connection()
Expand Down

0 comments on commit 5bd1f48

Please sign in to comment.