def test_decorator_actor_send_with_inherit_Core_3(self): def add_in_registry(): from anyblok import Declarations @Declarations.register(Declarations.Core) class SqlBase: @classmethod def add(cls, val): return val @Declarations.register(Declarations.Model) # noqa class Task: id = Integer(primary_key=True) @actor_send() def add(cls, val): return super(Task, cls).add(val) * 2 registry = self.init_registry_with_bloks(('dramatiq',), add_in_registry) self.assertTrue(isinstance(registry.Task.add, AnyBlokActor)) self.assertFalse(EnvironmentManager.get('_postcommit_hook')) registry.Task.add(1) self.assertEqual(registry.Dramatiq.Message.query().count(), 1) self.assertTrue(EnvironmentManager.get('_postcommit_hook'))
def test_without_environment_for_get(self): # don't use define_environment_cls, because she must be verify EnvironmentManager.environment = None try: EnvironmentManager.get('db_name') self.fail('No watchdog for None environment') except EnvironmentException: pass
def load(cls, entry_points=('bloks',)): """ Load all the bloks and import them :param entry_points: Use by ``iter_entry_points`` to get the blok :exception: BlokManagerException """ if not entry_points: raise BlokManagerException("The entry_points mustn't be empty") cls.entry_points = entry_points if EnvironmentManager.get('current_blok'): while EnvironmentManager.get('current_blok'): sleep(0.1) EnvironmentManager.set('current_blok', 'start') bloks = [] for entry_point in entry_points: count = 0 for i in iter_entry_points(entry_point): count += 1 blok = i.load() blok.required_by = [] blok.optional_by = [] blok.conditional_by = [] blok.conflicting_by = [] cls.set(i.name, blok) blok.name = i.name bloks.append((blok.priority, i.name)) if not count: raise BlokManagerException( "Invalid bloks group %r" % entry_point) # Empty the ordered blok to reload it depending on the priority cls.ordered_bloks = [] bloks.sort() try: while bloks: blok = bloks.pop(0)[1] cls.get_need_blok(blok) finally: EnvironmentManager.set('current_blok', None)
def format(self, record): """ Add color to the message :param record: logging record instance :rtype: logging record formatted """ record.database = EnvironmentManager.get('db_name', 'No database') return logging.Formatter.format(self, record)
def load(cls, entry_points=('bloks', )): """ Load all the bloks and import them :param entry_points: Use by ``iter_entry_points`` to get the blok :exception: BlokManagerException """ if not entry_points: raise BlokManagerException("The entry_points mustn't be empty") cls.entry_points = entry_points if EnvironmentManager.get('current_blok'): while EnvironmentManager.get('current_blok'): sleep(0.1) EnvironmentManager.set('current_blok', 'start') bloks = [] for entry_point in entry_points: count = 0 for i in iter_entry_points(entry_point): count += 1 blok = i.load() blok.required_by = [] blok.optional_by = [] blok.conditional_by = [] blok.conflicting_by = [] cls.set(i.name, blok) blok.name = i.name bloks.append((blok.priority, i.name)) if not count: raise BlokManagerException("Invalid bloks group %r" % entry_point) # Empty the ordered blok to reload it depending on the priority cls.ordered_bloks = [] bloks.sort() try: while bloks: blok = bloks.pop(0)[1] cls.get_need_blok(blok) finally: EnvironmentManager.set('current_blok', None)
def __call__(self, *args, **kwargs): """Send to the broker or call directly the classmethod""" is_called_by_dramatiq_actor = EnvironmentManager.get( 'is_called_by_dramatiq_actor', False) if is_called_by_dramatiq_actor: kwargs.pop('run_at', None) kwargs.pop('delay', None) return self.fn(*args, **kwargs) return self.send(*args, **kwargs)
def set(self, context=None, **kwargs): if context is None: context = {} previous_context = EnvironmentManager.get( 'context', default=ImmutableContextDict({})) manager = AttributeContextManager(previous_context) ctx = previous_context.copy() ctx.update(context) ctx.update(kwargs) EnvironmentManager.set('context', ImmutableContextDict(ctx)) return manager
def test_precommit_hook_twice_with_after_another(self): def add_in_registry(): from anyblok import Declarations @Declarations.register(Declarations.Model) class Test: pass registry = self.init_registry(add_in_registry) registry.Test.precommit_hook('_precommit_hook1') registry.Test.precommit_hook('_precommit_hook2') assert len(EnvironmentManager.get('_precommit_hook', [])) == 2 assert [x[1] for x in EnvironmentManager.get('_precommit_hook')] == [ '_precommit_hook1', '_precommit_hook2'] registry.Test.precommit_hook('_precommit_hook1') assert [x[1] for x in EnvironmentManager.get('_precommit_hook')] == [ '_precommit_hook1', '_precommit_hook2'] registry.Test.precommit_hook( '_precommit_hook1', put_at_the_end_if_exist=True) assert [x[1] for x in EnvironmentManager.get('_precommit_hook')] == [ '_precommit_hook2', '_precommit_hook1']
def test_precommit_hook_twice(self): def add_in_registry(): from anyblok import Declarations @Declarations.register(Declarations.Model) class Test: pass registry = self.init_registry(add_in_registry) registry.Test.precommit_hook('_precommit_hook') registry.Test.precommit_hook('_precommit_hook') assert len(EnvironmentManager.get('_precommit_hook', [])) == 1
def test_decorator_actor_send_with_options(self): def add_in_registry(): from anyblok import Declarations @Declarations.register(Declarations.Model) class Task: id = Integer(primary_key=True) @actor_send(queue_name="other", priority=1) def add(cls, val): return val registry = self.init_registry_with_bloks(('dramatiq',), add_in_registry) self.assertTrue(isinstance(registry.Task.add, AnyBlokActor)) self.assertFalse(EnvironmentManager.get('_postcommit_hook')) registry.Task.add(1) self.assertEqual(registry.Dramatiq.Message.query().count(), 1) self.assertEqual(registry.Task.add.queue_name, "other") self.assertEqual(registry.Task.add.priority, 1) self.assertTrue(EnvironmentManager.get('_postcommit_hook'))
def format(self, record): """ Add color to the message :param record: logging record instance :rtype: logging record formatted """ fg_color, bg_color = LEVEL_COLOR_MAPPING[record.levelno] record.levelname = COLOR_PATTERN % (30 + fg_color, 40 + bg_color, record.levelname) fg_color, bg_color = CYAN, DEFAULT record.database = COLOR_PATTERN % (30 + fg_color, 40 + bg_color, EnvironmentManager.get( 'db_name', 'No database')) return logging.Formatter.format(self, record)
def format(self, record): """ Add color to the message :param record: logging record instance :rtype: logging record formatted """ fg_color, bg_color = LEVEL_COLOR_MAPPING[record.levelno] record.levelname = COLOR_PATTERN % ( 30 + fg_color, 40 + bg_color, record.levelname) fg_color, bg_color = CYAN, DEFAULT record.database = COLOR_PATTERN % ( 30 + fg_color, 40 + bg_color, EnvironmentManager.get('db_name', 'No database')) return logging.Formatter.format(self, record)
def test_global_property(self): RegistryManager.declare_entry('Other') blok = 'newblok' RegistryManager.init_blok(blok) try: oldblok = EnvironmentManager.get('current_blok') EnvironmentManager.set('current_blok', blok) assert not RegistryManager.has_blok_property('myproperty') RegistryManager.add_or_replace_blok_property('myproperty', 2) assert RegistryManager.has_blok_property('myproperty') assert RegistryManager.get_blok_property('myproperty') == 2 RegistryManager.add_or_replace_blok_property('myproperty', 3) assert RegistryManager.get_blok_property('myproperty') == 3 RegistryManager.remove_blok_property('myproperty') assert not RegistryManager.has_blok_property('myproperty') finally: EnvironmentManager.set('current_blok', oldblok)
def test_global_property(self): RegistryManager.declare_entry('Other') blok = 'newblok' RegistryManager.init_blok(blok) try: oldblok = EnvironmentManager.get('current_blok') EnvironmentManager.set('current_blok', blok) self.assertEqual(RegistryManager.has_blok_property('myproperty'), False) RegistryManager.add_or_replace_blok_property('myproperty', 2) self.assertEqual(RegistryManager.has_blok_property('myproperty'), True) self.assertEqual(RegistryManager.get_blok_property('myproperty'), 2) RegistryManager.add_or_replace_blok_property('myproperty', 3) self.assertEqual(RegistryManager.get_blok_property('myproperty'), 3) RegistryManager.remove_blok_property('myproperty') self.assertEqual(RegistryManager.has_blok_property('myproperty'), False) finally: EnvironmentManager.set('current_blok', oldblok)
def test_global_property(self): RegistryManager.declare_entry('Other') blok = 'newblok' RegistryManager.init_blok(blok) try: oldblok = EnvironmentManager.get('current_blok') EnvironmentManager.set('current_blok', blok) self.assertEqual(RegistryManager.has_blok_property('myproperty'), False) RegistryManager.add_or_replace_blok_property('myproperty', 2) self.assertEqual( RegistryManager.has_blok_property('myproperty'), True) self.assertEqual( RegistryManager.get_blok_property('myproperty'), 2) RegistryManager.add_or_replace_blok_property('myproperty', 3) self.assertEqual( RegistryManager.get_blok_property('myproperty'), 3) RegistryManager.remove_blok_property('myproperty') self.assertEqual(RegistryManager.has_blok_property('myproperty'), False) finally: EnvironmentManager.set('current_blok', oldblok)
def run(cls, job_uuid=None): """dramatiq actor to execute a specific task""" autocommit = EnvironmentManager.get('job_autocommit', True) try: job = cls.query().filter(cls.uuid == job_uuid).one() job.status = cls.STATUS_RUNNING if autocommit: cls.registry.commit() # use to save the state job.task.run(job) if autocommit: cls.registry.commit() # use to save the state except Exception as e: logger.error(str(e)) cls.registry.rollback() job.status = cls.STATUS_FAILED job.error = str(e) if autocommit: cls.registry.commit() # use to save the state raise e
def __enter__(self): return EnvironmentManager.get( 'context', default=ImmutableContextDict({}))
def test_create_message_add_postcommit_hook(self): registry = self.init_registry(None) registry.upgrade(install=('test_dramatiq_1', )) self.assertFalse(EnvironmentManager.get('_postcommit_hook')) registry.Dramatiq.create_message(registry.Task.add, name='test') self.assertTrue(EnvironmentManager.get('_postcommit_hook'))
def reload_module_if_blok_is_reloading(module): from anyblok.environment import EnvironmentManager if EnvironmentManager.get('reload', default=False): reload_wraper(module)
def test_set_and_get_variable(self): db_name = 'test db name' EnvironmentManager.set('db_name', db_name) self.assertEqual(EnvironmentManager.get('db_name'), db_name)
def reload_module_if_blok_is_reloading(module): from anyblok.environment import EnvironmentManager if EnvironmentManager.get('reload', default=False): reload_module(module)
def __getattr__(self, key): context = EnvironmentManager.get( 'context', default=ImmutableContextDict({})) return getattr(context, key)
def wrap(self, *a, **kw): context = EnvironmentManager.get( 'context', default=ImmutableContextDict({})) return getattr(context, meth)(*a, **kw)