#!/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 import n4d.responses import n4d.server.core as n4dCore from n4d.utils import n4d_mv class SchedulerServer(): def __init__(self): self.dbg=True 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 self.n4dCore=n4dCore.Core.get_core() #def __init__ def _debug(self,msg): if (self.dbg): print("Scheduler: %s" %msg) #def _debug def read_config(self): status=False 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()) return n4d.responses.build_successful_call_response(data) except Exception as e: data=e status=False self._debug(("unable to open %s") % self.conf_file) return n4d.responses.build_successful_call_response("") #return ({'status':status,'data':data}) #def read_config def write_config(self,data): (task,key,value)=data 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 n4d.responses.build_successful_call_response(data) #return ({'status':status,'data':data}) #def write_config def get_tasks(self,*args): #return(self._read_wrkfiles(self.tasks_dir)) data=(self._read_wrkfiles(self.tasks_dir)) return n4d.responses.build_successful_call_response(data) #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 self.n4dCore.set_variable('SCHEDULED_TASKS',val) self._debug("New value is %s"%val) #def _register_cron_update