def _onsuccess(self, result): """ To execute on execution success :param kser.result.Result result: Execution result :return: Execution result :rtype: kser.result.Result """ if KSER_METRICS_ENABLED == "yes": KSER_TASKS_STATUS.labels(__hostname__, self.__class__.path, 'SUCCESS').inc() if result: result = self.result + result else: result = self.result logger.info( "{}.Success: {}[{}]: {}".format(self.__class__.__name__, self.__class__.path, self.uuid, result), extra=dict( kmsg=Message(self.uuid, entrypoint=self.__class__.path, params=self.params, metadata=self.metadata).dump(), kresult=ResultSchema().dump(result) if result else dict())) return self.onsuccess(result)
def _onerror(self, result): """ To execute on execution failure :param kser.result.Result result: Execution result :return: Execution result :rtype: kser.result.Result """ if KSER_METRICS_ENABLED == "yes": KSER_TASKS_STATUS.labels(__hostname__, self.__class__.path, 'FAILED').inc() if result: result = self.result + result else: result = self.result extra = dict(kmsg=Message(self.uuid, entrypoint=self.__class__.path, params=self.params, metadata=self.metadata).dump(), kresult=ResultSchema().dump(result) if result else dict()) if result: error = result.search_value("error") if error: extra['error'] = error logger.error("{}.Failed: {}[{}]: {}".format(self.__class__.__name__, self.__class__.path, self.uuid, result), extra=extra) return self.onerror(result)
def _prebuild(self, **kwargs): logger.debug("{}.PreBuild: {}[{}]: {}".format(self.__class__.__name__, self.__class__.path, self.uuid, kwargs), extra=dict(kmsg=Message(self.uuid, entrypoint=self.__class__.path, params=self.params).dump())) self.check_required_params() return self.prebuild(**kwargs)
def _prerun(self): """ To execute before running message """ self.check_required_params() self._set_status("RUNNING") logger.debug("{}.PreRun: {}[{}]: running...".format( self.__class__.__name__, self.__class__.path, self.uuid), extra=dict(kmsg=Message(self.uuid, entrypoint=self.__class__.path, params=self.params).dump())) return self.prerun()
def decode(self, jdata): """ Decode message using libsodium :param str jdata: jdata to load :return: the Encoded message """ ckmsg = self.loads(jdata) return Message.loads( pysodium.crypto_secretbox_open( base64.b64decode(ckmsg["data"]), base64.b64decode(ckmsg["nonce"]), base64.b64decode(self.secretbox_key)).decode('utf-8'))
def to_Message(self, result=None): """ Entrypoint -> Message :param kser.result.Result result: Execution result :return: Kafka message :rtype kser.schemas.Message """ return Message(uuid=self.uuid, entrypoint=self.__class__.path, params=self.params, result=result if result else self.result, metadata=self.metadata)
def log(self, message, level=logging.INFO, *args, **kwargs): msg = "{}.{}: {}[{}]: {}".format( self.__class__.__name__, self.status, self.__class__.path, self.uuid, message ) extra = kwargs.pop("extra", dict()) extra.update(dict(kmsg=Message( self.uuid, entrypoint=self.__class__.path, params=self.params ).dump())) return logger.log( level=level, msg=msg, extra=extra, *args, **kwargs )
def _set_status(self, status, result=None): """ update operation status :param str status: New status :param cdumay_result.Result result: Execution result """ logger.info("{}.SetStatus: {}[{}] status update '{}' -> '{}'".format( self.__class__.__name__, self.__class__.path, self.uuid, self.status, status), extra=dict(kmsg=Message(self.uuid, entrypoint=self.__class__.path, params=self.params).dump())) return self.set_status(status, result)
def _build_tasks(self, **kwargs): """ :param dict kwargs: tasks parameters (~=context) :return: list of tasks :rtype: list(kser.sequencing.operation.Operation) """ tasks = self.build_tasks(**kwargs) logger.debug("{}.BuildTasks: {} task(s) found".format( self.__class__.__name__, len(tasks)), extra=dict(kmsg=Message(self.uuid, entrypoint=self.__class__.path, params=self.params).dump())) return tasks
def _postrun(self, result): """ To execute after exection :param kser.result.Result result: Execution result :return: Execution result :rtype: kser.result.Result """ logger.debug("{}.PostRun: {}[{}]".format(self.__class__.__name__, self.__class__.path, self.uuid), extra=dict(kmsg=Message(self.uuid, entrypoint=self.__class__.path, params=self.params, metadata=self.metadata).dump())) return self.postrun(result)
def _prerun(self): """ To execute before running message :return: Kafka message :rtype: kser.schemas.Message """ logger.debug("{}.PreRun: {}[{}]".format(self.__class__.__name__, self.__class__.path, self.uuid), extra=dict(kmsg=Message(self.uuid, entrypoint=self.__class__.path, params=self.params, metadata=self.metadata).dump())) self.check_required_params() return self.prerun()
def _run(self): """ Execution body :return: Execution result :rtype: kser.result.Result """ if KSER_METRICS_ENABLED == "yes": KSER_TASK_COUNT.inc() logger.debug("{}.Run: {}[{}]".format(self.__class__.__name__, self.__class__.path, self.uuid), extra=dict(kmsg=Message(self.uuid, entrypoint=self.__class__.path, params=self.params, metadata=self.metadata).dump())) return self.run()
def _onsuccess(self, result): """ To execute on execution success :param cdumay_result.Result result: Execution result :return: Execution result :rtype: cdumay_result.Result """ self._set_status("SUCCESS", result) logger.info( "{}.Success: {}[{}]: {}".format(self.__class__.__name__, self.__class__.path, self.uuid, result), extra=dict( kmsg=Message(self.uuid, entrypoint=self.__class__.path, params=self.params).dump(), kresult=ResultSchema().dump(result) if result else dict())) return self.onsuccess(result)
def log(self, message, level=logging.INFO, *args, **kwargs): """ Send log entry :param str message: log message :param int level: Logging level :param list args: log record arguments :param dict kwargs: log record key argument """ msg = "{}.{}: {}[{}]: {}".format(self.__class__.__name__, self.status, self.__class__.path, self.uuid, message) extra = kwargs.pop("extra", dict()) extra.update( dict(kmsg=Message(self.uuid, entrypoint=self.__class__.path, params=self.params, metadata=self.metadata).dump())) return logger.log(level=level, msg=msg, extra=extra, *args, **kwargs)
def _onerror(self, result): """ To execute on execution failure :param cdumay_result.Result result: Execution result :return: Execution result :rtype: cdumay_result.Result """ self._set_status("FAILED", result) logger.error( "{}.Failed: {}[{}]: {}".format( self.__class__.__name__, self.__class__.path, self.uuid, result ), extra=dict( kmsg=Message( self.uuid, entrypoint=self.__class__.path, params=self.params ).dump(), kresult=ResultSchema().dump(result) if result else dict() ) ) return self.onerror(result)