#!/usr/bin/env python ###### #Scheduler library for Lliurex #Add to N4D the scheduled tasks #This class reads the json file with the scheduled tasks and #distributes the info among the clients # -*- coding: utf-8 -*- import os import json from datetime import date class SchedulerServer(): def __init__(self): self.dbg=False self.tasks_dir="/etc/scheduler/tasks.d" self.available_tasks_dir="/etc/scheduler/conf.d/tasks" self.conf_dir="/etc/scheduler/conf.d/" self.conf_file="%s/scheduler.conf"%self.conf_dir #def __init__ def _debug(self,msg): if (self.dbg): print("Scheduler: %s" %msg) #def _debug def read_config(self): status=True data={} if not os.path.isdir(self.conf_dir): try: os.makedirs(self.conf_dir) except Exception as e: status=False data=e self._debug("Couldn't create conf dir %s"%self.conf_dir) if os.path.isfile(self.conf_file): try: data=json.loads(open(self.conf_file).read()) except Exception as e: data=e status=False self._debug(("unable to open %s") % self.conf_file) return ({'status':status,'data':data}) #def read_config def write_config(self,task,key,value): status=True data={} config={} if os.path.isfile(self.conf_file): try: config=json.loads(open(self.conf_file).read()) except Exception as e: data=e status=False self._debug(("unable to open %s") % self.conf_file) if task in config.keys(): config[task].update({key:value}) else: config[task]={key:value} try: with open(self.conf_file,'w') as f: json.dump(config,f,indent=4) except Exception as e: data=e self._debug(("unable to write %s") % self.conf_file) status=False return ({'status':status,'data':data}) #def write_config def get_tasks(self,*args): return(self._read_wrkfiles(self.tasks_dir)) #def get_tasks def get_local_tasks(self,*args): today=date.today() local_tasks={} tasks_data=self._read_wrkfiles(self.tasks_dir)['data'].copy() status=False for task_name,serial_data in tasks_data.items(): sw_continue=False for serial,data in serial_data.items(): sw_pass=False if 'autoremove' in data.keys(): if (data['mon'].isdigit()): mon=int(data['mon']) if mon=1000: val=0 val+=1 objects["VariablesManager"].set_variable("SCHEDULED_TASKS",val) self._debug("New value is %s"%val) #def _register_cron_update