Esempio n. 1
0
 def start_ticker(self):
     log.info("Ticking every %ss." % self.tick_interval)
     tock = False
     while True:
         log.debug("Tock." if tock else "Tick.")
         # TODO: timestamp here instead of 1, for debugging?
         self.redis.rpush(self.tick_key, 1)
         sleep(self.tick_interval)
         tock = not tock
Esempio n. 2
0
 def start_ticker(self):
     log.info('Ticking every %ss.' % self.tick_interval)
     tock = False
     while True:
         log.debug('Tock.' if tock else 'Tick.')
         # TODO: timestamp here instead of True, for debugging?
         self.redis.rpush(self.tick_key, True)
         sleep(self.tick_interval)
         tock = not tock
Esempio n. 3
0
    def save_snapshot(self):
        log.info("Saving snapshot: %s" % self.snapshot_path)
        child_pid = os.fork()

        if not child_pid:
            f = tempfile.NamedTemporaryFile(delete=False)
            snapshot = self.snapshot_serializer.serialize(
                {"entities": [e.to_dict() for e in self.all()]}
            )
            # if self.config['persistence'].get('compressed'):
            #     snapshot = zlib.compress(snapshot)
            f.write(snapshot)
            f.close()
            shutil.move(f.name, self.snapshot_path)
            os._exit(os.EX_OK)
Esempio n. 4
0
    def save_snapshot(self):
        log.info('Saving snapshot: %s' % self.snapshot_path)
        child_pid = os.fork()

        if not child_pid:
            f = tempfile.NamedTemporaryFile(delete=False)
            snapshot = self.snapshot_serializer.serialize({
                'entities': [e.to_dict() for e in self.all()]
            })
            # if self.config['persistence'].get('compressed'):
            #     snapshot = zlib.compress(snapshot)
            f.write(snapshot)
            f.close()
            shutil.move(f.name, self.snapshot_path)
            os._exit(os.EX_OK)
Esempio n. 5
0
    def start(self):
        try:
            self.redis.ping()
        except ConnectionError as e:
            fatal("Redis error: %s" % e.message)

        self.running = True
        log.info("Listening.")

        # Clear any existing tick events
        self.redis.ltrim(self.tick_key, 0, 0)
        try:
            while self.running:
                self.process_one_event()
        except Exception as e:
            log.critical(traceback.format_exc())
        except BaseException as e:
            pass
        finally:
            self.save_snapshot()
Esempio n. 6
0
    def start(self):
        try:
            self.redis.ping()
        except ConnectionError as e:
            fatal("Redis error: %s" % e.message)

        self.running = True
        log.info('Listening.')

        # Clear any existing tick events
        self.redis.ltrim(self.tick_key, 0, 0)
        try:
            while self.running:
                self.process_one_event()
        except Exception as e:
            log.critical(traceback.format_exc())
        except BaseException as e:
            pass
        finally:
            self.save_snapshot()
Esempio n. 7
0
    def load_snapshot(self):
        if not os.path.exists(self.snapshot_path):
            return False

        log.info("Loading snapshot: %s" % self.snapshot_path)
        with open(self.snapshot_path, "r") as f:
            snapshot = f.read()
            # if self.config['persistence'].get('compressed'):
            #     snapshot = zlib.decompress(snapshot)
            snapshot = self.snapshot_serializer.unserialize(snapshot)

            log.info("Creating entities...")

            for entity_dict in snapshot["entities"]:
                entity = Entity.from_dict(
                    {"id": entity_dict["id"], "hearing": entity_dict["hearing"]}, self
                )
                self._max_id = max(self._max_id, entity.id)

            log.info("Creating components...")

            for entity_dict in snapshot["entities"]:
                entity = self.get(entity_dict["id"])
                entity.attach_from_dict(entity_dict)

        return True
Esempio n. 8
0
    def load_snapshot(self):
        if not os.path.exists(self.snapshot_path):
            return False

        log.info('Loading snapshot: %s' % self.snapshot_path)
        with open(self.snapshot_path, 'r') as f:
            snapshot = f.read()
            # if self.config['persistence'].get('compressed'):
            #     snapshot = zlib.decompress(snapshot)
            snapshot = self.snapshot_serializer.unserialize(snapshot)

            log.info('Creating entities...')

            for entity_dict in snapshot['entities']:
                entity = Entity.from_dict({
                    'id': entity_dict['id'],
                    'hearing': entity_dict['hearing'],
                }, self)
                self._max_id = max(self._max_id, entity.id)

            log.info('Creating components...')

            for entity_dict in snapshot['entities']:
                entity = self.get(entity_dict['id'])
                entity.attach_from_dict(entity_dict)

        return True