Flask SQLAlchemy KeyError 'False' in create_engine - python

I have a flask app that connects with mysql using sqlalchemy. A strange error happens on DB query.
`Traceback (most recent call last):
File "/home/Desktop/work/nesting-app/env/lib/python3.8/site-packages/sqlalchemy/util/_collections.py", line 1008, in call
return self.registry[key]
KeyError: <greenlet.greenlet object at 0x7f22a0936a90 (otid=0x7f22a08ef100) current active started main>
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File
"/home/Desktop/work/nesting-app/server/users/routes.py", line
28, in register_new_user
if form.validate_on_submit(): File "/home/Desktop/work/nesting-app/env/lib/python3.8/site-packages/flask_wtf/form.py",
line 100, in validate_on_submit
return self.is_submitted() and self.validate() File "/home/Desktop/work/nesting-app/env/lib/python3.8/site-packages/wtforms/form.py",
line 318, in validate
return super(Form, self).validate(extra) File "/home/Desktop/work/nesting-app/env/lib/python3.8/site-packages/wtforms/form.py",
line 150, in validate
if not field.validate(self, extra): File "/home/Desktop/work/nesting-app/env/lib/python3.8/site-packages/wtforms/fields/core.py",
line 226, in validate
stop_validation = self._run_validation_chain(form, chain) File "/home/Desktop/work/nesting-app/env/lib/python3.8/site-packages/wtforms/fields/core.py",
line 246, in _run_validation_chain
validator(form, self) File "/home/Desktop/work/nesting-app/server/users/forms.py", line
43, in validate_email
user = User.query.filter_by(email=email.data).first() File "/home/Desktop/work/nesting-app/env/lib/python3.8/site-packages/flask_sqlalchemy/init.py",
line 552, in get
return type.query_class(mapper, session=self.sa.session()) File "/home/Desktop/work/nesting-app/env/lib/python3.8/site-packages/sqlalchemy/orm/scoping.py",
line 47, in call
sess = self.registry() File "/home/Desktop/work/nesting-app/env/lib/python3.8/site-packages/sqlalchemy/util/collections.py", line 1010, in call
return self.registry.setdefault(key, self.createfunc()) File "/home/Desktop/work/nesting-app/env/lib/python3.8/site-packages/sqlalchemy/orm/session.py",
line 4171, in call
return self.class(**local_kw) File "/home/Desktop/work/nesting-app/env/lib/python3.8/site-packages/flask_sqlalchemy/init.py",
line 176, in init
bind = options.pop('bind', None) or db.engine File "/home/Desktop/work/nesting-app/env/lib/python3.8/site-packages/flask_sqlalchemy/init.py",
line 998, in engine
return self.get_engine() File "/home/Desktop/work/nesting-app/env/lib/python3.8/site-packages/flask_sqlalchemy/init.py",
line 1017, in get_engine
return connector.get_engine() File "/home/Desktop/work/nesting-app/env/lib/python3.8/site-packages/flask_sqlalchemy/init.py",
line 594, in get_engine
self._engine = rv = self._sa.create_engine(sa_url, options) File "/home/Desktop/work/nesting-app/env/lib/python3.8/site-packages/flask_sqlalchemy/init.py",
line 1027, in create_engine
return sqlalchemy.create_engine(sa_url, **engine_opts) File "", line 2, in create_engine File
"/home/Desktop/work/nesting-app/env/lib/python3.8/site-packages/sqlalchemy/util/deprecations.py", line 298, in warned
return fn(*args, **kwargs) File "/home/Desktop/work/nesting-app/env/lib/python3.8/site-packages/sqlalchemy/engine/create.py",
line 661, in create_engine
engine = engineclass(pool, dialect, u, **engine_args) File "/home/Desktop/work/nesting-app/env/lib/python3.8/site-packages/sqlalchemy/engine/base.py",
line 2758, in init
self.echo = echo File "/home/Desktop/work/nesting-app/env/lib/python3.8/site-packages/sqlalchemy/log.py",
line 225, in set
instance_logger(instance, echoflag=value) File "/home/Desktop/work/nesting-app/env/lib/python3.8/site-packages/sqlalchemy/log.py",
line 202, in instance_logger
logger = InstanceLogger(echoflag, name) File "/home/Desktop/work/nesting-app/env/lib/python3.8/site-packages/sqlalchemy/log.py",
line 103, in init
if self._echo_map[echo] <= logging.INFO and not self.logger.handlers: KeyError: 'False' `
Here is the config.py
from os import environ, path
from dotenv import load_dotenv
from pathlib import Path
BASE_DIR = Path(__file__).resolve().parent.parent
load_dotenv(path.join(BASE_DIR, '.env'))
class Config:
SECRET_KEY = environ.get('SECRET_KEY')
SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://user:password#127.0.0.1:3306/dbname'
# SQLALCHEMY_ECHO: When set to 'True', Flask-SQLAlchemy will log all database
# activity to Python's stderr for debugging purposes.
SQLALCHEMY_ECHO = environ.get('SQLALCHEMY_ECHO')
# To suppress the warning "this option takes a lot of system resources" set
SQLALCHEMY_TRACK_MODIFICATIONS = environ.get('SQLALCHEMY_TRACK_MODIFICATIONS')
Here is the init.py
db = SQLAlchemy()
bcrypt = Bcrypt()
login_manager = LoginManager()
login_manager.login_view = 'users.login'
mail = Mail()
csrf = CSRFProtect()
def create_app(config_class=Config):
flask_app = Flask(__name__)
flask_app.config.from_object(config_class)
db.init_app(flask_app)
bcrypt.init_app(flask_app)
login_manager.init_app(flask_app)
mail.init_app(flask_app)
csrf.init_app(flask_app)
I have tried with different connection strings:
SQLALCHEMY_DATABASE_URI=mysql+mysqlconnector://user:pass#localhost:3306/dbname
SQLALCHEMY_DATABASE_URI=mysql+mysqlconnector://user:pass#localhost/dbname
SQLALCHEMY_DATABASE_URI=mysql+mysqlconnector://user:pass#127.0.0.1:3306/dbname
Thank you for any help.

The echo flag for SQLAlchemy's create_engine function accepts a limited set of values: None, False, True, "debug" (source).
The traceback shows that the string "False" is being passed: KeyError: 'False'. In fact, the error can be reproduced by passing any string (except "debug") as the value of create_engine's echo flag:
create_engine('sqlite://', echo='banana')
results in
Traceback (most recent call last):
...
KeyError: 'banana'
At a guess, the problem is that
SQLALCHEMY_ECHO = environ.get('SQLALCHEMY_ECHO')
does not consider that environment variables are always strings. Something like this might be better:
SQLALCHEMY_ECHO = environ.get('SQLALCHEMY_ECHO') in ('1', 'True')
as it will result in a boolean value being assigned.

Related

KeyError: <weakref at 0x7fc9e8267ad0; to 'Flask' at 0x7fc9e9ec5750>

I've been having a hard time handling sessions in flask. Since when I manage the application in the local environment everything works perfectly, including flask sessions. But when i already host it in Render i always get this error in every route.
[55] [ERROR] Error handling request /valle-de-guadalupe
Traceback (most recent call last):
File "/opt/render/project/src/.venv/lib/python3.7/site-packages/flask/app.py", line 2525, in wsgi_app
response = self.full_dispatch_request()
File "/opt/render/project/src/.venv/lib/python3.7/site-packages/flask/app.py", line 1822, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/opt/render/project/src/.venv/lib/python3.7/site-packages/flask/app.py", line 1820, in full_dispatch_request
rv = self.dispatch_request()
File "/opt/render/project/src/.venv/lib/python3.7/site-packages/flask/app.py", line 1796, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
File "/opt/render/project/src/app_folder/routes/public.py", line 35, in valle_de_guadalupe
return render_template("public/cities/valle_guadalupe.html")
File "/opt/render/project/src/.venv/lib/python3.7/site-packages/flask/templating.py", line 147, in render_template
return _render(app, template, context)
File "/opt/render/project/src/.venv/lib/python3.7/site-packages/flask/templating.py", line 128, in _render
app.update_template_context(context)
File "/opt/render/project/src/.venv/lib/python3.7/site-packages/flask/app.py", line 994, in update_template_context
context.update(func())
File "/opt/render/project/src/.venv/lib/python3.7/site-packages/flask_login/utils.py", line 407, in _user_context_processor
return dict(current_user=_get_user())
File "/opt/render/project/src/.venv/lib/python3.7/site-packages/flask_login/utils.py", line 372, in _get_user
current_app.login_manager._load_user()
File "/opt/render/project/src/.venv/lib/python3.7/site-packages/flask_login/login_manager.py", line 364, in _load_user
user = self._user_callback(user_id)
File "/opt/render/project/src/app.py", line 52, in load_user
return User.get_by_id(int(user_id))
File "/opt/render/project/src/app_folder/models/models.py", line 82, in get_by_id
return User.query.get(id)
File "<string>", line 2, in get
File "/opt/render/project/src/.venv/lib/python3.7/site-packages/sqlalchemy/util/deprecations.py", line 402, in warned
return fn(*args, **kwargs)
File "/opt/render/project/src/.venv/lib/python3.7/site-packages/sqlalchemy/orm/query.py", line 947, in get
return self._get_impl(ident, loading.load_on_pk_identity)
File "/opt/render/project/src/.venv/lib/python3.7/site-packages/sqlalchemy/orm/query.py", line 959, in _get_impl
execution_options=self._execution_options,
File "/opt/render/project/src/.venv/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 2959, in _get_impl
load_options=load_options,
File "/opt/render/project/src/.venv/lib/python3.7/site-packages/sqlalchemy/orm/loading.py", line 534, in load_on_pk_identity
bind_arguments=bind_arguments,
File "/opt/render/project/src/.venv/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 1702, in execute
bind = self.get_bind(**bind_arguments)
File "/opt/render/project/src/.venv/lib/python3.7/site-packages/flask_sqlalchemy/session.py", line 61, in get_bind
engines = self._db.engines
File "/opt/render/project/src/.venv/lib/python3.7/site-packages/flask_sqlalchemy/extension.py", line 629, in engines
return self._app_engines[app]
File "/usr/local/lib/python3.7/weakref.py", line 396, in __getitem__
return self.data[ref(key)]
KeyError: <weakref at 0x7fc9e8267ad0; to 'Flask' at 0x7fc9e9ec5750>
index.py
from app import app
from app_folder.utils.db import db
db.init_app(app)
with app.app_context():
db.create_all()
if __name__ == "__main__":
app.run(
debug = False,
port = 5000
)
app.py
from flask import Flask
"""Flask SqlAlchemy"""
from flask_sqlalchemy import SQLAlchemy
"""Flask Login"""
from flask_login import LoginManager
"""Dot Env"""
from dotenv import load_dotenv
"""App Folder Routes"""
from app_folder.handlers.stripe_handlers import stripe_error
from app_folder.handlers.web_handlers import web_error
from app_folder.models.models import User
from app_folder.routes.admin import admin
from app_folder.routes.public import public
from app_folder.routes.users import users
from app_folder.utils.db import db
"""Imports"""
import os
import stripe
load_dotenv()
"""config app"""
app = Flask(__name__,
static_url_path="",
template_folder="app_folder/templates",
static_folder="app_folder/static")
app.config['SECRET_KEY'] = os.getenv("SECRET_KEY")
app.config['SQLALCHEMY_DATABASE_URI'] = os.getenv("SQLALCHEMY_DATABASE_VERSION")+os.getenv("SQLALCHEMY_USERNAME")+":"+os.getenv("SQLALCHEMY_PASSWORD")+"#"+os.getenv("SQLALCHEMY_SERVER")+"/"+os.getenv("SQLALCHEMY_DATABASE")
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = os.getenv("SQLALCHEMY_TRACK_MODIFICATIONS")
"""blueprints"""
app.register_blueprint(stripe_error)
app.register_blueprint(web_error)
app.register_blueprint(admin)
app.register_blueprint(public)
app.register_blueprint(users)
SQLAlchemy(app)
login_manager = LoginManager(app)
""" stripe """
stripe_keys = {
'secret_key': os.getenv("STRIPE_SECRET_KEY"),
'publishable_key': os.getenv("STRIPE_PUBLISHABLE_KEY")
}
stripe.api_key = stripe_keys['secret_key']
"""Login Manager"""
#login_manager.user_loader
def load_user(user_id):
return User.get_by_id(int(user_id))
"""Teardown"""
#app.teardown_appcontext
def shutdown_session(exception=None):
db.session.remove()
Regardless of which route i'm on, while handling sessions I get the same error, but in this case use this path.
public.py
"""routes"""
#public.route("/", methods=["GET", "POST"])
def index():
return redirect(url_for('public.valle_de_guadalupe'))
"""cities"""
#public.route("/valle-de-guadalupe", methods=["GET", "POST"])
def valle_de_guadalupe():
return render_template("public/cities/valle_guadalupe.html")
I don't know if this has happened to someone else.
It's usually best to remove as many extra components and identify a minimal example that produces the error, otherwise it's often hard to help.
That said, I think that error suggests that db (SQLAlachemy() from flask-sqlalchemy) is not being inited with app.
I'm not sure what the db is that is being imported from app_folder.utils.db, but it appears that you may need to call db.init_app(app).
Relatedly, the line SQLAlchemy(app) is not being assigned. Perhaps you meant to assign that to db?
Other people report that this issue happens since the upgrade from flask-sqlalchemy v2.5.1 to v3. So downgrading might be a work around (not a permanent solution)
Source

How to get list of events using the Python kubernetes API?

I am trying to obtain the list of events from a minikube cluster usigh the Python Kubernetes api using the following code:
from kubernetes import config, client
config.load_kube_config()
api = client.EventsV1beta1Api()
print(api.list_event_for_all_namespaces())
I am getting the following error:
C:\Users\lameg\kubesense>python test.py
Traceback (most recent call last):
File "C:\Users\lameg\kubesense\test.py", line 6, in <module>
print(api.list_event_for_all_namespaces())
File "C:\Users\lameg\miniforge3\lib\site-packages\kubernetes\client\api\events_v1beta1_api.py", line 651, in list_event_for_all_namespaces
return self.list_event_for_all_namespaces_with_http_info(**kwargs) # noqa: E501
File "C:\Users\lameg\miniforge3\lib\site-packages\kubernetes\client\api\events_v1beta1_api.py", line 758, in list_event_for_all_namespaces_with_http_info
return self.api_client.call_api(
File "C:\Users\lameg\miniforge3\lib\site-packages\kubernetes\client\api_client.py", line 348, in call_api
return self.__call_api(resource_path, method,
File "C:\Users\lameg\miniforge3\lib\site-packages\kubernetes\client\api_client.py", line 192, in __call_api
return_data = self.deserialize(response_data, response_type)
File "C:\Users\lameg\miniforge3\lib\site-packages\kubernetes\client\api_client.py", line 264, in deserialize
return self.__deserialize(data, response_type)
File "C:\Users\lameg\miniforge3\lib\site-packages\kubernetes\client\api_client.py", line 303, in __deserialize
return self.__deserialize_model(data, klass)
File "C:\Users\lameg\miniforge3\lib\site-packages\kubernetes\client\api_client.py", line 639, in __deserialize_model
kwargs[attr] = self.__deserialize(value, attr_type)
File "C:\Users\lameg\miniforge3\lib\site-packages\kubernetes\client\api_client.py", line 280, in __deserialize
return [self.__deserialize(sub_data, sub_kls)
File "C:\Users\lameg\miniforge3\lib\site-packages\kubernetes\client\api_client.py", line 280, in <listcomp>
return [self.__deserialize(sub_data, sub_kls)
File "C:\Users\lameg\miniforge3\lib\site-packages\kubernetes\client\api_client.py", line 303, in __deserialize
return self.__deserialize_model(data, klass)
File "C:\Users\lameg\miniforge3\lib\site-packages\kubernetes\client\api_client.py", line 641, in __deserialize_model
instance = klass(**kwargs)
File "C:\Users\lameg\miniforge3\lib\site-packages\kubernetes\client\models\v1beta1_event.py", line 112, in __init__
self.event_time = event_time
File "C:\Users\lameg\miniforge3\lib\site-packages\kubernetes\client\models\v1beta1_event.py", line 291, in event_time
raise ValueError("Invalid value for `event_time`, must not be `None`") # noqa: E501
ValueError: Invalid value for `event_time`, must not be `None`
Any ideas ?
That looks like either a bug in the Python client, or a bug in the OpenAPI specification used to generate the client: clearly, null is a value for eventTime that is supported by the API.
I think the only workaround is to monkey-patch the kubernetes.client module so that it accepts null values. Something like this:
from kubernetes import config, client
config.load_kube_config()
api = client.EventsV1beta1Api()
# This is descriptor, see https://docs.python.org/3/howto/descriptor.html
class FakeEventTime:
def __get__(self, obj, objtype=None):
return obj._event_time
def __set__(self, obj, value):
obj._event_time = value
# Monkey-patch the `event_time` attribute of ` the V1beta1Event class.
client.V1beta1Event.event_time = FakeEventTime()
# Now this works.
events = api.list_event_for_all_namespaces()
The above code runs successfully against my OpenShift instance, whereas previously it would fail as you describe in your question.

Error on writing to Google cloud spanner using Google cloud functions

I am trying to insert data into cloud spanner table using cloud functions but it is throwing the error given below.Reading data from cloud spanner is working properly but writing using both the Data Definition Language commands and batch.insert method both throws the same error. I am thinking its some kind of permissions problem! I don't know how to fix it?
Requirements file contains only google-cloud-spanner==1.7.1
Code running in cloud functions
import json
from google.cloud import spanner
INSTANCE_ID = 'AARISTA'
DATABASE_ID = 'main'
TABLE_NAME = 'userinfo'
dataDict = None
def new_user(request):
dataDict = json.loads(request.data)# Data is available in dict format
if dataDict['USER_ID']==None:
return "User id empty"
elif dataDict['IMEI'] == None:
return "Imei number empty"
elif dataDict['DEVICE_ID'] == None:
return "Device ID empty"
elif dataDict['NAME'] == None:
return "Name field is empty"
elif dataDict['VIRTUAL_PRIVATE_KEY']== None:
return "User's private key cant be empty"
else:
return insert_data(INSTANCE_ID,DATABASE_ID)
def insert_data(instance_id, database_id):
spanner_client = spanner.Client()
instance = spanner_client.instance(instance_id)
database = instance.database(database_id)
def insert_user(transcation):
row_ct= transcation.execute_update("INSERT userinfo
(USER_ID,DEVICE_ID,IMEI,NAME,VIRTUAL_PRIVATE_KEY) VALUES"
"("+dataDict['USER_ID']+',
'+dataDict['DEVICE_ID']+', '+ dataDict['IMEI']+',
'+dataDict['NAME']+',
'+dataDict['VIRTUAL_PRIVATE_KEY']+")")
database.run_in_transaction(insert_user)
return 'Inserted data.'
Error logs on Cloud Functions
Traceback (most recent call last):
File "/env/local/lib/python3.7/site-packages/google/cloud/spanner_v1/pool.py", line 265, in get session = self._sessions.get_nowait()
File "/opt/python3.7/lib/python3.7/queue.py", line 198, in get_nowait return self.get(block=False)
File "/opt/python3.7/lib/python3.7/queue.py", line 167, in get raise Empty _queue.Empty
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/env/local/lib/python3.7/site-packages/google/api_core/grpc_helpers.py", line 57, in error_remapped_callable return callable_(*args, **kwargs)
File "/env/local/lib/python3.7/site-packages/grpc/_channel.py", line 547, in __call__ return _end_unary_response_blocking(state, call, False, None)
File "/env/local/lib/python3.7/site-packages/grpc/_channel.py", line 466, in _end_unary_response_blocking raise _Rendezvous(state, None, None, deadline)
grpc._channel._Rendezvous: <_Rendezvous of RPC that terminated with: status = StatusCode.INVALID_ARGUMENT details = "Invalid CreateSession request." debug_error_string = "{"created":"#1547373361.398535906","description":"Error received from peer","file":"src/core/lib/surface/call.cc","file_line":1036,"grpc_message":"Invalid> CreateSession request.","grpc_status":3}" >
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py", line 297, in run_http_function result = _function_handler.invoke_user_function(flask.request)
File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py", line 199, in invoke_user_function return call_user_function(request_or_event)
File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py", line 192, in call_user_function return self._user_function(request_or_event)
File "/user_code/main.py", line 21, in new_user return insert_data(INSTANCE_ID,DATABASE_ID)
File "/user_code/main.py", line 31, in insert_data database.run_in_transaction(insert_user)
File "/env/local/lib/python3.7/site-packages/google/cloud/spanner_v1/database.py", line 438, in run_in_transaction with SessionCheckout(self._pool) as session:
File "/env/local/lib/python3.7/site-packages/google/cloud/spanner_v1/pool.py", line 519, in __enter__ self._session = self._pool.get(**self._kwargs)
File "/env/local/lib/python3.7/site-packages/google/cloud/spanner_v1/pool.py", line 268, in get session.create()
File "/env/local/lib/python3.7/site-packages/google/cloud/spanner_v1/session.py", line 116, in create session_pb = api.create_session(self._database.name, metadata=metadata, **kw)
File "/env/local/lib/python3.7/site-packages/google/cloud/spanner_v1/gapic/spanner_client.py", line 276, in create_session request, retry=retry, timeout=timeout, metadata=metadata
File "/env/local/lib/python3.7/site-packages/google/api_core/gapic_v1/method.py", line 143, in __call__ return wrapped_func(*args, **kwargs)
File "/env/local/lib/python3.7/site-packages/google/api_core/retry.py", line 270, in retry_wrapped_func on_error=on_error,
File "/env/local/lib/python3.7/site-packages/google/api_core/retry.py", line 179, in retry_target return target()
File "/env/local/lib/python3.7/site-packages/google/api_core/timeout.py", line 214, in func_with_timeout return func(*args, **kwargs)
File "/env/local/lib/python3.7/site-packages/google/api_core/grpc_helpers.py", line 59, in error_remapped_callable six.raise_from(exceptions.from_grpc_error(exc), exc)
File "<string>", line 3, in raise_from
google.api_core.exceptions.InvalidArgument: 400 Invalid CreateSession request.
I tried to reproduce this but it seems to work for me as a Python 3.7 function. I used the latest google-cloud-spanner library in requirements.txt.
While I am unsure what would be causing your error I did notice a few other things.
It seemed odd to declare a global dataDict and not use the one constructed and pass it. Instead I added that as a param to the insert method.
The spacing of the query was a bit odd and the use of single and double quotes was odd. this made it hard to parse visually. As the function runs as python 3.7 you can also use f-strings which likely would make it even more readable.
Here is the code I ran in a function that seemed to work.
import json
from google.cloud import spanner
INSTANCE_ID = 'testinstance'
DATABASE_ID = 'testdatabase'
TABLE_ID = 'userinfo'
def new_user(request):
data = { 'USER_ID': '10', 'DEVICE_ID': '11' }
return insert_data(INSTANCE_ID, DATABASE_ID, data)
def insert_data(instance_id, database_id, data):
spanner_client = spanner.Client()
instance = spanner_client.instance(instance_id)
database = instance.database(database_id)
def insert_user(transaction):
query = f"INSERT {TABLE_ID} (USER_ID,DEVICE_ID) VALUES ({data['USER_ID']},{data['DEVICE_ID']})"
row_ct = transaction.execute_update(query)
database.run_in_transaction(insert_user)
return 'Inserted data.'

Celery tries to import the broker URL as a module?

I recently added Celery to my back end but i got this weird error below
[2017-10-25 21:41:37,142: CRITICAL/MainProcess] Unrecoverable error: ImportError('No module named myredisserverip.com',)
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/celery/worker/worker.py", line 203, in start
self.blueprint.start(self)
File "/usr/local/lib/python2.7/dist-packages/celery/bootsteps.py", line 115, in start
self.on_start()
File "/usr/local/lib/python2.7/dist-packages/celery/apps/worker.py", line 143, in on_start
self.emit_banner()
File "/usr/local/lib/python2.7/dist-packages/celery/apps/worker.py", line 158, in emit_banner
' \n', self.startup_info(artlines=not use_image))),
File "/usr/local/lib/python2.7/dist-packages/celery/apps/worker.py", line 221, in startup_info
results=self.app.backend.as_uri(),
File "/usr/local/lib/python2.7/dist-packages/kombu/utils/objects.py", line 44, in __get__
value = obj.__dict__[self.__name__] = self.__get(obj)
File "/usr/local/lib/python2.7/dist-packages/celery/app/base.py", line 1183, in backend
return self._get_backend()
File "/usr/local/lib/python2.7/dist-packages/celery/app/base.py", line 901, in _get_backend
self.loader)
File "/usr/local/lib/python2.7/dist-packages/celery/app/backends.py", line 66, in by_url
return by_name(backend, loader), url
File "/usr/local/lib/python2.7/dist-packages/celery/app/backends.py", line 46, in by_name
cls = symbol_by_name(backend, aliases)
File "/usr/local/lib/python2.7/dist-packages/kombu/utils/imports.py", line 56, in symbol_by_name
module = imp(module_name, package=package, **kwargs)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
ImportError: No module named myredisserverip.com
My config.py looks like this
class BaseConfig(object):
""" A base configuration of the app """
DEBUG = False
SERVER_NAME = "my-production-ip"
SECRET_KEY = os.environ['SECRET']
BASE_DIR = os.path.abspath(os.path.dirname(__file__))
SQLALCHEMY_DATABASE_URI = os.environ['SQL_PRODUCTION']
SQLALCHEMY_TRACK_MODIFICATIONS = True
CELERY_BROKER_URL = os.environ['CELERY_BROKER_PROD']
CELERY_RESULT_BACKEND = os.environ['CELERY_RESULT_BACKEND_PROD']
DATABASE_CONNECT_OPTIONS = {}
THREADS_PER_PAGE = 2
CSRF_ENABLED = True
CSRF_SESSION_KEY = "secret"
MAIL_SERVER = "smtp.gmail.com"
MAIL_PORT = 465
MAIL_USE_SSL = True
MAIL_USE_TSL = False
MAIL_USERNAME = "blabla"
MAIL_PASSWORD = "pwd"
CELERY_BROKER_PROD and CELERY_RESULT_BACKEND are both the same and they contain the URL of the redis instance im running on amazon AWS. When i try to run
celery worker -A app.celery
from within my project directory i get this error, what is happening?
The way i setup celery is this
celery = Celery(app.name, broker=app.config['CELERY_BROKER_URL'])
celery.conf.update(app.config)
can you confirm what the values of CELERY_BROKER_PROD and CELERY_RESULTS_BACKEND are?
It seems that you have defined this as myredisserverip.com, however according to the celery docs, redis server should be defined as:
CELERY_RESULT_BACKEND = 'redis://:password#host:port/db'
as per the documentation here: http://docs.celeryproject.org/en/3.1/configuration.html#redis-backend-settings
For the broker_url you need to define the transport, documentation on this can be found here:
http://docs.celeryproject.org/en/3.1/configuration.html#broker-url
Is the issue that you are missing the transport, ie the redis:// prefix in your environment variables?

sqlalchemy: assert dispatch_reg[owner_ref] == listen_ref

I'm using Invenio 2.0 and try to replace old version of SQLAlchemy 0.8.7 with the last 0.9.7.
The utility to automaticaly create the db works (inveniomanage database recreate --yes-i-know).
But when I start tests with: python setup.py test
It return me a error:
test_fisrt_blueprint (invenio.testsuite.test_ext_template.TemplateLoaderCase) ... --------------------------------------------------------------------------------
ERROR in wrappers [/home/vagrant/.virtualenvs/invenio2/src/invenio/invenio/ext/logging/wrappers.py:310]:
--------------------------------------------------------------------------------
Traceback (most recent call last):
File "/home/vagrant/.virtualenvs/invenio2/src/invenio/invenio/ext/legacy/__init__.py", line 124, in __call__
response = self.app.full_dispatch_request()
File "/home/vagrant/.virtualenvs/invenio2/local/lib/python2.7/site-packages/flask/app.py", line 1470, in full_dispatch_request
self.try_trigger_before_first_request_functions()
File "/home/vagrant/.virtualenvs/invenio2/local/lib/python2.7/site-packages/flask/app.py", line 1497, in try_trigger_before_first_request_functions
func()
File "/home/vagrant/.virtualenvs/invenio2/src/invenio/invenio/modules/messages/views.py", line 264, in invoke_email_alert_register
email_alert_register()
File "/home/vagrant/.virtualenvs/invenio2/src/invenio/invenio/modules/messages/models.py", line 202, in email_alert_register
event.listen(MsgMESSAGE, 'after_insert', email_alert)
File "/home/vagrant/.virtualenvs/invenio2/local/lib/python2.7/site-packages/sqlalchemy/event/api.py", line 63, in listen
_event_key(target, identifier, fn).listen(*args, **kw)
File "/home/vagrant/.virtualenvs/invenio2/local/lib/python2.7/site-packages/sqlalchemy/event/registry.py", line 187, in listen
self.dispatch_target.dispatch._listen(self, *args, **kw)
File "/home/vagrant/.virtualenvs/invenio2/local/lib/python2.7/site-packages/sqlalchemy/orm/events.py", line 547, in _listen
event_key.base_listen(**kw)
File "/home/vagrant/.virtualenvs/invenio2/local/lib/python2.7/site-packages/sqlalchemy/event/registry.py", line 226, in base_listen
for_modify(target.dispatch).append(self, propagate)
File "/home/vagrant/.virtualenvs/invenio2/local/lib/python2.7/site-packages/sqlalchemy/event/attr.py", line 328, in append
event_key.append_to_list(self, self.listeners)
File "/home/vagrant/.virtualenvs/invenio2/local/lib/python2.7/site-packages/sqlalchemy/event/registry.py", line 237, in append_to_list
_stored_in_collection(self, owner)
File "/home/vagrant/.virtualenvs/invenio2/local/lib/python2.7/site-packages/sqlalchemy/event/registry.py", line 74, in _stored_in_collection
assert dispatch_reg[owner_ref] == listen_ref
AssertionError
In /home/vagrant/.virtualenvs/invenio2/src/invenio/invenio/modules/messages/views.py (row 264)
# Registration of email_alert invoked from blueprint
# in order to use before_app_first_request.
# Reading config CFG_WEBMESSAGE_EMAIL_ALERT
# required app context.
#blueprint.before_app_first_request
def invoke_email_alert_register():
email_alert_register()
In /home/vagrant/.virtualenvs/invenio2/src/invenio/invenio/modules/messages/models.py (row 202)
# Registration of email_alert invoked from blueprint
# in order to use before_app_first_request.
# Reading config CFG_WEBMESSAGE_EMAIL_ALERT
# required app context.
def email_alert_register():
if cfg['CFG_WEBMESSAGE_EMAIL_ALERT']:
from sqlalchemy import event
# Register after insert callback.
event.listen(MsgMESSAGE, 'after_insert', email_alert)
Someone can help me?
Installed:
-e git+https://github.com/mitsuhiko/flask-sqlalchemy#c7eccba63314f3ea77e2c6217d3d3c8b0d2552fd#egg=Flask_SQLAlchemy-2.0
MySQL-python==1.2.5
SQLAlchemy==0.9.7
SQLAlchemy-Utils==0.23.5
With help from google (today) I found what I suspect would be a solution here (I'm not an invenio user)
I suspect an SQLa update will fix your issue.
https://bitbucket.org/zzzeek/sqlalchemy/issue/3199/deduplication-of-events-doesnt-work-for
-->
https://bitbucket.org/zzzeek/sqlalchemy/commits/9ae4db27b993
-->
Fixed in SQLA 0.9.8 (supposedly)

Categories

Resources