def aboutToExecute(self, job): """ its kinda signal from job to scheduler that job is going to execute. Scheduler checks here for blockers and may delay or stop execution if necessary """ jid = id(job) if jid not in self.blockers: return True for j in self.blockers[jid]: if j.isWorking(): if jid not in self.blockActions: from rcore import Core Core.instance().getRPCService("alarm").notify("Execution of %s was blocked by currently" " working %s and no handlers for this case were set. This" " usually means architectural design flaw." % \ (repr(job), repr(j)), ["error"]) return False if self.blockActions[jid]['action'] == self.QueueBlocked: log.msg("execution of %s queued" % repr(job)) def queueExecution(result, job): log.msg("executing queued job" % repr(job)) reactor.callLater(0, job._execute) return result j.currentDeferred.addBoth(queueExecution, job) if self.blockActions[jid]['action'] == self.RescheduleBlocked: raise Exception("RescheduleBlocked is not implemented") return False return True
def __call__(self, *params): if self._methodPath: try: print("XML-RPC %s:" % self.__class__.__name__, self._methodPath, params) proxy = self._getProxy() proxy.queryFactory.noisy = False wfd = waitForDeferred( proxy.callRemote(self._methodPath, *params)) wfd.setRestoreContext(self.restoreContext) yield wfd result = wfd.getResult() yield result return except Exception as e: errStr = str(e) if isinstance( e, Fault) else getFailureFor(e).getTraceback() msg = "Internal XML-RPC Failed: " + self.__class__.__name__ + ":" + self._methodPath + \ str(params) + "\n" + errStr log.msg(msg) self.__class__._lastError = msg if self._alertable: Core.instance().getRPCService("alarm").notify([ msg, "<div style='color:#ff0000;font-weight:bold'>%s</div>" % escape(self.__class__._lastError).replace( "\n", "<br/>") ], ["error"]) raise else: raise InternalError("method name is not given")
def checkTimeout(d): if d and not d.called: from rcore import Core Core.instance().getRPCService("alarm").notify( "Execution of %s was timed out. This usually mean " "that deferreds chain is corrupted or something just hangs and " "its definitelly a bad sign. notice if this a looped job next " "cycle will never be started" % repr(self), ["error"])
def invoke(self, defualtCallback, defaultErrback): from rcore import Core if Core.instance().debugEnabled(): print("ActionQueue: Executing job from queue: " + str(self.func)) self.active = True d = defer.maybeDeferred(self.func, *self.args, **self.kwargs) if self.defer: if Core.instance().debugEnabled(): print("ActionQueue: adding own callback") d.addCallbacks(self.defer.callback, self.defer.errback) else: if Core.instance().debugEnabled(): print("ActionQueue: adding default callback") d.addCallbacks(defualtCallback, defaultErrback) return d
def executeInExactContext(func, constructor, *args, **kw): """ Creates instance of passed context constructor and set it current before starting func. restores context before return. @param func: @param constructor: @param args: @param kw: @return: """ from rcore.core import Core if Core.instance().debugEnabled(): print "Context: execute in context: ", str(func) def deleteTempContext(result): deleteContext(contextId) return result currentId = _contextData["currentId"] contextId = makeContext(constructor) setCurrentContext(contextId) d = defer.maybeDeferred(func, *args, **kw) d.addBoth(deleteTempContext) setCurrentContext(currentId) return d
def roleChecker(*args, **kwargs): if not Core.instance().getUser( getContext().initiator).hasRole(roleType): raise AccessDenied( "User '%s' does not have %s access" % (str(getContext().initiator), roles[roleType])) return rpc_method(*args, **kwargs)
def executeInExactContext(func, constructor, *args, **kw): """ Creates instance of passed context constructor and set it current before starting func. restores context before return. @param func: @param constructor: @param args: @param kw: @return: """ from rcore.core import Core if Core.instance().debugEnabled(): print("Context: execute in context: " + str(func)) def deleteTempContext(result): deleteContext(contextId) return result currentId = _contextData["currentId"] contextId = makeContext(constructor) setCurrentContext(contextId) d = defer.maybeDeferred(func, *args, **kw) d.addBoth(deleteTempContext) setCurrentContext(currentId) return d
def __init__(self): self.contacts = [] cur = Core.instance().db.cursor() cur.execute("SELECT * FROM contacts") for row in cur: c = Contact.fromDict(row) print("Loaded contact:", str(c)) self.contacts.append(c)
def __init__(self): self.contacts = [] cur = Core.instance().db.cursor() cur.execute("SELECT * FROM contacts") for row in cur: c = Contact.fromDict(row) print ("Loaded contact:", str(c)) self.contacts.append(c)
def save(self): c = Core.instance().db.cursor() # if self.id: # c.execute("UPDATE OR REPLACE contacts(type, address, privilege, subscription) "\ # "VALUES(?, ?, ?, ?)", (self.type, self.address, self.privilege, self.subscription)) # else: c.execute("REPLACE INTO contacts(type, address, privilege, subscription) "\ "VALUES(?, ?, ?, ?)", (self.type, self.address, self.privilege, str(self.subscription))) c.close()
def append(self, func, *args, **kwargs): """ Enqueue some action `func`. Passed func will be executed with passed args and kwargs (no other args passed to func) You can get deferred from returned action and do with it what you want @param func: Callable @param args: list of args for callable @param kwargs: dict of kwargs for callable @return: ActionQueueItem """ from rcore import Core if Core.instance().debugEnabled(): print("ActionQueue: Adding job to queue: " + str(func)) item = ActionQueueItem(func, args, kwargs) list.append(self, item) if not self.checkPlanned: self.checkPlanned = True if Core.instance().debugEnabled(): print("ActionQueue: Planning check queue") reactor.callLater(0, self.checkQueue) return item
def finished(result, item): if Core.instance().debugEnabled(): print("ActionQueue: job finished: " + str(result)) self.active = False; reactor.callLater(0, self.checkQueue) if item.contextId: try: setCurrentContext(item.contextId) except Exception as e: print("ActionQueue: Unable to restore context. " "this should never happen and may damage other context and their data.") print("ActionQueue: Actual result was: " + str(result)) return ContextError(str(e)).toFailure()
def __call__(self, *params): if self._methodPath: try: print "XML-RPC %s:" % self.__class__.__name__, self._methodPath, params proxy = self._getProxy() proxy.queryFactory.noisy = False wfd = waitForDeferred(proxy.callRemote(self._methodPath, *params)) wfd.setRestoreContext(self.restoreContext) yield wfd result = wfd.getResult() yield result return except Exception as e: errStr = str(e) if isinstance(e, Fault) else getFailureFor(e).getTraceback() msg = ( "Internal XML-RPC Failed: " + self.__class__.__name__ + ":" + self._methodPath + str(params) + "\n" + errStr ) log.msg(msg) self.__class__._lastError = msg if self._alertable: Core.instance().getRPCService("alarm").notify( [ msg, "<div style='color:#ff0000;font-weight:bold'>%s</div>" % escape(self.__class__._lastError).replace("\n", "<br/>"), ], ["error"], ) raise else: raise InternalError("method name is not given")
def checkQueue(self): from rcore import Core def finished(result, item): if Core.instance().debugEnabled(): print("ActionQueue: job finished: " + str(result)) self.active = False; reactor.callLater(0, self.checkQueue) if item.contextId: try: setCurrentContext(item.contextId) except Exception as e: print("ActionQueue: Unable to restore context. " "this should never happen and may damage other context and their data.") print("ActionQueue: Actual result was: " + str(result)) return ContextError(str(e)).toFailure() #import gc #from rcore import Context #print "GARBAGE: ", gc.garbage, gc.get_referrers([a for a in gc.get_objects() if isinstance(a, Context)][-1]) #return result if Core.instance().debugEnabled(): print("ActionQueue: Checking queue: " + (str(len(self)) + " jobs" if len(self) else "empty")) if len(self): if not self.active: self.active = True item = self.pop(0) item.invoke(self._callback, self._errback).addBoth(finished, item) else: self.checkPlanned = False if self.finishWaiter: d = self.finishWaiter self.finishWaiter = None d.callback(None)
def deleteContext(id): from rcore.core import Core del _contextData["contexts"][id] if id == _contextData["currentId"]: _contextData["currentId"] = Core.instance().mainContextId
def roleChecker(*args, **kwargs): if not Core.instance().getUser(getContext().initiator).hasRole(roleType): raise AccessDenied("User '%s' does not have %s access" % (str(getContext().initiator), roles[roleType])) return rpc_method(*args, **kwargs)
def db(self): """Instance of db connection for this job""" if not self._db: from rcore import Core self._db = Core.instance().makeDbSession() return self._db
def delete(self): c = Core.instance().db.cursor() c.execute("DELETE FROM contacts WHERE type=? AND address=?", (self.type, self.address))
def parse(self, data): self.services = Core.instance().tagParser.servicesFromString(data)