def test_unserializable(self): # Hide an unserializable object as deep as we can in supported collections value = ['a', ('b', {'c': {'d', object()}})] with pytest.raises(TypeError): serialization.serialize(value) with pytest.raises(TypeError): serialization.dumps(value)
def add_backlog(self, task, entry, amount='', session=None): """Add single entry to task backlog If :amount: is not specified, entry will only be injected on next execution.""" snapshot = entry.get('_backlog_snapshot') if not snapshot: if task.current_phase != 'input': # Not having a snapshot is normal during input phase, don't display a warning logger.warning( 'No input snapshot available for `{}`, using current state', entry['title']) snapshot = serialize(entry) expire_time = datetime.now() + parse_timedelta(amount) backlog_entry = (session.query(BacklogEntry).filter( BacklogEntry.title == entry['title']).filter( BacklogEntry.task == task.name).first()) if backlog_entry: # If there is already a backlog entry for this, update the expiry time if necessary. if backlog_entry.expire < expire_time: logger.debug('Updating expiry time for {}', entry['title']) backlog_entry.expire = expire_time else: logger.debug('Saving {}', entry['title']) backlog_entry = BacklogEntry() backlog_entry.title = entry['title'] backlog_entry.entry = snapshot backlog_entry.task = task.name backlog_entry.expire = expire_time session.add(backlog_entry)
def serialize(cls, entry: 'Entry') -> dict: fields = {} for key in entry: if key.startswith('_') or entry.is_lazy(key): continue try: fields[key] = serialize(entry[key]) except TypeError as exc: logger.debug('field {} was not serializable. {}', key, exc) lazy_lookups = [] for ll in entry.lazy_lookups: try: lazy_lookups.append(serialize(ll)) except TypeError as exc: logger.exception( 'BUG: Lazy lookup was not compatible with serialization. Please file a bug report: {}', exc, ) return {'fields': fields, 'lazy_lookups': lazy_lookups}
def on_task_metainfo(self, task, config): # Take a snapshot of any new entries' states before metainfo event in case we have to store them to backlog for entry in task.entries: entry['_backlog_snapshot'] = serialize(entry)