#!/usr/bin/env python ### # ### # -*- coding: utf-8 -*- import os,socket import threading import time from datetime import date import psutil import xmlrpclib as xmlrpc class SchedulerClient(): def __init__(self): self.cron_dir='/etc/cron.d' self.task_prefix='remote-' #Temp workaround->Must be declared on a n4d var self.cron_dir='/etc/cron.d' self.count=0 self.dbg=0 self.holidays_shell="/usr/bin/check_holidays.py" def startup(self,options): t=threading.Thread(target=self._main_thread) t.daemon=True t.start() def _debug(self,msg): if self.dbg: print("%s"%msg) def _main_thread(self): objects["VariablesManager"].register_trigger("SCHEDULED_TASKS","SchedulerClient",self.process_tasks) tries=10 for x in range (0,tries): self.scheduler_var=objects["VariablesManager"].get_variable("SCHEDULED_TASKS") if self.scheduler_var!=self.count: self.count=self.scheduler_var self.process_tasks() break else: time.sleep(1) def process_tasks(self,data=None): self._debug("Scheduling tasks") today=date.today() prefixes={'remote':True,'local':False} tasks={} try: socket.gethostbyname('server') except: prefixes={'local':False} for prefix,sw_remote in prefixes.iteritems(): if prefix=='remote': n4d=xmlrpc.ServerProxy("https://server:9779") tasks=n4d.get_remote_tasks("","SchedulerServer")['data'].copy() else: n4d=xmlrpc.ServerProxy("https://localhost:9779") tasks=n4d.get_local_tasks("","SchedulerServer")['data'].copy() #Delete files for f in os.listdir(self.cron_dir): if f.startswith(prefix): os.remove(self.cron_dir+'/'+f) #Create the cron files for name in tasks.keys(): task_names={} self._debug("Processing task: %s"%name) for serial in tasks[name].keys(): self._debug("Item %s"%serial) sw_pass=False if 'autoremove' in tasks[name][serial]: if (tasks[name][serial]['mon'].isdigit()): mon=int(tasks[name][serial]['mon']) if mon