当前位置:   article > 正文

Django通过Json配置文件分配多个定时任务

Django通过Json配置文件分配多个定时任务
	def load_config():
	    with open("rule.json", 'rb')as f:
	        config = json.load(f)
	    return config
	def job(task_name, config, time_interval):
	# 	...	通过task_name判断进行操作
		if task_name == 'get_data_times':
			pass

	def main():
	    config = load_config()
	
	    for task_name, task_value in config.items():
	        # 循环配置文件中配置的时间 启动定时任务			多少秒运行一次
	        if task_name.startswith('get_data') or task_name.startswith('get_internet'):
	            time_interval = int(task_value.replace('s', ''))
	            schedule.every(time_interval).seconds.do(job, task_name, config, time_interval)
	        elif task_name.endswith('_time'):
	        	# 多少分钟运行一次
	            time_interval = int(task_value.replace('min', ''))
	            time_intervals = time_interval + 1
	            schedule.every(time_interval).minutes.do(job, task_name, config, time_intervals)
			# 可以设置多个 时分秒 周月星期一到星期天 at
	    def run_jobs():
	        while True:
	            schedule.run_pending()
	            time.sleep(1)
	
	    thread = threading.Thread(target=run_jobs)
	    thread.start()
	
	if __name__ == '__main__':
	    import django, os
	    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "QsManagement.settings")
	    django.setup()
	    from app01.logger import logger
	    from app01.models import ComputerStatus, AlarmInfo, InterNetSpeed, WindowsError, ProductionLine, Machine, MachineType
	    from app01.methods import get_ip
	    main()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

在这里插入图片描述

json配置文件 通过配置文件设置阈值,到达指定阈值报警

	{
	  "get_data_times": "3min",
	  "get_internet_times": "3min",
	  "get_windows_error_time": "3min",
	
	  "cpu_time" : "1min",
	  "cpu_rate" : "90%",
	
	  "memory_time" : "1min",
	  "memory_rate" : "75%",
	
	  "gpu_time" : "1min",
	  "gpu_rate" : "50%",
	
	  "disk_times": "3min",
	  "C_disk_space": "50G",
	  "D_disk_space": "30G",
	  "E_disk_space": "50G",
	  "F_disk_space": "50G",
	  "G_disk_space": "50G",
	
	  "cpu_temperature":  "50度",
	  "temperature_time": "1min",
	
	  "internet_speed": "200ms",
	  "internet_time" : "1min"
	}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/blog/article/detail/80610
推荐阅读
相关标签
  

闽ICP备14008679号