Example #1
0
 def do_wait(wait_seconds):
     """Wait for n seconds, or until closing event is set"""
     for _ in xrange(wait_seconds):
         if closing_event.is_set():
             return True
         sleep(1)
     return False
Example #2
0
    def _sync(self):
        start = time()
        self.log.debug("syncing...")

        if not self._check_auth_token():
            return

        if not self.auth_token:
            self.log.error("sync failed -- no auth token, have you run 'dataplicity register'?")
            return

        if not os.path.exists(self.firmware_path):
            self.log.debug("no firmware installed")
            try:
                self.deploy()
            except:
                self.log.exception("unable to deploy firmware")
            raise ForceRestart("new firmware")

        samplers_updated = []
        random.seed()
        sync_id = ''.join(random.choice('abcdefghijklmnopqrstuvwxyz0123456789') for _ in xrange(12))
        batch = None
        try:
            with self.remote.batch() as batch:
                # Authenticate
                batch.call_with_id('authenticate_result',
                                   'device.check_auth',
                                   device_class=self.device_class,
                                   serial=self.serial,
                                   auth_token=self.auth_token,
                                   sync_id=sync_id)

                # Tell the server which firmware we're running
                batch.call_with_id('set_firmware_result',
                                   'device.set_firmware',
                                   version=self.current_firmware_version)

                # Check for new firmware (if required)
                if self.check_firmware:
                    batch.call_with_id('firmware_result',
                                       'device.check_firmware',
                                       current_version=self.current_firmware_version)

                samplers_updated = self._sync_samples(batch)
                self._sync_conf(batch)
                self._sync_timelines(batch)
                self._sync_m2m(batch)

            # get_result will throw exceptions with (hopefully) helpful error messages if they fail
            batch.get_result('authenticate_result')

            # If the server doesn't have the current firmware, we don't want to break the rest of the sync
            try:
                batch.get_result('set_firmware_result')
            except Exception as e:
                self.log.warning("unable to set firmware ({})".format(e))

            self._update_samples(batch, samplers_updated)
            self._update_conf(batch)

            ellapsed = time() - start
            self.log.debug('sync complete {:0.2f}s'.format(ellapsed))

        finally:
            # We have to run this code so we have a chance to update, if a bug is causing the sync handler to fail
            try:
                self._sync_firmware(batch)
            finally:
                ellapsed = time() - start
                self.log.debug('sync complete {:0.2f}s'.format(ellapsed))