# HG changeset patch # User Marcin Kuzminski # Date 2010-11-24 16:15:33 # Node ID 89b9037d68b7a8bc39131169301914973fb67bfb # Parent ff881ec6a140a7c8dd5a4134196050ca34881c85 fixed Example celery config to ampq, fixed session problems on models and celery, implemented one base model as a parent for other diff --git a/celeryconfig.py b/celeryconfig.py --- a/celeryconfig.py +++ b/celeryconfig.py @@ -14,8 +14,10 @@ PYLONS_CONFIG = config CELERY_IMPORTS = ("rhodecode.lib.celerylib.tasks",) ## Result store settings. -CELERY_RESULT_BACKEND = "database" -CELERY_RESULT_DBURI = dict(config.items('app:main'))['sqlalchemy.db1.url'] +CELERY_RESULT_BACKEND = "amqp" +CELERY_AMQP_TASK_RESULT_EXPIRES = 18000 # 5 hours. + +#CELERY_RESULT_DBURI = dict(config.items('app:main'))['sqlalchemy.db1.url'] CELERY_RESULT_SERIALIZER = 'json' diff --git a/rhodecode/lib/celerylib/tasks.py b/rhodecode/lib/celerylib/tasks.py --- a/rhodecode/lib/celerylib/tasks.py +++ b/rhodecode/lib/celerylib/tasks.py @@ -280,7 +280,7 @@ def create_repo_fork(form_data, cur_user from rhodecode.model.repo import RepoModel from vcs import get_backend log = create_repo_fork.get_logger() - repo_model = RepoModel() + repo_model = RepoModel(get_session()) repo_model.create(form_data, cur_user, just_db=True, fork=True) repo_name = form_data['repo_name'] repos_path = get_repos_path() diff --git a/rhodecode/model/__init__.py b/rhodecode/model/__init__.py --- a/rhodecode/model/__init__.py +++ b/rhodecode/model/__init__.py @@ -21,3 +21,12 @@ def init_model(engine): #engine = engine_from_config(config, 'sqlalchemy.') #init_model(engine) # DO SOMETHING + + +class BaseModel(object): + + def __init__(self, sa=None): + if sa is not None: + self.sa = sa + else: + self.sa = meta.Session() diff --git a/rhodecode/model/permission.py b/rhodecode/model/permission.py --- a/rhodecode/model/permission.py +++ b/rhodecode/model/permission.py @@ -20,21 +20,18 @@ """ Created on Aug 20, 2010 Model for permissions -@author: marcink +:author: marcink """ +from rhodecode.model import BaseModel from rhodecode.model.db import User, Permission, UserToPerm, RepoToPerm from rhodecode.model.caching_query import FromCache -from rhodecode.model.meta import Session import logging import traceback log = logging.getLogger(__name__) -class PermissionModel(object): - - def __init__(self): - self.sa = Session() +class PermissionModel(BaseModel): def get_permission(self, permission_id, cache=False): perm = self.sa.query(Permission) diff --git a/rhodecode/model/repo.py b/rhodecode/model/repo.py --- a/rhodecode/model/repo.py +++ b/rhodecode/model/repo.py @@ -21,12 +21,12 @@ Created on Jun 5, 2010 model for handling repositories actions :author: marcink """ -from vcs.backends import get_repo, get_backend +from vcs.backends import get_backend from datetime import datetime from pylons import app_globals as g from rhodecode.model.db import Repository, RepoToPerm, User, Permission, \ Statistics -from rhodecode.model.meta import Session +from rhodecode.model import BaseModel from rhodecode.model.user import UserModel from rhodecode.model.caching_query import FromCache import logging @@ -35,10 +35,7 @@ import shutil import traceback log = logging.getLogger(__name__) -class RepoModel(object): - - def __init__(self): - self.sa = Session() +class RepoModel(BaseModel): def get(self, repo_id, cache=False): repo = self.sa.query(Repository)\ @@ -57,7 +54,7 @@ class RepoModel(object): if cache: repo = repo.options(FromCache("sql_cache_short", "get_repo_%s" % repo_name)) - return repo.scalar() + return repo.scalar() def get_users_js(self): @@ -75,14 +72,14 @@ class RepoModel(object): #update permissions for username, perm in form_data['perms_updates']: r2p = self.sa.query(RepoToPerm)\ - .filter(RepoToPerm.user == UserModel()\ + .filter(RepoToPerm.user == UserModel(self.sa)\ .get_by_username(username, cache=False))\ .filter(RepoToPerm.repository == \ self.get_by_repo_name(repo_name))\ .one() r2p.permission_id = self.sa.query(Permission).filter( - Permission.permission_name == + Permission.permission_name == perm).one().permission_id self.sa.add(r2p) @@ -90,7 +87,7 @@ class RepoModel(object): for username, perm in form_data['perms_new']: r2p = RepoToPerm() r2p.repository = self.get_by_repo_name(repo_name) - r2p.user = UserModel().get_by_username(username, cache=False) + r2p.user = UserModel(self.sa).get_by_username(username, cache=False) r2p.permission_id = self.sa.query(Permission).filter( Permission.permission_name == perm)\ @@ -144,7 +141,7 @@ class RepoModel(object): #create default permission repo_to_perm = RepoToPerm() default = 'repository.read' - for p in UserModel().get_by_username('default', cache=False).user_perms: + for p in UserModel(self.sa).get_by_username('default', cache=False).user_perms: if p.permission.permission_name.startswith('repository.'): default = p.permission.permission_name break @@ -156,7 +153,8 @@ class RepoModel(object): .one().permission_id repo_to_perm.repository_id = new_repo.repo_id - repo_to_perm.user_id = UserModel().get_by_username('default', cache=False).user_id + repo_to_perm.user_id = UserModel(self.sa)\ + .get_by_username('default', cache=False).user_id self.sa.add(repo_to_perm) self.sa.commit() diff --git a/rhodecode/model/scm.py b/rhodecode/model/scm.py --- a/rhodecode/model/scm.py +++ b/rhodecode/model/scm.py @@ -28,7 +28,7 @@ from rhodecode import BACKENDS from rhodecode.lib import helpers as h from rhodecode.lib.auth import HasRepoPermissionAny from rhodecode.lib.utils import get_repos, make_ui, action_logger -from rhodecode.model import meta +from rhodecode.model import BaseModel from rhodecode.model.db import Repository, User, RhodeCodeUi, CacheInvalidation, \ UserFollowing from rhodecode.model.caching_query import FromCache @@ -54,14 +54,11 @@ class RepoTemp(object): self.repo_id = repo_id -class ScmModel(object): +class ScmModel(BaseModel): """ Mercurial Model """ - def __init__(self): - self.sa = meta.Session() - @LazyProperty def repos_path(self): """ diff --git a/rhodecode/model/settings.py b/rhodecode/model/settings.py --- a/rhodecode/model/settings.py +++ b/rhodecode/model/settings.py @@ -20,10 +20,11 @@ """ Created on Nov 17, 2010 Model for RhodeCode -@author: marcink +:author: marcink """ + from rhodecode.lib import helpers as h -from rhodecode.model import meta +from rhodecode.model import BaseModel from rhodecode.model.caching_query import FromCache from rhodecode.model.db import RhodeCodeSettings from sqlalchemy.orm import joinedload @@ -32,15 +33,11 @@ import logging log = logging.getLogger(__name__) -class SettingsModel(object): +class SettingsModel(BaseModel): """ Settings model """ - def __init__(self): - self.sa = meta.Session() - - def get(self, settings_key, cache=False): r = self.sa.query(RhodeCodeSettings)\ .filter(RhodeCodeSettings.app_settings_name == settings_key).scalar() diff --git a/rhodecode/model/user.py b/rhodecode/model/user.py --- a/rhodecode/model/user.py +++ b/rhodecode/model/user.py @@ -24,9 +24,9 @@ Model for users """ from pylons.i18n.translation import _ +from rhodecode.model import BaseModel from rhodecode.model.caching_query import FromCache from rhodecode.model.db import User -from rhodecode.model.meta import Session from rhodecode.lib.exceptions import * import logging @@ -36,10 +36,7 @@ log = logging.getLogger(__name__) -class UserModel(object): - - def __init__(self): - self.sa = Session() +class UserModel(BaseModel): def get(self, user_id, cache=False): user = self.sa.query(User)