Пример #1
0
    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)
Пример #2
0
    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)
Пример #3
0
 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)
Пример #4
0
 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()
Пример #5
0
    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'))
Пример #6
0
    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)
Пример #7
0
    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
        )
Пример #8
0
    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)
Пример #9
0
    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
Пример #10
0
    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)
Пример #11
0
    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()
Пример #12
0
    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()
Пример #13
0
 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)
Пример #14
0
    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)
Пример #15
0
    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)