Ejemplo n.º 1
0
class OVDMGearmanWorker(python3_gearman.GearmanWorker):
    """
    Class for the current Gearman worker
    """
    def __init__(self):
        self.stop = False
        self.ovdm = OpenVDM()
        self.job_pid = ''
        self.job_info = {}
        super().__init__(host_list=[self.ovdm.get_gearman_server()])

    def _get_job_info(self):
        """
        Fetch job metadata
        """

        collection_system_transfers = self.ovdm.get_collection_system_transfers(
        )
        for collection_system_transfer in collection_system_transfers:
            if collection_system_transfer['pid'] == self.job_pid:
                return {
                    'type': 'collectionSystemTransfer',
                    'id':
                    collection_system_transfer['collectionSystemTransferID'],
                    'name': collection_system_transfer['name'],
                    'pid': collection_system_transfer['pid']
                }

        cruise_data_transfers = self.ovdm.get_cruise_data_transfers()
        for cruise_data_transfer in cruise_data_transfers:
            if cruise_data_transfer['pid'] != "0":
                return {
                    'type': 'cruiseDataTransfer',
                    'id': cruise_data_transfer['cruiseDataTransferID'],
                    'name': cruise_data_transfer['name'],
                    'pid': cruise_data_transfer['pid']
                }

        cruise_data_transfers = self.ovdm.get_required_cruise_data_transfers()
        for cruise_data_transfer in cruise_data_transfers:
            if cruise_data_transfer['pid'] != "0":
                return {
                    'type': 'cruiseDataTransfer',
                    'id': cruise_data_transfer['cruiseDataTransferID'],
                    'name': cruise_data_transfer['name'],
                    'pid': cruise_data_transfer['pid']
                }

        tasks = self.ovdm.get_tasks()
        for task in tasks:
            if task['pid'] != "0":
                return {
                    'type': 'task',
                    'id': task['taskID'],
                    'name': task['name'],
                    'pid': task['pid']
                }

        return {'type': 'unknown'}

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

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

        payload_obj = json.loads(current_job.data)

        self.job_pid = payload_obj['pid']
        self.job_info = self._get_job_info()

        logging.info("Job: %s, Killing PID: %s failed at: %s",
                     current_job.handle, self.job_pid,
                     time.strftime("%D %T", time.gmtime()))

        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.info("Job: %s Killing PID %s failed at: %s",
                     current_job.handle, self.job_pid,
                     time.strftime("%D %T", time.gmtime()))

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

        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)

        logging.debug("Job Results: %s", json.dumps(results_obj, indent=2))
        logging.info("Job: %s, Killing PID %s completed at: %s",
                     current_job.handle, self.job_pid,
                     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()
Ejemplo n.º 2
0
    time.sleep(5)

    logging.info("Setting all tasks to idle.")
    tasks = openVDM.get_tasks()
    for task in tasks:
        openVDM.set_idle_task(task['taskID'])

    logging.info("Setting all Collection System Transfers to idle.")
    collection_system_transfers = openVDM.get_collection_system_transfers()
    for collection_system_transfer in collection_system_transfers:
        if not collection_system_transfer['status'] == '3':
            openVDM.set_idle_collection_system_transfer(
                collection_system_transfer['collectionSystemTransferID'])

    logging.info("Setting all Cruise Data Transfers to idle.")
    cruise_data_transfers = openVDM.get_cruise_data_transfers()
    for cruise_data_transfer in cruise_data_transfers:
        if not cruise_data_transfer['status'] == '3':
            openVDM.set_idle_cruise_data_transfer(
                cruise_data_transfer['cruiseDataTransferID'])

    required_cruise_data_transfers = openVDM.get_required_cruise_data_transfers(
    )
    for required_cruise_data_transfer in required_cruise_data_transfers:
        if not required_cruise_data_transfer['status'] == '3':
            openVDM.set_idle_cruise_data_transfer(
                required_cruise_data_transfer['cruiseDataTransferID'])

    logging.info("Clearing all jobs from Gearman.")
    openVDM.clear_gearman_jobs_from_db()
Ejemplo n.º 3
0
        collection_system_transfers = ovdm.get_active_collection_system_transfers()
        for collection_system_transfer in collection_system_transfers:
            logging.info("Submitting collection system transfer job for: %s", collection_system_transfer['longName'])

            gmData = {
                'collectionSystemTransfer': {
                    'collectionSystemTransferID': collection_system_transfer['collectionSystemTransferID']
                }
            }

            completed_job_request = gm_client.submit_job("runCollectionSystemTransfer", json.dumps(gmData), background=True)

            time.sleep(2)


        cruise_data_transfers = ovdm.get_cruise_data_transfers()
        for cruise_data_transfer in cruise_data_transfers:
            logging.info("Submitting cruise data transfer job for: %s", cruise_data_transfer['longName'])

            gmData = {
                'cruiseDataTransfer': {
                    'cruiseDataTransferID': cruise_data_transfer['cruiseDataTransferID']
                }
            }

            completed_job_request = gm_client.submit_job("runCruiseDataTransfer", json.dumps(gmData), background=True)

            time.sleep(2)

        required_cruise_data_transfers = ovdm.get_required_cruise_data_transfers()
        for required_cruise_data_transfer in required_cruise_data_transfers: