Ejemplo n.º 1
0
    def send_data(self):
        if not self._archive:
            self.log_warn('No archive previously created. We should not have been called.')
            return

        cfg_server = self._config[_CFG_PROPS.SERVER]
        url = self._config[_CFG_PROPS.API_URLS][_CFG_PROPS.DATA_UPLOAD] % {
            'host': cfg_server[_CFG_PROPS.HOST],
            'site': self._site_code
        }
        auth = cfg_server[_CFG_PROPS.AUTH]

        with file(self._archive, 'rb') as archive:
            self.log_info('uploading file %s using URL %s', self._archive, url)
            resp = requests.post(
                url,
                files={
                    'zip': archive
                },
                auth=(auth[_CFG_PROPS.LOGIN], auth[_CFG_PROPS.PASSWORD])
            )

        self.log_info('%s - %s', resp, resp.text)
        if resp.ok:
            resp_data = json.loads(resp.text)
            job_id = resp_data['jobID']

            self.log_info('upload successful (%s) - job id=%s' % (resp_data['message'], job_id))

            # nothing else to be done : data have been transmitted
            # if anomalies are detected during the processing, they will be notified to the
            # site manager, and there is no need to activate the backlog retry mechanism here,
            # since it will not fix the anomaly but just reproduce it again and again

            # Anyway, for record's sake, we go one step further by monitoring the job completion status
            # and log the result

            # add the job id to the persistent queue
            queue = PendingJobsQueue()
            queue.append(job_id)

        else:
            try:
                msg = 'failed : %d - %s (%s)' % (resp.status_code, resp.reason, resp.text)
            except ValueError:
                msg = 'unexpected server error : %d - %s' %(resp.status_code, resp.reason)
            self.log_error(msg)
            raise pycstbox.export.ExportError(msg)