def __setstate__(self, state): if state.get('version', 1) > 1: raise ValueError( 'Job has version %s, but only version 1 can be handled' % state['version']) if 'id' in state: self.id = state['id'] try: if 'func' in state and state['func']: self.func_ref = state['func'] self.func = ref_to_obj(self.func_ref) if 'trigger' in state: self.trigger = state['trigger'] if 'executor' in state: self.executor = state['executor'] else: self.executor = 'default' if 'args' in state: self.args = state['args'] if 'kwargs' in state: self.kwargs = state['kwargs'] except Exception, e: #self._scheduler._logger.info("reason: %s\n" % str(e)) raise
def test_complex_path(self): pkg1 = ModuleType('pkg1') pkg1.pkg2 = 'blah' pkg2 = ModuleType('pkg1.pkg2') pkg2.varname = 'test' sys.modules['pkg1'] = pkg1 sys.modules['pkg1.pkg2'] = pkg2 assert ref_to_obj('pkg1.pkg2:varname') == 'test'
def _runJob(self, jobID, task): func = ref_to_obj(task.func) if self.conf.delegator: if self.conf.delegator(func, args=task.args, kwargs=task.kwargs): return with self._threadContext(): # TODO: ensure proper transaction handling with transaction.manager: func(*task.args or [], **task.kwargs or {})
def __init__(self, *args, **kwargs): if len(args) > 1 and hasattr(args[1], '__call__') or 'func' in kwargs: models.Model.__init__(self) Job.__init__(self, *args, **kwargs) else: models.Model.__init__(self, *args, **kwargs) self.instances = 0 self._lock = Lock() self.func = ref_to_obj(self.func_ref)
def __setstate__(self, state): if state.get('version', 1) > 1: raise ValueError( 'Got serialized data for version %s of %s, but only versions up to 1 can be ' 'handled' % (state['version'], self.__class__.__name__)) self.triggers = [] for clsref, state in state['triggers']: cls = ref_to_obj(clsref) trigger = cls.__new__(cls) trigger.__setstate__(state) self.triggers.append(trigger)
def __init__(self, trigger, func, args, kwargs, misfire_grace_time, coalesce, name=None, max_runs=None, max_instances=1): if not trigger: raise ValueError('The trigger must not be None') if not hasattr(args, '__getitem__'): raise TypeError('args must be a list-like object') if not hasattr(kwargs, '__getitem__'): raise TypeError('kwargs must be a dict-like object') if misfire_grace_time <= 0: raise ValueError('misfire_grace_time must be a positive value') if max_runs is not None and max_runs <= 0: raise ValueError('max_runs must be a positive value') if max_instances <= 0: raise ValueError('max_instances must be a positive value') if isinstance(func, str): self.func = ref_to_obj(func) self.func_ref = func elif callable(func): self.func = func try: self.func_ref = obj_to_ref(func) except ValueError: # If this happens, this Job won't be serializable self.func_ref = None else: raise TypeError( 'func must be a callable or a textual reference to one') self._lock = Lock() self.trigger = trigger self.args = args self.kwargs = kwargs self.name = to_unicode(name or get_callable_name(self.func)) self.misfire_grace_time = misfire_grace_time self.coalesce = coalesce self.max_runs = max_runs self.max_instances = max_instances self.runs = 0 self.instances = 0
def __setstate__(self, state): if state.get('version', 1) > 1: raise ValueError('Job has version %s, but only version 1 can be handled' % state['version']) self.id = state['id'] self.func_ref = state['func'] self.func = ref_to_obj(self.func_ref) self.trigger = state['trigger'] self.executor = state['executor'] self.args = state['args'] self.kwargs = state['kwargs'] self.name = state['name'] self.misfire_grace_time = state['misfire_grace_time'] self.coalesce = state['coalesce'] self.max_instances = state['max_instances'] self.next_run_time = state['next_run_time']
def _runJob(self, jobID, task): with self._threadContext(): # TODO: ensure proper transaction handling with transaction.manager: func = ref_to_obj(task.func) try: func(*task.args or [], **task.kwargs or {}) except Exception as e: exc_type, exc_value, exc_traceback = sys.exc_info() _traceback = traceback.format_exc() if hasattr(self, 'mail_conf'): self.sendErrorMail(_traceback) log.error('job ID "%s" (func : %s) failed : %s', jobID, task.func, e) else: raise Exception(e)
def __init__( self, trigger, func, args, kwargs, misfire_grace_time, coalesce, name=None, max_runs=None, max_instances=1 ): if not trigger: raise ValueError("The trigger must not be None") if not hasattr(args, "__getitem__"): raise TypeError("args must be a list-like object") if not hasattr(kwargs, "__getitem__"): raise TypeError("kwargs must be a dict-like object") if misfire_grace_time <= 0: raise ValueError("misfire_grace_time must be a positive value") if max_runs is not None and max_runs <= 0: raise ValueError("max_runs must be a positive value") if max_instances <= 0: raise ValueError("max_instances must be a positive value") if isinstance(func, str): self.func = ref_to_obj(func) self.func_ref = func elif callable(func): self.func = func try: self.func_ref = obj_to_ref(func) except ValueError: # If this happens, this Job won't be serializable self.func_ref = None else: raise TypeError("func must be a callable or a textual reference to one") self._lock = Lock() self.trigger = trigger self.args = args self.kwargs = kwargs self.name = to_unicode(name or get_callable_name(self.func)) self.misfire_grace_time = misfire_grace_time self.coalesce = coalesce self.max_runs = max_runs self.max_instances = max_instances self.runs = 0 self.instances = 0
def __setstate__(self, state): if state.get('version', 1) > 1: raise ValueError('Job has version %s, but only version 1 can be handled' % state['version']) if 'id' in state: self.id = state['id'] try: if 'func' in state and state['func']: self.func_ref = state['func'] self.func = ref_to_obj(self.func_ref) if 'trigger' in state: self.trigger = state['trigger'] if 'executor' in state: self.executor = state['executor'] else: self.executor = 'default' if 'args' in state: self.args = state['args'] if 'kwargs' in state: self.kwargs = state['kwargs'] except Exception,e: #self._scheduler._logger.info("reason: %s\n" % str(e)) raise
def _modify(self, **changes): """ Validates the changes to the Job and makes the modifications if and only if all of them validate. """ approved = {} if 'id' in changes: value = changes.pop('id') if not isinstance(value, six.string_types): raise TypeError("id must be a nonempty string") if hasattr(self, 'id'): raise ValueError('The job ID may not be changed') approved['id'] = value if 'func' in changes or 'args' in changes or 'kwargs' in changes: func = changes.pop('func') if 'func' in changes else self.func args = changes.pop('args') if 'args' in changes else self.args kwargs = changes.pop( 'kwargs') if 'kwargs' in changes else self.kwargs if isinstance(func, six.string_types): func_ref = func func = ref_to_obj(func) elif callable(func): try: func_ref = obj_to_ref(func) except ValueError: # If this happens, this Job won't be serializable func_ref = None else: raise TypeError( 'func must be a callable or a textual reference to one') if not hasattr(self, 'name') and changes.get('name', None) is None: changes['name'] = get_callable_name(func) if isinstance(args, six.string_types) or not isinstance(args, Iterable): raise TypeError('args must be a non-string iterable') if isinstance(kwargs, six.string_types) or not isinstance(kwargs, Mapping): raise TypeError('kwargs must be a dict-like object') check_callable_args(func, args, kwargs) approved['func'] = func approved['func_ref'] = func_ref approved['args'] = args approved['kwargs'] = kwargs if 'name' in changes: value = changes.pop('name') if not value or not isinstance(value, six.string_types): raise TypeError("name must be a nonempty string") approved['name'] = value if 'misfire_grace_time' in changes: value = changes.pop('misfire_grace_time') if value is not None and (not isinstance(value, six.integer_types) or value <= 0): raise TypeError( 'misfire_grace_time must be either None or a positive integer' ) approved['misfire_grace_time'] = value if 'coalesce' in changes: value = bool(changes.pop('coalesce')) approved['coalesce'] = value if 'max_instances' in changes: value = changes.pop('max_instances') if not isinstance(value, six.integer_types) or value <= 0: raise TypeError('max_instances must be a positive integer') approved['max_instances'] = value if 'trigger' in changes: trigger = changes.pop('trigger') if not isinstance(trigger, BaseTrigger): raise TypeError('Expected a trigger instance, got %s instead' % trigger.__class__.__name__) approved['trigger'] = trigger if 'executor' in changes: value = changes.pop('executor') if not isinstance(value, six.string_types): raise TypeError('executor must be a string') approved['executor'] = value if 'next_run_time' in changes: value = changes.pop('next_run_time') approved['next_run_time'] = convert_to_datetime( value, self._scheduler.timezone, 'next_run_time') if 'ip' in changes: value = changes.pop('ip') if not isinstance(value, six.string_types): raise TypeError('ip must be a string') approved['ip'] = value if changes: raise AttributeError( 'The following are not modifiable attributes of Job: %s' % ', '.join(changes)) for key, value in six.iteritems(approved): setattr(self, key, value)
def _runJob(self, jobID, task): with self._threadContext(): # TODO: ensure proper transaction handling with transaction.manager: func = ref_to_obj(task.func) func(*task.args or [], **task.kwargs or {})
def test_valid_ref(self): from logging.handlers import RotatingFileHandler assert ref_to_obj('logging.handlers:RotatingFileHandler') is RotatingFileHandler
def __setstate__(self, state): state["instances"] = 0 state["func"] = ref_to_obj(state.pop("func_ref")) state["_lock"] = Lock() self.__dict__ = state
def _modify(self, **changes): """Validates the changes to the Job and makes the modifications if and only if all of them validate.""" approved = {} if 'id' in changes: value = changes.pop('id') if not isinstance(value, six.string_types): raise TypeError("id must be a nonempty string") if hasattr(self, 'id'): raise ValueError('The job ID may not be changed') approved['id'] = value if 'func' in changes or 'args' in changes or 'kwargs' in changes: func = changes.pop('func') if 'func' in changes else self.func args = changes.pop('args') if 'args' in changes else self.args kwargs = changes.pop('kwargs') if 'kwargs' in changes else self.kwargs if isinstance(func, str): func_ref = func func = ref_to_obj(func) elif callable(func): try: func_ref = obj_to_ref(func) except ValueError: # If this happens, this Job won't be serializable func_ref = None else: raise TypeError('func must be a callable or a textual reference to one') if not hasattr(self, 'name') and changes.get('name', None) is None: changes['name'] = get_callable_name(func) if isinstance(args, six.string_types) or not isinstance(args, Iterable): raise TypeError('args must be a non-string iterable') if isinstance(kwargs, six.string_types) or not isinstance(kwargs, Mapping): raise TypeError('kwargs must be a dict-like object') check_callable_args(func, args, kwargs) approved['func'] = func approved['func_ref'] = func_ref approved['args'] = args approved['kwargs'] = kwargs if 'name' in changes: value = changes.pop('name') if not value or not isinstance(value, six.string_types): raise TypeError("name must be a nonempty string") approved['name'] = value if 'misfire_grace_time' in changes: value = changes.pop('misfire_grace_time') if value is not None and (not isinstance(value, six.integer_types) or value <= 0): raise TypeError('misfire_grace_time must be either None or a positive integer') approved['misfire_grace_time'] = value if 'coalesce' in changes: value = bool(changes.pop('coalesce')) approved['coalesce'] = value if 'max_instances' in changes: value = changes.pop('max_instances') if not isinstance(value, six.integer_types) or value <= 0: raise TypeError('max_instances must be a positive integer') approved['max_instances'] = value if 'trigger' in changes: trigger = changes.pop('trigger') if not isinstance(trigger, BaseTrigger): raise TypeError('Expected a trigger instance, got %s instead' % trigger.__class__.__name__) approved['trigger'] = trigger if 'executor' in changes: value = changes.pop('executor') if not isinstance(value, six.string_types): raise TypeError('executor must be a string') approved['executor'] = value if 'next_run_time' in changes: value = changes.pop('next_run_time') approved['next_run_time'] = convert_to_datetime(value, self._scheduler.timezone, 'next_run_time') if changes: raise AttributeError('The following are not modifiable attributes of Job: %s' % ', '.join(changes)) for key, value in six.iteritems(approved): setattr(self, key, value)
def __setstate__(self, state): state['instances'] = 0 state['func'] = ref_to_obj(state.pop('func_ref')) state['_lock'] = Lock() self.__dict__ = state
def test_valid_ref(self): from logging.handlers import RotatingFileHandler assert ref_to_obj( 'logging.handlers:RotatingFileHandler') is RotatingFileHandler
def sum_task(num1: int, num2: int): return num1 + num2 def multi_task(num1: int, num2: int): return num1 * num2 sum_task.description = "求和任务,两个参数,比如(1,3)" multi_task.description = "求积任务,两个参数,比如(4,5)" if __name__ == '__main__': from apscheduler.util import ref_to_obj res = ref_to_obj("tasks:test_task.multi_task") print(res)