class OVDMGearmanWorker(python3_gearman.GearmanWorker):
    """
    Class for the current Gearman worker
    """
    def __init__(self):
        self.stop = False
        self.ovdm = OpenVDM()
        self.cruise_id = self.ovdm.get_cruise_id()
        self.system_status = self.ovdm.get_system_status()
        self.cruise_data_transfer = {}
        self.shipboard_data_warehouse_config = self.ovdm.get_shipboard_data_warehouse_config(
        )
        super().__init__(host_list=[self.ovdm.get_gearman_server()])

    def on_job_execute(self, current_job):
        """
        Function run whenever a new job arrives
        """

        logging.debug("current_job: %s", current_job)

        self.stop = False
        payload_obj = json.loads(current_job.data)

        try:
            self.cruise_data_transfer = self.ovdm.get_cruise_data_transfer(
                payload_obj['cruiseDataTransfer']['cruiseDataTransferID'])

            if not self.cruise_data_transfer:
                return self.on_job_complete(
                    current_job,
                    json.dumps({
                        'parts': [{
                            "partName":
                            "Located Cruise Data Tranfer Data",
                            "result":
                            "Fail",
                            "reason":
                            "Could not find configuration data for cruise data transfer"
                        }],
                        'files': {
                            'new': [],
                            'updated': [],
                            'exclude': []
                        }
                    }))

            if self.cruise_data_transfer['status'] == "1":  #not running
                logging.info(
                    "Transfer job for %s skipped because a transfer for that cruise data destination is already in-progress",
                    self.cruise_data_transfer['name'])
                return self.on_job_complete(
                    current_job,
                    json.dumps({
                        'parts': [{
                            "partName": "Transfer In-Progress",
                            "result": "Fail",
                            "reason": "Transfer is already in-progress"
                        }],
                        'files': {
                            'new': [],
                            'updated': [],
                            'exclude': []
                        }
                    }))

        except Exception as err:
            logging.debug(str(err))
            return self.on_job_complete(
                current_job,
                json.dumps({
                    'parts': [{
                        "partName":
                        "Located Cruise Data Tranfer Data",
                        "result":
                        "Fail",
                        "reason":
                        "Could not find retrieve data for cruise data transfer from OpenVDM API"
                    }],
                    'files': {
                        'new': [],
                        'updated': [],
                        'exclude': []
                    }
                }))

        self.system_status = payload_obj[
            'systemStatus'] if 'systemStatus' in payload_obj else self.ovdm.get_system_status(
            )
        self.cruise_data_transfer.update(payload_obj['cruiseDataTransfer'])

        if self.system_status == "Off" or self.cruise_data_transfer[
                'enable'] == '0':
            logging.info(
                "Transfer job for %s skipped because that cruise data transfer is currently disabled",
                self.cruise_data_transfer['name'])
            return self.on_job_complete(
                current_job,
                json.dumps({
                    'parts': [{
                        "partName": "Transfer Enabled",
                        "result": "Ignore",
                        "reason": "Transfer is disabled"
                    }],
                    'files': {
                        'new': [],
                        'updated': [],
                        'exclude': []
                    }
                }))

        self.cruise_id = self.ovdm.get_cruise_id()

        logging.info("Job: %s, %s transfer started at: %s", current_job.handle,
                     self.cruise_data_transfer['name'],
                     time.strftime("%D %T", time.gmtime()))

        self.shipboard_data_warehouse_config = self.ovdm.get_shipboard_data_warehouse_config(
        )

        return super().on_job_execute(current_job)

    def on_job_exception(self, current_job, exc_info):
        """
        Function run whenever the current job has an exception
        """

        logging.error("Job: %s, %s transfer failed at: %s", current_job.handle,
                      self.cruise_data_transfer['name'],
                      time.strftime("%D %T", time.gmtime()))

        self.send_job_data(
            current_job,
            json.dumps([{
                "partName": "Worker crashed",
                "result": "Fail",
                "reason": "Unknown"
            }]))
        self.ovdm.set_error_cruise_data_transfer(
            self.cruise_data_transfer['cruiseDataTransferID'],
            'Worker crashed')

        exc_type, _, exc_tb = sys.exc_info()
        fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
        logging.error(exc_type, fname, exc_tb.tb_lineno)
        return super().on_job_exception(current_job, exc_info)

    def on_job_complete(self, current_job, job_result):
        """
        Function run whenever the current job completes
        """

        results_obj = json.loads(job_result)

        if len(results_obj['parts']) > 0:
            if results_obj['parts'][-1]['result'] == "Fail":  # Final Verdict
                self.ovdm.set_error_cruise_data_transfer(
                    self.cruise_data_transfer['cruiseDataTransferID'],
                    results_obj['parts'][-1]['reason'])
            else:
                self.ovdm.set_idle_cruise_data_transfer(
                    self.cruise_data_transfer['cruiseDataTransferID'])
        else:
            self.ovdm.set_idle_cruise_data_transfer(
                self.cruise_data_transfer['cruiseDataTransferID'])

        logging.debug("Job Results: %s", json.dumps(results_obj, indent=2))
        logging.info("Job: %s, %s transfer completed at: %s",
                     current_job.handle, self.cruise_data_transfer['name'],
                     time.strftime("%D %T", time.gmtime()))

        return super().send_job_complete(current_job, job_result)

    def stop_task(self):
        """
        Function to stop the current job
        """
        self.stop = True
        logging.warning("Stopping current task...")

    def quit_worker(self):
        """
        Function to quit the worker
        """
        self.stop = True
        logging.warning("Quitting worker...")
        self.shutdown()
Пример #2
0
class OVDMGearmanWorker(python3_gearman.GearmanWorker):
    """
    Class for the current Gearman worker
    """

    def __init__(self):
        self.stop = False
        self.ovdm = OpenVDM()
        self.cruise_id = self.ovdm.get_cruise_id()
        self.cruise_data_transfer = {}
        self.shipboard_data_warehouse_config = self.ovdm.get_shipboard_data_warehouse_config()
        super().__init__(host_list=[self.ovdm.get_gearman_server()])


    def on_job_execute(self, current_job):
        """
        Function run whenever a new job arrives
        """

        logging.debug("current_job: %s", current_job)

        self.stop = False
        payload_obj = json.loads(current_job.data)

        if 'cruiseDataTransferID' in payload_obj['cruiseDataTransfer']:
            self.cruise_data_transfer = self.ovdm.get_cruise_data_transfer(payload_obj['cruiseDataTransfer']['cruiseDataTransferID'])

            if not self.cruise_data_transfer:
                logging.error("could not find configuration data")
                return self.on_job_complete(current_job, json.dumps({'parts':[{"partName": "Located Cruise Data Tranfer Data", "result": "Fail", "reason": "Could not find configuration data for cruise data transfer"},{"partName": "Final Verdict", "result": "Fail", "reason": "Could not find configuration data for cruise data transfer"}]}))

            self.cruise_data_transfer.update(payload_obj['cruiseDataTransfer'])

        else:
            self.cruise_data_transfer = payload_obj['cruiseDataTransfer']

        self.cruise_id = self.ovdm.get_cruise_id()

        logging.info("Job: %s, %s transfer test started at: %s", current_job.handle, self.cruise_data_transfer['name'], time.strftime("%D %T", time.gmtime()))

        self.shipboard_data_warehouse_config = self.ovdm.get_shipboard_data_warehouse_config()

        return super().on_job_execute(current_job)


    def on_job_exception(self, current_job, exc_info):
        """
        Function run whenever the current job has an exception
        """

        logging.error("Job: %s, %s transfer test failed at: %s", current_job.handle, self.cruise_data_transfer['name'], time.strftime("%D %T", time.gmtime()))

        self.send_job_data(current_job, json.dumps([{"partName": "Worker crashed", "result": "Fail", "reason": "Unknown"}]))

        if 'cruiseDataTransferID' in self.cruise_data_transfer:
            self.ovdm.set_error_cruise_data_transfer_test(self.cruise_data_transfer['cruiseDataTransferID'], 'Worker crashed')

        exc_type, _, exc_tb = sys.exc_info()
        fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
        logging.error(exc_type, fname, exc_tb.tb_lineno)
        return super().on_job_exception(current_job, exc_info)



    def on_job_complete(self, current_job, job_result):
        """
        Function run whenever the current job completes
        """

        results_obj = json.loads(job_result)

        if 'cruiseDataTransferID' in self.cruise_data_transfer:
            if len(results_obj['parts']) > 0:
                if results_obj['parts'][-1]['result'] == "Fail": # Final Verdict
                    self.ovdm.set_error_cruise_data_transfer_test(self.cruise_data_transfer['cruiseDataTransferID'], results_obj['parts'][-1]['reason'])
                else:
                    self.ovdm.clear_error_cruise_data_transfer(self.cruise_data_transfer['cruiseDataTransferID'], self.cruise_data_transfer['status'])
            else:
                self.ovdm.clear_error_cruise_data_transfer(self.cruise_data_transfer['cruiseDataTransferID'], self.cruise_data_transfer['status'])

        logging.debug("Job Results: %s", json.dumps(results_obj, indent=2))
        logging.info("Job: %s, %s transfer test completed at: %s", current_job.handle, self.cruise_data_transfer['name'], time.strftime("%D %T", time.gmtime()))

        return super().send_job_complete(current_job, job_result)


    def stop_task(self):
        """
        Function to stop the current job
        """
        self.stop = True
        logging.warning("Stopping current task...")


    def quit_worker(self):
        """
        Function to quit the worker
        """
        self.stop = True
        logging.warning("Quitting worker...")
        self.shutdown()