Esempio n. 1
0
 def finish_exception(self, e: Exception, traceback: str):
     logger.warning("Job {} finished with exception: {}".format(self.id, str(e)))
     self.finish_time = datetime.datetime.utcnow()
     self.status = JobStatus.EXCEPTION
     try:
         self.exception = json.dumps(
             {
                 'message': str(e),
                 'type': type(e).__name__,
                 'args': e.args,
                 'traceback': traceback
             }, default=json_dumper)
     except Exception as e:
         errmsg = "Unable to serialize exception or traceback: {}".format(str(e))
         logger.exception(errmsg)
         self.exception = {"error": errmsg}
     try:
         json_data = json.dumps({
             "job_id": self.id,
             "status": "EXCEPTION",
             "exception": str(e),
         })
         add_event(json_data=json_data, event_type="update", update_type="job")
     except Exception as e:
         pass
Esempio n. 2
0
    def finish_success(self, res: dict, next_job_id: Optional[int]):
        try:
            if isinstance(res, NornirJobResult) and isinstance(res.nrresult, AggregatedResult):
                self.result = {'devices': nr_result_serialize(res.nrresult)}
                if res.change_score and type(res.change_score) == int:
                    self.change_score = res.change_score
            elif isinstance(res, (StrJobResult, DictJobResult)):
                self.result = res.result
            else:
                self.result = json.dumps(res, default=json_dumper)
        except Exception as e:
            logger.exception("Job {} got unserializable ({}) result after finishing: {}". \
                           format(self.id, str(e), self.result))
            self.result = {"error": "unserializable"}

        self.finish_time = datetime.datetime.utcnow()
        if self.status == JobStatus.ABORTING:
            self.status = JobStatus.ABORTED
        else:
            self.status = JobStatus.FINISHED
        if next_job_id:
            # TODO: check if this exists in the db?
            self.next_job_id = next_job_id
        try:
            event_data = {
                "job_id": self.id,
                "status": self.status.name
            }
            if next_job_id:
                event_data['next_job_id'] = next_job_id
            json_data = json.dumps(event_data)
            add_event(json_data=json_data, event_type="update", update_type="job")
        except Exception as e:
            pass
Esempio n. 3
0
def after_update_device(mapper, connection, target: Device):
    update_data = {
        "action": "UPDATED",
        "device_id": target.id,
        "hostname": target.hostname,
        "object": target.as_dict()
    }
    json_data = json.dumps(update_data)
    add_event(json_data=json_data, event_type="update", update_type="device")
Esempio n. 4
0
 def finish_abort(self, message: str):
     logger.debug("Job {} aborted: {}".format(self.id, message))
     self.finish_time = datetime.datetime.utcnow()
     self.status = JobStatus.ABORTED
     self.result = {"message": message}
     try:
         json_data = json.dumps({
             "job_id": self.id,
             "status": "ABORTED",
             "message": message,
         })
         add_event(json_data=json_data, event_type="update", update_type="job")
     except Exception as e:
         pass
Esempio n. 5
0
 def start_job(self, function_name: Optional[str] = None, scheduled_by: Optional[str] = None):
     self.start_time = datetime.datetime.utcnow()
     self.status = JobStatus.RUNNING
     self.finished_devices = []
     if function_name:
         self.function_name = function_name
     if scheduled_by:
         self.scheduled_by = scheduled_by
     try:
         json_data = json.dumps({
             "job_id": self.id,
             "status": "RUNNING",
             "function_name": self.function_name,
             "scheduled_by": self.scheduled_by
         })
         add_event(json_data=json_data, event_type="update", update_type="job")
     except Exception as e:
         pass
Esempio n. 6
0
 def emit(self, record):
     msg = self.format(record)
     add_event(msg, level=record.levelname)