def on_start(self): if not self._custom_logging and self.redirect_stdouts: self.app.log.redirect_stdouts(self.redirect_stdouts_level) WorkController.on_start(self) # this signal can be used to e.g. change queues after # the -Q option has been applied. signals.celeryd_after_setup.send(sender=self.hostname, instance=self, conf=self.app.conf) if not self.app.conf.value_set_for("accept_content"): warnings.warn(CDeprecationWarning(W_PICKLE_DEPRECATED)) if self.purge: self.purge_messages() # Dump configuration to screen so we have some basic information # for when users sends bug reports. print( safe_str( "".join( [ string(self.colored.cyan(" \n", self.startup_info())), string(self.colored.reset(self.extra_info() or "")), ] ) ), file=sys.__stdout__, ) self.set_process_status("-active-") self.install_platform_tweaks(self)
def on_start(self): app = self.app WorkController.on_start(self) # this signal can be used to e.g. change queues after # the -Q option has been applied. signals.celeryd_after_setup.send( sender=self.hostname, instance=self, conf=app.conf, ) if self.purge: self.purge_messages() # Dump configuration to screen so we have some basic information # for when users sends bug reports. use_image = self._term_supports_images() if use_image: self.termimage(static.logo_as_base64()) print(safe_str(''.join([ string(self.colored.cyan( ' \n', self.startup_info(artlines=not use_image))), string(self.colored.reset(self.extra_info() or '')), ])), file=sys.__stdout__) self.set_process_status('-active-') self.install_platform_tweaks(self) if not self._custom_logging and self.redirect_stdouts: app.log.redirect_stdouts(self.redirect_stdouts_level)
def on_start(self): if not self._custom_logging and self.redirect_stdouts: self.app.log.redirect_stdouts(self.redirect_stdouts_level) WorkController.on_start(self) # this signal can be used to e.g. change queues after # the -Q option has been applied. signals.celeryd_after_setup.send( sender=self.hostname, instance=self, conf=self.app.conf, ) if getattr(os, 'getuid', None) and os.getuid() == 0: accept_encoding = self.app.conf.CELERY_ACCEPT_CONTENT if ('pickle' in accept_encoding or 'application/x-python-serialize' in accept_encoding): if not C_FORCE_ROOT: raise RuntimeError(ROOT_DISALLOWED) warnings.warn(RuntimeWarning(ROOT_DISCOURAGED)) if not self.app.conf.value_set_for('CELERY_ACCEPT_CONTENT'): warnings.warn(CDeprecationWarning(W_PICKLE_DEPRECATED)) if self.purge: self.purge_messages() # Dump configuration to screen so we have some basic information # for when users sends bug reports. print(''.join([ string(self.colored.cyan(' \n', self.startup_info())), string(self.colored.reset(self.extra_info() or '')), ]), file=sys.__stdout__) self.set_process_status('-active-') self.install_platform_tweaks(self)
def emit_banner(self): # Dump configuration to screen so we have some basic information # for when users sends bug reports. use_image = term.supports_images() if use_image: print(term.imgcat(static.logo())) print(safe_str(''.join([ string(self.colored.cyan( ' \n', self.startup_info(artlines=not use_image))), string(self.colored.reset(self.extra_info() or '')), ])), file=sys.__stdout__)
def _fold_no_color(self, a, b): try: A = a.no_color() except AttributeError: A = string(a) try: B = b.no_color() except AttributeError: B = string(b) return ''.join((string(A), string(B)))
def _fold_no_color(self, a, b): try: A = a.no_color() except AttributeError: A = string(a) try: B = b.no_color() except AttributeError: B = string(b) return safe_str(safe_str(A) + safe_str(B))
def test_unicode(self): class X(object): def __unicode__(self): return "UNICODE" __str__ = __unicode__ def __repr__(self): return "REPR" x = Proxy(lambda: X()) self.assertEqual(string(x), "UNICODE") del (X.__unicode__) del (X.__str__) self.assertEqual(string(x), "REPR")
def on_failure(self, exc_info): """Handler called if the task raised an exception.""" task_ready(self) send_failed_event = True if not exc_info.internal: exc = exc_info.exception if isinstance(exc, Retry): return self.on_retry(exc_info) # These are special cases where the process would not have had # time to write the result. if self.store_errors: if isinstance(exc, WorkerLostError): self.task.backend.mark_as_failure( self.id, exc, request=self, ) elif isinstance(exc, Terminated): self._announce_revoked( 'terminated', True, string(exc), False) send_failed_event = False # already sent revoked event # (acks_late) acknowledge after result stored. if self.task.acks_late: self.acknowledge() self._log_error(exc_info, send_failed_event=send_failed_event)
def test_unicode(self): class X(object): def __unicode__(self): return 'UNICODE' __str__ = __unicode__ def __repr__(self): return 'REPR' x = Proxy(lambda: X()) self.assertEqual(string(x), 'UNICODE') del(X.__unicode__) del(X.__str__) self.assertEqual(string(x), 'REPR')
def test_unicode(self): @python_2_unicode_compatible class X(object): def __unicode__(self): return 'UNICODE' __str__ = __unicode__ def __repr__(self): return 'REPR' x = Proxy(lambda: X()) assert string(x) == 'UNICODE' del(X.__unicode__) del(X.__str__) assert string(x) == 'REPR'
def _index(self, id, body, **kwargs): return self.server.index( id=string(id), index=self.index, doc_type=self.doc_type, body=body, **kwargs )
def test_pylibmc_unicode_key(self): with mock.reset_modules('celery.backends.cache'): with self.mock_pylibmc(): from celery.backends import cache cache._imp = [None] task_id, result = string(uuid()), 42 b = cache.CacheBackend(backend='memcache', app=self.app) b.store_result(task_id, result, state=states.SUCCESS) self.assertEqual(b.get_result(task_id), result)
def test_pylibmc_unicode_key(self): with reset_modules('celery.backends.cache'): with self.mock_pylibmc(): from celery.backends import cache cache._imp = [None] task_id, result = string(uuid()), 42 b = cache.CacheBackend(backend='memcache', app=self.app) b.store_result(task_id, result, state=states.SUCCESS) self.assertEqual(b.get_result(task_id), result)
def pretty(self, n): OK = str(self.colored.green('OK')) if isinstance(n, list): return OK, self.pretty_list(n) if isinstance(n, dict): if 'ok' in n or 'error' in n: return self.pretty_dict_ok_error(n) if isinstance(n, string_t): return OK, string(n) return OK, pformat(n)
def prettify(self, n): OK = str(self.colored.green('OK')) if isinstance(n, list): return OK, self.prettify_list(n) if isinstance(n, dict): if 'ok' in n or 'error' in n: return self.prettify_dict_ok_error(n) if isinstance(n, string_t): return OK, string(n) return OK, pformat(n)
def on_failure(self, exc_info, send_failed_event=True, return_ok=False): """Handler called if the task raised an exception.""" task_ready(self) if isinstance(exc_info.exception, MemoryError): raise MemoryError('Process got: %s' % (exc_info.exception, )) elif isinstance(exc_info.exception, Reject): return self.reject(requeue=exc_info.exception.requeue) elif isinstance(exc_info.exception, Ignore): return self.acknowledge() exc = exc_info.exception if isinstance(exc, Retry): return self.on_retry(exc_info) # (acks_late) acknowledge after result stored. requeue = False if self.task.acks_late: reject = (self.task.reject_on_worker_lost and isinstance(exc, WorkerLostError)) ack = self.task.acks_on_failure_or_timeout if reject: requeue = not self.delivery_info.get('redelivered') self.reject(requeue=requeue) send_failed_event = False elif ack: self.acknowledge() # These are special cases where the process would not have had time # to write the result. if isinstance(exc, Terminated): self._announce_revoked('terminated', True, string(exc), False) send_failed_event = False # already sent revoked event elif not requeue and (isinstance(exc, WorkerLostError) or not return_ok): # only mark as failure if task has not been requeued self.task.backend.mark_as_failure( self.id, exc, request=self._context, store_result=self.store_errors, ) if send_failed_event: self.send_event( 'task-failed', exception=safe_repr(get_pickled_exception(exc_info.exception)), traceback=exc_info.traceback, ) if not return_ok: error('Task handler raised error: %r', exc, exc_info=exc_info.exc_info)
def test_memcache_unicode_key(self): with self.mock_memcache(): with reset_modules("celery.backends.cache"): with mask_modules("pylibmc"): from celery.backends import cache cache._imp = [None] task_id, result = string(uuid()), 42 b = cache.CacheBackend(backend="memcache") b.store_result(task_id, result, status=states.SUCCESS) self.assertEqual(b.get_result(task_id), result)
def emit_banner(self): # Dump configuration to screen so we have some basic information # for when users sends bug reports. use_image = term.supports_images() if use_image: print(term.imgcat(static.logo())) print( safe_str( "".join( [ string( self.colored.cyan( " \n", self.startup_info(artlines=not use_image) ) ), string(self.colored.reset(self.extra_info() or "")), ] ) ), file=sys.__stdout__, )
def pretty(self, n): OK = str(self.colored.green('OK')) if isinstance(n, list): return OK, self.pretty_list(n) if isinstance(n, dict): if 'ok' in n or 'error' in n: return self.pretty_dict_ok_error(n) else: return OK, json.dumps(n, sort_keys=True, indent=4) if isinstance(n, string_t): return OK, string(n) return OK, pformat(n)
def on_failure(self, exc_info, send_failed_event=True, return_ok=False): """Handler called if the task raised an exception.""" task_ready(self) if isinstance(exc_info.exception, MemoryError): raise MemoryError('Process got: %s' % (exc_info.exception,)) elif isinstance(exc_info.exception, Reject): return self.reject(requeue=exc_info.exception.requeue) elif isinstance(exc_info.exception, Ignore): return self.acknowledge() exc = exc_info.exception if isinstance(exc, Retry): return self.on_retry(exc_info) # These are special cases where the process wouldn't've had # time to write the result. if isinstance(exc, Terminated): self._announce_revoked( 'terminated', True, string(exc), False) send_failed_event = False # already sent revoked event elif isinstance(exc, WorkerLostError) or not return_ok: self.task.backend.mark_as_failure( self.id, exc, request=self._context, store_result=self.store_errors, ) # (acks_late) acknowledge after result stored. if self.task.acks_late: reject = ( self.task.reject_on_worker_lost and isinstance(exc, WorkerLostError) ) ack = self.task.acks_on_failure_or_timeout if reject: requeue = not self.delivery_info.get('redelivered') self.reject(requeue=requeue) send_failed_event = False elif ack: self.acknowledge() if send_failed_event: self.send_event( 'task-failed', exception=safe_repr(get_pickled_exception(exc_info.exception)), traceback=exc_info.traceback, ) if not return_ok: error('Task handler raised error: %r', exc, exc_info=exc_info.exc_info)
def on_start(self): app = self.app if not self._custom_logging and self.redirect_stdouts: app.log.redirect_stdouts(self.redirect_stdouts_level) WorkController.on_start(self) # this signal can be used to e.g. change queues after # the -Q option has been applied. signals.celeryd_after_setup.send( sender=self.hostname, instance=self, conf=app.conf, ) if self.purge: self.purge_messages() # Dump configuration to screen so we have some basic information # for when users sends bug reports. print(safe_str(''.join([ string(self.colored.cyan(' \n', self.startup_info())), string(self.colored.reset(self.extra_info() or '')), ])), file=sys.__stdout__) self.set_process_status('-active-') self.install_platform_tweaks(self)
def test_index(self): x = ElasticsearchBackend(app=self.app) x.doc_type = 'test-doc-type' x._server = Mock() x._server.index = Mock() expected_result = dict(_id=sentinel.task_id, _source={'result': sentinel.result}) x._server.index.return_value = expected_result body = {"field1": "value1"} x._index(id=sentinel.task_id, body=body, kwarg1='test1') x._server.index.assert_called_once_with(id=string(sentinel.task_id), doc_type=x.doc_type, index=x.index, body=body, kwarg1='test1')
def startup_info(self): app = self.app concurrency = string(self.concurrency) appr = '{0}:{1:#x}'.format(app.main or '__main__', id(app)) if not isinstance(app.loader, AppLoader): loader = qualname(app.loader) if loader.startswith('celery.loaders'): # pragma: no cover loader = loader[14:] appr += ' ({0})'.format(loader) if self.autoscale: max, min = self.autoscale concurrency = '{{min={0}, max={1}}}'.format(min, max) pool = self.pool_cls if not isinstance(pool, string_t): pool = pool.__module__ concurrency += ' ({0})'.format(pool.split('.')[-1]) events = 'ON' if not self.send_events: events = 'OFF (enable -E to monitor this worker)' banner = BANNER.format( app=appr, hostname=safe_str(self.hostname), timestamp=datetime.now().replace(microsecond=0), version=VERSION_BANNER, conninfo=self.app.connection().as_uri(), results=maybe_sanitize_url( self.app.conf.result_backend or 'disabled', ), concurrency=concurrency, platform=safe_str(_platform.platform()), events=events, queues=app.amqp.queues.format(indent=0, indent_first=False), ).splitlines() # integrate the ASCII art. for i, x in enumerate(banner): try: banner[i] = ' '.join([ARTLINES[i], banner[i]]) except IndexError: banner[i] = ' ' * 16 + banner[i] return '\n'.join(banner) + '\n'
def test_index(self): x = ElasticsearchBackend(app=self.app) x.doc_type = 'test-doc-type' x._server = Mock() x._server.index = Mock() expected_result = dict( _id=sentinel.task_id, _source={'result': sentinel.result} ) x._server.index.return_value = expected_result body = {"field1": "value1"} x._index(id=sentinel.task_id, body=body, kwarg1='test1') x._server.index.assert_called_once_with( id=string(sentinel.task_id), doc_type=x.doc_type, index=x.index, body=body, kwarg1='test1' )
def startup_info(self, artlines=True): app = self.app concurrency = string(self.concurrency) appr = "{0}:{1:#x}".format(app.main or "__main__", id(app)) if not isinstance(app.loader, AppLoader): loader = qualname(app.loader) if loader.startswith("celery.loaders"): # pragma: no cover loader = loader[14:] appr += " ({0})".format(loader) if self.autoscale: max, min = self.autoscale concurrency = "{{min={0}, max={1}}}".format(min, max) pool = self.pool_cls if not isinstance(pool, string_t): pool = pool.__module__ concurrency += " ({0})".format(pool.split(".")[-1]) events = "ON" if not self.task_events: events = "OFF (enable -E to monitor tasks in this worker)" banner = BANNER.format( app=appr, hostname=safe_str(self.hostname), timestamp=datetime.now().replace(microsecond=0), version=VERSION_BANNER, conninfo=self.app.connection().as_uri(), results=self.app.backend.as_uri(), concurrency=concurrency, platform=safe_str(_platform.platform()), events=events, queues=app.amqp.queues.format(indent=0, indent_first=False), ).splitlines() # integrate the ASCII art. if artlines: for i, _ in enumerate(banner): try: banner[i] = " ".join([ARTLINES[i], banner[i]]) except IndexError: banner[i] = " " * 16 + banner[i] return "\n".join(banner) + "\n"
def startup_info(self): app = self.app concurrency = string(self.concurrency) appr = '{0}:{1:#x}'.format(app.main or '__main__', id(app)) if not isinstance(app.loader, AppLoader): loader = qualname(app.loader) if loader.startswith('celery.loaders'): # pragma: no cover loader = loader[14:] appr += ' ({0})'.format(loader) if self.autoscale: max, min = self.autoscale concurrency = '{{min={0}, max={1}}}'.format(min, max) pool = self.pool_cls if not isinstance(pool, string_t): pool = pool.__module__ concurrency += ' ({0})'.format(pool.split('.')[-1]) events = 'ON' if not self.send_events: events = 'OFF (enable -E to monitor this worker)' banner = BANNER.format( app=appr, hostname=safe_str(self.hostname), version=VERSION_BANNER, conninfo=self.app.connection().as_uri(), results=maybe_sanitize_url( self.app.conf.result_backend or 'disabled', ), concurrency=concurrency, platform=safe_str(_platform.platform()), events=events, queues=app.amqp.queues.format(indent=0, indent_first=False), ).splitlines() # integrate the ASCII art. for i, x in enumerate(banner): try: banner[i] = ' '.join([ARTLINES[i], banner[i]]) except IndexError: banner[i] = ' ' * 16 + banner[i] return '\n'.join(banner) + '\n'
def test_set(self): self.backend._client = MagicMock() self.backend._client.put_item = MagicMock() # should return None with patch('celery.backends.dynamodb.time', self._mock_time): assert self.backend.set(sentinel.key, sentinel.value) is None assert self.backend._client.put_item.call_count == 1 _, call_kwargs = self.backend._client.put_item.call_args expected_kwargs = { 'Item': { u'timestamp': {u'N': str(self._static_timestamp)}, u'id': {u'S': string(sentinel.key)}, u'result': {u'B': sentinel.value} }, 'TableName': 'celery' } assert call_kwargs['Item'] == expected_kwargs['Item'] assert call_kwargs['TableName'] == 'celery'
def startup_info(self): app = self.app concurrency = string(self.concurrency) appr = "{0}:{1:#x}".format(app.main or "__main__", id(app)) if not isinstance(app.loader, AppLoader): loader = qualname(app.loader) if loader.startswith("celery.loaders"): loader = loader[14:] appr += " ({0})".format(loader) if self.autoscale: max, min = self.autoscale concurrency = "{{min={0}, max={1}}}".format(min, max) pool = self.pool_cls if not isinstance(pool, string_t): pool = pool.__module__ concurrency += " ({0})".format(pool.split(".")[-1]) events = "ON" if not self.send_events: events = "OFF (enable -E to monitor this worker)" banner = BANNER.format( app=appr, hostname=safe_str(self.hostname), version=VERSION_BANNER, conninfo=self.app.connection().as_uri(), results=maybe_sanitize_url(self.app.conf.result_backend or "disabled"), concurrency=concurrency, platform=safe_str(_platform.platform()), events=events, queues=app.amqp.queues.format(indent=0, indent_first=False), ).splitlines() # integrate the ASCII art. for i, x in enumerate(banner): try: banner[i] = " ".join([ARTLINES[i], banner[i]]) except IndexError: banner[i] = " " * 16 + banner[i] return "\n".join(banner) + "\n"
def startup_info(self, artlines=True): app = self.app concurrency = string(self.concurrency) appr = '{0}:{1:#x}'.format(app.main or '__main__', id(app)) if not isinstance(app.loader, AppLoader): loader = qualname(app.loader) if loader.startswith('celery.loaders'): # pragma: no cover loader = loader[14:] appr += ' ({0})'.format(loader) pool = self.pool_cls if not isinstance(pool, string_t): pool = pool.__module__ concurrency += ' ({0})'.format(pool.split('.')[-1]) events = 'ON' if not self.task_events: events = 'OFF (enable -E to monitor tasks in this worker)' banner = BANNER.format( app=appr, hostname=safe_str(self.hostname), timestamp=datetime.now().replace(microsecond=0), version=VERSION_BANNER, conninfo=self.app.connection().as_uri(), results=self.app.backend.as_uri(), concurrency=concurrency, platform=safe_str(_platform.platform()), events=events, queues=app.amqp.queues.format(indent=0, indent_first=False), ).splitlines() # integrate the ASCII art. if artlines: for i, x in enumerate(banner): try: banner[i] = ' '.join([ARTLINES[i], banner[i]]) except IndexError: banner[i] = ' ' * 16 + banner[i] return '\n'.join(banner) + '\n'
def on_failure(self, exc_info, send_failed_event=True, return_ok=False): """Handler called if the task raised an exception.""" task_ready(self) if isinstance(exc_info.exception, MemoryError): raise MemoryError("Process got: %s" % (exc_info.exception,)) elif isinstance(exc_info.exception, Reject): return self.reject(requeue=exc_info.exception.requeue) elif isinstance(exc_info.exception, Ignore): return self.acknowledge() exc = exc_info.exception if isinstance(exc, Retry): return self.on_retry(exc_info) # These are special cases where the process would not have had # time to write the result. if self.store_errors: if isinstance(exc, Terminated): self._announce_revoked("terminated", True, string(exc), False) send_failed_event = False # already sent revoked event elif isinstance(exc, WorkerLostError) or not return_ok: self.task.backend.mark_as_failure(self.id, exc, request=self) # (acks_late) acknowledge after result stored. if self.task.acks_late: self.acknowledge() if send_failed_event: self.send_event( "task-failed", exception=safe_repr(get_pickled_exception(exc_info.exception)), traceback=exc_info.traceback, ) if not return_ok: error("Task handler raised error: %r", exc, exc_info=exc_info.exc_info)
def no_color(self): if self.s: return string(reduce(self._fold_no_color, self.s)) return ''
def embed(self): prefix = '' if self.enabled: prefix = self.op return ''.join((string(prefix), string(reduce(self._add, self.s))))
def __add__(self, other): return string(self) + string(other)
def _add(self, a, b): return string(a) + string(b)
def strtotal(self): if not self.total_apx: return '?' return string(self.total_apx)
def set(self, key, value): key = string(key) request_parameters = self._prepare_put_request(key, value) self.client.put_item(**request_parameters)
def __unicode__(self): try: return string(self._get_current_object()) except RuntimeError: # pragma: no cover return repr(self)
def __unicode__(self): suffix = '' if self.enabled: suffix = RESET_SEQ return string(''.join((self.embed(), string(suffix))))
def get(self, key): key = string(key) request_parameters = self._prepare_get_request(key) item_response = self.client.get_item(**request_parameters) item = self._item_to_dict(item_response) return item.get(self._value_field.name)
def delete(self, key): key = string(key) request_parameters = self._prepare_get_request(key) self.client.delete_item(**request_parameters)
def __str__(self): suffix = '' if self.enabled: suffix = RESET_SEQ return string(''.join((self.embed(), string(suffix))))
def _index(self, id, body, **kwargs): return self.server.index(id=string(id), index=self.index, doc_type=self.doc_type, body=body, **kwargs)
def prepare_exception(self, exc, serializer=None): """Prepare exception for serialization.""" serializer = self.serializer if serializer is None else serializer if serializer in EXCEPTION_ABLE_CODECS: return get_pickleable_exception(exc) return {'exc_type': type(exc).__name__, 'exc_message': string(exc)}