Exemplo n.º 1
0
    def run(self):
        site_code = self._cfg.site_code
        period = int(self._cfg.status_monitoring_period)
        auth = (self._cfg.login, self._cfg.password)

        query = self._cfg.job_status_url

        self._log.info('started (site_code=%s period=%d secs)', site_code, period)

        last_check = 0

        while True:
            now = time.time()
            if now - last_check >= period:
                queue = PendingJobsQueue()
                for job_id in queue.items():
                    if self._debug:
                        self._log.debug('requesting status of site/job %s/%s', self._cfg.site_code, job_id)

                    resp = requests.get(url=query % (site_code, job_id), auth=auth)

                    if resp.ok:
                        if self._debug:
                            self._log.debug('got reply : %s', resp.text)

                        reply = json.loads(resp.text)

                        completion_code = reply['code']

                        if completion_code == 0:    # completed
                            # log it and remove the job from the queue
                            log.info('job %s completed ok', job_id)
                            queue.remove(job_id)
                        elif completion_code < 0:
                            # solid error => log it and remove the job from the queue
                            try:
                                code_msg = reply['status']
                            except KeyError:
                                code_msg = "unknown code"
                            log.error('job %s failed with code %d (%s)', job_id, completion_code, code_msg)
                            queue.remove(job_id)

                        # otherwise the job is still pending. Just leave it a is

                    else:
                        log.error("server replied with : %d - %s", resp.status_code, resp.reason)

                last_check = now

            if self._terminated:
                self._log.info('terminate request detected')
                break

            time.sleep(self.TERMINATE_CHECK_PERIOD)

        self._log.info('worker thread terminated')
Exemplo n.º 2
0
    process_cfg = ProcessConfiguration()

    # Loads the configuration parameters
    try:
        process_cfg.load(pycstbox.config.make_config_file_path(CONFIG_FILE_NAME))

    except ConfigParser.Error as e:
        log.fatal('configuration error (%s)', e)
        sys.exit(1)

    else:
        log.debug('--> %s:', process_cfg.as_dict())
        log.info('initializing export process')
        process = DWHEventsExportProcess()
        process.log_setLevel_from_args(args)

        try:
            error = process.run(process_cfg)

        except Exception as e:      #pylint: disable=W0703
            log.exception(e)
            log.fatal('process failed with an unexpected error')
            sys.exit(1)

        else:
            if error:
                log.error('process failed with errcode=%d', error)
                sys.exit(error)
            else:
                log.info('process completed ok')