def handle_request(self): """Called when a request is found.""" # Clear request immediately. request = self.request self.request = None self.save() self.log.info("Request received: %s" % Request.name(request)) if request == Request.PAUSE: self.set_status(Status.PAUSED) elif request == Request.RESUME: if self.status != Status.PAUSED: self.log.info("Must be paused to resume; clearing request.") else: self.set_status(Status.RUNNING) elif request == Request.STOP: self.set_status(Status.ENDED) elif request == Request.KILL: self.set_status(Status.KILLED) elif request == Request.RELOAD: changed = MultiQuerySet(Schedule, CronSchedule) changed = changed.objects.unfinished.filter( changed=True, scheduler=self) for item in self.timer.tasks: s = item[2][0] if s in changed: self.log.info("Removing outdated: %s" % s) self.timer.tasks.remove(item) self.set.remove(s) s = type(s).objects.get(pk=s.pk) for s in changed: self.log.info("Adding updated: %s" % s) self.add(s) changed.update(changed=False)
def handle_request(self): """Called when a request is found.""" # Clear request immediately. request = self.request self.request = None self.save() self.log.info("Request received: %s" % Request.name(request)) if request == Request.PAUSE: self.set_status(Status.PAUSED) elif request == Request.RESUME: if self.status != Status.PAUSED: self.log.info("Must be paused to resume; clearing request.") else: self.set_status(Status.RUNNING) elif request == Request.STOP: self.set_status(Status.ENDED) elif request == Request.KILL: self.set_status(Status.KILLED) elif request == Request.RELOAD: changed = MultiQuerySet(Schedule, CronSchedule) changed = changed.objects.unfinished.filter(changed=True, scheduler=self) for item in self.timer.tasks: s = item[2][0] if s in changed: self.log.info("Removing outdated: %s" % s) self.timer.tasks.remove(item) self.set.remove(s) s = type(s).objects.get(pk=s.pk) for s in changed: self.log.info("Adding updated: %s" % s) self.add(s) changed.update(changed=False)
def instances(self): """A custom implementation of the Django related manager pattern.""" return MultiQuerySet( *[i.objects.filter(executor=self.pk) for i in INSTANCE_MODELS])
def main(): usage = "norc_control [executor | scheduler | host] <id | host> " + \ "--[stop | kill | pause | resume | reload | handle] [--wait]" def bad_args(message): print message print usage sys.exit(2) parser = OptionParser(usage) parser.add_option("-s", "--stop", action="store_true", default=False, help="Send a stop request.") parser.add_option("-k", "--kill", action="store_true", default=False, help="Send a kill request.") parser.add_option("-p", "--pause", action="store_true", default=False, help="Send a pause request.") parser.add_option("-u", "--resume", action="store_true", default=False, help="Send an resume request.") parser.add_option("-r", "--reload", action="store_true", default=False, help="Send an reload request to a Scheduler.") parser.add_option("--handle", action="store_true", default=False, help="Change the object's status to HANDLED.") parser.add_option("-f", "--force", action="store_true", default=False, help="Force the request to be made..") parser.add_option("-w", "--wait", action="store_true", default=False, help="Wait until the request has been responded to.") options, args = parser.parse_args() if len(args) != 2: bad_args("Invalid number of arguments.") requests = filter(lambda a: getattr(options, a.lower()), Request.NAMES.values()) if len(requests) + (1 if options.handle else 0) != 1: bad_args("Must request exactly one action.") if not options.handle: request = requests[0] req = getattr(Request, request) cls = None if args[0] in EXECUTOR_KEYWORDS: cls = Executor elif args[0] in SCHEDULER_KEYWORDS: cls = Scheduler elif args[0] in HOST_KEYWORDS: if options.handle: bad_args("Can't perform handle operation on multiple daemons.") daemons = MultiQuerySet(Executor, Scheduler).objects.all() daemons = daemons.filter(host=args[1]).status_in("active") if not options.force: daemons = daemons.filter(request=None) for d in daemons: if req in d.VALID_REQUESTS: d.make_request(req) print "%s was sent a %s request." % (d, request) if options.wait: _wait(daemons, req) else: bad_args("Invalid keyword '%s'." % args[0]) if cls: name = cls.__name__ try: obj_id = int(args[1]) except ValueError: bad_args("Invalid id '%s'; must be an integer." % args[1]) try: d = cls.objects.get(id=obj_id) except cls.DoesNotExist: print "Could not find a(n) %s with id=%s" % (name, obj_id) else: if options.handle: if controls.handle(d): print "The error state of %s was marked as handled." % d else: print "%s isn't in an error state." % d elif Status.is_final(d.status) and not options.force: print "%s is already in a final state." % d elif d.request == None or options.force: d.make_request(req) print "%s was sent a %s request." % (d, request) if options.wait: _wait([d], req) else: print "%s already has request %s." % \ (d, Request.name(d.request))