def enable(self): plugin_manager = component.get("PluginManager") config = plugin_manager.config try: self.status_file = config[_plugin_name]["status_file"] except: self.status_file = Plugin.DEFAULT_STATUS_FILE log.info(("Didn't set `status_file` in plugin.ini in `%s` section" ", use default one: %s"), _plugin_name, self.status_file) self.enabled = True status_dir = os.path.dirname(self.status_file) if not os.path.exists(status_dir): try: os.makedirs(status_dir) except: self.enabled = False log.warning("Create directory failed: %s", status_dir) event_manager = component.get("EventManager") event_manager.register_event_handler("TaskEnqueueEvent", self.__on_task_enqueue) event_manager.register_event_handler("TaskStartEvent", self.__on_task_start) event_manager.register_event_handler("TaskStopEvent", self.__on_task_stop)
def enable(self): event_manager = component.get("EventManager") event_manager.register_event_handler("MirrorStartEvent", self.__on_mirror_start) event_manager.register_event_handler("RunSystemTaskEvent", self.__run_log_cleaner) self.task = logcleantask.LogCleanTask()
def __set_task_status(self, taskname, status, overwrite = True): scheduler = component.get("Scheduler") task = scheduler.tasks.get(taskname) if task.isinternal: return try: fp = open(self.status_file, "r+" if os.path.exists(self.status_file) else "w+") except: log.warning("Open file failed: %s", self.status_file) return task_status = fp.read().rstrip("\r\n") if task_status: task_status = json.loads(task_status) else: task_status = {} if overwrite: task_status[taskname] = status else: if taskname in task_status: task_status[taskname]["schedule"] = status["schedule"] else: status["status"] = self.STATUS_INITIAL task_status[taskname] = status if len(task_status) > 1: task_status = odict(sorted(task_status.iteritems())) fp.seek(0) try: fp.write(json.dumps(task_status)) except Exception, e: log.exception(e) fp.truncate(0)
def run_task(self, taskinfo, stage=1): if taskinfo.name not in self.tasks: return task = self.tasks[taskinfo.name] # for tasks that is still running when next schedule time # is reached (but has no timeout set), we just need to # reappend it. if task.running and task.timeout <= 0: taskinfo.time = task.get_schedule_time(since=time.time()) self.reappend_task(task, taskinfo) if task.running and (not task.twostage): log.info("Task: %s is still running and no timeout set, skipped", taskinfo.name) return event_manager = component.get("EventManager") event_manager.emit(mirror.event.PreTaskStartEvent(taskinfo.name)) task.run(stage) if taskinfo in self.queue: self.queue.remove(taskinfo) log.info("Task: %s begin to run with pid %d", taskinfo.name, task.pid) event_manager.emit(mirror.event.TaskStartEvent(taskinfo.name, task.pid)) if task.timeout <= 0: self.append_task(taskinfo.name, task, time.time()) else: self.append_timeout_task(taskinfo.name, task, task.start_time + task.timeout)
def run_task(self, taskinfo, stage = 1): if taskinfo.name not in self.tasks: return task = self.tasks[taskinfo.name] # for tasks that is still running when next schedule time # is reached (but has no timeout set), we just need to # reappend it. if task.running and task.timeout <= 0: taskinfo.time = task.get_schedule_time(since = time.time()) self.reappend_task(task, taskinfo) if task.running and ( not task.twostage ): log.info("Task: %s is still running and no timeout set, skipped", taskinfo.name) return event_manager = component.get("EventManager") event_manager.emit(mirror.event.PreTaskStartEvent(taskinfo.name)) task.run(stage) if taskinfo in self.queue: self.queue.remove(taskinfo) log.info("Task: %s begin to run with pid %d", taskinfo.name, task.pid) event_manager.emit(mirror.event.TaskStartEvent(taskinfo.name, task.pid)) if task.timeout <= 0: self.append_task(taskinfo.name, task, time.time()) else: self.append_timeout_task(taskinfo.name, task, task.start_time + task.timeout)
def __on_task_stop(self, taskname, pid, exitcode): if not self.enabled: return scheduler = component.get("Scheduler") task = scheduler.tasks.get(taskname, None) if not task: return status = { "status": self.STATUS_FINISHED } if task.cmdname == "rsync": status["message"] = _rsync_error[exitcode] else: status["message"] = "Task finished" status["exitcode"] = exitcode status["date"] = time.strftime(self.DATE_FORMAT) taskinfo = scheduler.queue.find(taskname) if taskinfo: status["schedule"] = time.strftime(self.DATE_FORMAT, time.localtime(taskinfo.time)) else: status["schedule"] = "Unknown" self.__set_task_status(taskname, status)
def __on_task_stop(self, taskname, pid, exitcode): if not self.enabled: return scheduler = component.get("Scheduler") task = scheduler.tasks.get(taskname, None) if not task: return status = {"status": self.STATUS_FINISHED} if task.cmdname == "rsync": status["message"] = _rsync_error[exitcode] else: status["message"] = "Task finished" status["exitcode"] = exitcode status["date"] = time.strftime(self.DATE_FORMAT) taskinfo = scheduler.queue.find(taskname) if taskinfo: status["schedule"] = time.strftime(self.DATE_FORMAT, time.localtime(taskinfo.time)) else: status["schedule"] = "Unknown" self.__set_task_status(taskname, status)
def start(self): event_manager = component.get("EventManager") event_manager.emit(mirror.event.MirrorStartEvent()) while (True): self.sleep() if not self.roused_by_child: log.info("I am waking up...") self.schedule()
def __set_task_status(self, taskname, status, overwrite=True): scheduler = component.get("Scheduler") task = scheduler.tasks.get(taskname) # We do not export internal task's status if task.isinternal: return # Add info about upstream if task.__class__.__name__ == "Task": status['upstream'] = task.upstream[0] + '::' + task.rsyncdir + '/' # Read old status file content try: fp = open(self.status_file, "r+" if os.path.exists(self.status_file) else "w+") except: log.warning("Open file failed: %s", self.status_file) return task_status = fp.read().rstrip("\r\n") if task_status: try: task_status = json.loads(task_status) except Exception as e: log.warning("Parse json file(%s) failed: %s", self.status_file, e) task_status = {} else: if self.auto_remove: # Remove tasks that already been removed from config file list( map( lambda taskname: task_status.pop(taskname), filter( lambda taskname: taskname not in scheduler. config, list(task_status)))) else: task_status = {} if overwrite: task_status[taskname] = status else: if taskname in task_status: task_status[taskname]["schedule"] = status["schedule"] else: status["status"] = self.STATUS_INITIAL task_status[taskname] = status if len(task_status) > 1: task_status = odict(sorted(task_status.items())) fp.seek(0) try: fp.write(json.dumps(task_status, indent=2)) except Exception as e: log.exception(e) fp.truncate(0) else: fp.truncate() fp.close()
def run_system_task(self, taskinfo): event_manager = component.get("EventManager") event_manager.emit(mirror.event.RunSystemTaskEvent(taskinfo)) # after we run the system task, we need to update the queue # or else the sleeptime will be invalid if taskinfo in self.queue: self.queue.remove(taskinfo) task = self.tasks[taskinfo.name] self.append_task(taskinfo.name, task, time.time())
def __on_task_enqueue(self, taskname): if not self.enabled: return scheduler = component.get("Scheduler") taskinfo = scheduler.queue.find(taskname) status = { "schedule": time.strftime(self.DATE_FORMAT, time.localtime(taskinfo.time)) } self.__set_task_status(taskname, status, overwrite = False)
def sigchld_handler(signo, frame): try: pid, status = os.waitpid(-1, os.WNOHANG) except OSError as e: log.error("Error occured when waitpid(), %s.", e) return scheduler = component.get("Scheduler") if scheduler is None: return scheduler.stop_task_with_pid(pid, status)
def __on_mirror_start(self): self.scheduler = component.get("Scheduler") self.scheduler.tasks[_plugin_name] = self.task self.scheduler.active_tasks += 1 # NOTE: However currently SystemTask is run in plugin thread, # and the Scheduler sleeps before SystemTask is appended into # the queue, so SystemTask will only begin to take effect on # next sleep loop. # However for most system tasks, this is acceptable. log.info("Task: %s added", _plugin_name)
def enable(self): event_manager = component.get("EventManager") event_manager.register_event_handler("MirrorStartEvent", self.__on_mirror_start) event_manager.register_event_handler("RunSystemTaskEvent", self.__run_log_cleaner) self.logclean_task = logcleantask.LogCleanTask() event_manager.register_event_handler("RunSystemTaskEvent", self.__run_task_cleaner) self.taskclean_task = taskcleantask.TaskCleanTask()
def reappend_task(self, task, taskinfo): """ Remove a taskinfo from queue and put it in again, to keep the queue in order. """ if taskinfo not in self.queue: return self.queue.remove(taskinfo) self.queue.put(taskinfo) event_manager = component.get("EventManager") event_manager.emit(mirror.event.TaskEnqueueEvent(taskinfo.name))
def __set_task_status(self, taskname, status, overwrite = True): scheduler = component.get("Scheduler") task = scheduler.tasks.get(taskname) # We do not export internal task's status if task.isinternal: return # Add info about upstream if task.__class__.__name__ == "Task": status['upstream'] = task.upstream[0] + '::' + task.rsyncdir + '/' # Read old status file content try: fp = open(self.status_file, "r+" if os.path.exists(self.status_file) else "w+") except: log.warning("Open file failed: %s", self.status_file) return task_status = fp.read().rstrip("\r\n") if task_status: try: task_status = json.loads(task_status) except Exception as e: log.warning("Parse json file(%s) failed: %s", self.status_file, e) task_status = {} else: # Remove tasks that already been removed from config file for taskname in list(task_status): if taskname not in scheduler.config: task_status.pop(taskname) else: task_status = {} if overwrite: task_status[taskname] = status else: if taskname in task_status: task_status[taskname]["schedule"] = status["schedule"] else: status["status"] = self.STATUS_INITIAL task_status[taskname] = status if len(task_status) > 1: task_status = odict(sorted(task_status.items())) fp.seek(0) try: fp.write(json.dumps(task_status, indent = 2)) except Exception as e: log.exception(e) fp.truncate(0) else: fp.truncate() fp.close()
def shutdown_handler(signo, frame): import mirror.configmanager pidfile = mirror.configmanager.get_config_dir("mirrord.pid") if os.path.isfile(pidfile): os.remove(pidfile) log.info("Got signal %s, exiting...", signals[signo]) import sys scheduler = component.get("Scheduler") if scheduler is None: sys.exit(0) # We will waitpid() in stop_all_tasks(), # so unregister sigchld_handler here. signal.signal(signal.SIGCHLD, signal.SIG_DFL) scheduler.stop_all_tasks() if hasattr(scheduler, "buspid"): os.kill(scheduler.buspid, signal.SIGTERM) pid, status = os.waitpid(scheduler.buspid, 0) log.info("Killed mirror dbus with pid: %d", pid) event_manager = component.get("EventManager") while event_manager.plugin_thread and len(event_manager.plugin_thread.event_queue) > 0: time.sleep(0.1) component.deregister(event_manager) component.deregister(component.get("PluginManager")) # Deregister the scheduler, # this will call scheduler's stop(). # But scheduler's start() is called # directly, not by component's start(). component.deregister(scheduler) log.info("Bye bye... :)") sys.exit(0)
def shutdown_handler(signo, frame): import mirror.configmanager pidfile = mirror.configmanager.get_config_dir("mirrord.pid") if os.path.isfile(pidfile): os.remove(pidfile) log.info("Got signal %s, exiting...", signals[signo]) import sys scheduler = component.get("Scheduler") if scheduler is None: sys.exit(0) # We will waitpid() in stop_all_tasks(), # so unregister sigchld_handler here. signal.signal(signal.SIGCHLD, signal.SIG_DFL) scheduler.stop_all_tasks() if hasattr(scheduler, "buspid"): os.kill(scheduler.buspid, signal.SIGTERM) pid, status = os.waitpid(scheduler.buspid, 0) log.info("Killed mirror dbus with pid: %d", pid) event_manager = component.get("EventManager") while (event_manager.plugin_thread and len(event_manager.plugin_thread.event_queue) > 0): time.sleep(0.1) component.deregister(event_manager) component.deregister(component.get("PluginManager")) # Deregister the scheduler, # this will call scheduler's stop(). # But scheduler's start() is called # directly, not by component's start(). component.deregister(scheduler) log.info("Bye bye... :)") sys.exit(0)
def append_timeout_task(self, taskname, task, time): """ A timeout checking task is added after a task begins to run. """ if not task.running: return if not task.enabled: return taskinfo = TaskInfo(taskname, TIMEOUT_TASK, time, task.priority) if taskinfo in self.queue: return self.queue.put(taskinfo) event_manager = component.get("EventManager") event_manager.emit(mirror.event.TaskEnqueueEvent(taskname))
def run(self): scheduler = component.get("Scheduler") logdir = scheduler.logdir now = datetime.now() paths = glob.glob(os.path.join(logdir, '*')) for path in paths: try: datestr = path.split('.')[-1] date = time.strptime(datestr, "%Y-%m-%d") delta = now - datetime.fromtimestamp(time.mktime(date)) if delta.days > 10: os.unlink(path) except Exception, e: log.exception(e) else: log.info("Deleted log file: %s", path)
def task_finished(self, task): """ Check whether a task needs post process, e.g. two stage tasks. """ event_manager = component.get("EventManager") if not task.twostage: event_manager.emit(mirror.event.TaskStopEvent(task.name, task.pid, task.code)) task.set_stop_flag() return if task.stage == 1: log.info("Task: %s scheduled to second stage", task.name) self.run_task(TaskInfo(task.name, REGULAR_TASK, 0, task.priority), stage = 2) else: event_manager.emit(mirror.event.TaskStopEvent(task.name, task.pid, task.code)) task.set_stop_flag() task.stage = 1
def task_post_process(self, task): """ Check whether a task needs post process, e.g. two stage tasks. """ event_manager = component.get("EventManager") if not task.twostage: event_manager.emit(mirror.event.TaskStopEvent(task.name, task.pid, task.code)) task.set_stop_flag() return if task.stage == 1: log.info("Task: %s scheduled to second stage", task.name) self.run_task(TaskInfo(task.name, REGULAR_TASK, 0, task.priority), stage = 2) else: event_manager.emit(mirror.event.TaskStopEvent(task.name, task.pid, task.code)) task.set_stop_flag() task.stage = 1
def __on_mirror_start(self): """ NOTE: However currently SystemTask is run in plugin thread, and the Scheduler sleeps before SystemTask is appended into the queue, so SystemTask will only begin to take effect on next sleep loop. However for most system tasks, this is acceptable. """ self.scheduler = component.get("Scheduler") self.scheduler.tasks[logcleantask._name] = self.logclean_task self.scheduler.active_tasks += 1 log.info("Task: %s added", logcleantask._name) self.scheduler.tasks[taskcleantask._name] = self.taskclean_task self.scheduler.active_tasks += 1 log.info("Task: %s added", taskcleantask._name)
def run(self): scheduler = component.get("Scheduler") curtime = time.time() for taskname, task in scheduler.tasks.items(): try: # internal tasks have no `running` if not hasattr(task, "running"): continue if not task.running: continue # SIGCHLD will trigger in main thread if curtime - task.start_time > self.TASK_TIMEOUT: os.kill(task.pid, signal.SIGTERM) log.info("Killed task: %s, whose life exceeds %d days", taskname, self.timeout_days) except Exception as e: log.exception(e)
def sigchld_handler(signo, frame): try: pid, status = os.waitpid(-1, os.WNOHANG) except OSError as e: log.error("Error occured when waitpid(), %s.", e) return scheduler = component.get("Scheduler") if scheduler is None: return scheduler.stop_task_with_pid(pid, status) # In Python 3, execution of signal handler will not terminate the sleep() as Python 2 # But it will still be terminated if signal handler raises an exception # For convenience, we raise en exception manually # https://mozillazg.com/2017/07/python-time-sleep-terminate-by-signal.html if mirror.common.is_python3(): raise mirror.error.MirrordTaskFinishedFakeError("Task finished, please stop sleep")
def run(self): scheduler = component.get("Scheduler") logdir = scheduler.logdir now = datetime.now() paths = glob.glob(os.path.join(logdir, '*')) for path in paths: try: datestr = path.split('.')[-1] date = time.strptime(datestr, "%Y-%m-%d") delta = now - datetime.fromtimestamp(time.mktime(date)) if delta.days > 10: os.unlink(path) log.info("Deleted log file: %s", path) # If the log was not generated by mirror except ValueError: continue except Exception as e: log.exception(e)
def append_task(self, taskname, task, since): """ In some cases a task with same name may be ignored if there is a running one, but this is a feature, not a bug... """ if task.running and task.timeout > 0: return if not task.enabled: return taskinfo = TaskInfo(taskname, (SYSTEM_TASK if task.isinternal else REGULAR_TASK), task.get_schedule_time(since), task.priority) # however this is hard to understand, # for system tasks and timeout tasks if taskinfo in self.queue: return self.queue.put(taskinfo) event_manager = component.get("EventManager") event_manager.emit(mirror.event.TaskEnqueueEvent(taskname))
def append_task(self, taskname, task, since): """ In some cases a task with same name may be ignored if there is a running one, but this is a feature, not a bug... """ if task.running and task.timeout > 0: return if not task.enabled: return taskinfo = TaskInfo(taskname, (SYSTEM_TASK if task.isinternal else REGULAR_TASK), task.get_schedule_time(since), task.priority) # for system tasks and tasks-without-timeout-set, task is appended again # in run_*_task() method, for tasks-with-timeout-set, a timeout task # with same name is appended in run_task() if taskinfo in self.queue: return self.queue.put(taskinfo) event_manager = component.get("EventManager") event_manager.emit(mirror.event.TaskEnqueueEvent(taskname))
def __set_task_status(self, taskname, status, overwrite=True): scheduler = component.get("Scheduler") task = scheduler.tasks.get(taskname) # We do not export internal task's status if task.isinternal: return # Add info about upstream if task.__class__.__name__ == "Task": status['upstream'] = task.upstream[0] + '::' + task.rsyncdir + '/' try: fp = open(self.status_file, "r+" if os.path.exists(self.status_file) else "w+") except: log.warning("Open file failed: %s", self.status_file) return task_status = fp.read().rstrip("\r\n") if task_status: task_status = json.loads(task_status) else: task_status = {} if overwrite: task_status[taskname] = status else: if taskname in task_status: task_status[taskname]["schedule"] = status["schedule"] else: status["status"] = self.STATUS_INITIAL task_status[taskname] = status if len(task_status) > 1: task_status = odict(sorted(task_status.iteritems())) fp.seek(0) try: fp.write(json.dumps(task_status)) except Exception as e: log.exception(e) fp.truncate(0) else: fp.truncate() fp.close()
def stop_all_tasks(self, signo = signal.SIGTERM): """ This method can only be called when mirrord is shut down by SIGTERM or SIGINT. NOTE: Currently when mirrord is shut down, all running tasks will also be killed. """ event_manager = component.get("EventManager") for taskname, task in self.tasks.items(): if task.isinternal: continue if not task.running: continue pid = task.pid task.stop(signo) # Not sure it is ok... pid, status = os.waitpid(pid, 0) endstr, code = self.parse_return_status(status) task.code = code event_manager.emit(mirror.event.TaskStopEvent(task.name, task.pid, task.code)) log.info("Killed task: %s with pid %d", taskname, pid)
def stop_all_tasks(self, signo = signal.SIGTERM): """ This method can only be called when mirrord is shut down by SIGTERM or SIGINT. NOTE: Currently when mirrord is shut down, all running tasks will also be killed. """ event_manager = component.get("EventManager") for taskname, task in self.tasks.iteritems(): if task.isinternal: continue if not task.running: continue pid = task.pid task.stop(signo) # Not sure it is ok... pid, status = os.waitpid(pid, 0) endstr, code = self.parse_return_status(status) task.code = code event_manager.emit(mirror.event.TaskStopEvent(task.name, task.pid, task.code)) log.info("Killed task: %s with pid %d", taskname, pid)
def enable(self): event_manager = component.get("EventManager") event_manager.register_event_handler("MirrorStartEvent", self.__on_mirror_start_event) log.info(("I am a slate fish living in the upstream river" " of Qiandao Lake."))
def reload_handler(signo, frame): log.info("Got signal %s, start reloading...", signals[signo]) scheduler = component.get("Scheduler") scheduler.reload_config()
os.kill(scheduler.buspid, signal.SIGTERM) pid, status = os.waitpid(scheduler.buspid, 0) log.info("Killed mirror dbus with pid: %d", pid) event_manager = component.get("EventManager") while (event_manager.plugin_thread and len(event_manager.plugin_thread.event_queue) > 0): time.sleep(0.1) component.deregister(event_manager) component.deregister(component.get("PluginManager")) # Deregister the scheduler, # this will call scheduler's stop(). # But scheduler's start() is called # directly, not by component's start(). component.deregister(scheduler) log.info("Bye bye... :)") sys.exit(0) def sigchld_handler(signo, frame): try: pid, status = os.waitpid(-1, os.WNOHANG) except OSError,e: log.error("Error occured when waitpid(), %s.", e) return scheduler = component.get("Scheduler") if scheduler is None: return scheduler.stop_task_with_pid(pid, status)
def __init__(self): threading.Thread.__init__(self, name="mirror.plugin") # python list is thread safe self.event_queue = [] self.event_manager = component.get("EventManager") self.stop_event = threading.Event()