TimeoutError: Unable to get database connection within 0 seconds. (TracError(,))

当你在 trac(An enhanced wiki and issue tracking system for software projects) 0.12 上启用某个插件后,比如 trac-mastertickets-3.0.1(Support for ticket dependencies),可能会要求你执行 trac-admin project_environment_path upgrade 以创建缺少的数据库表,比如 mastertickets。

如果你是使用 Apache 作为 web 前端,PostgreSQL(而不是默认的 sqlite) 作为后端存储的话,当执行完上述命令后,再用浏览器查看 trac 的时候可能会得到下面的错误信息:

Traceback (most recent call last):
  File "build/bdist.freebsd-8.1-RELEASE-amd64/egg/trac/web/api.py", line 440, in send_error
    data, 'text/html')
  File "build/bdist.freebsd-8.1-RELEASE-amd64/egg/trac/web/chrome.py", line 827, in render_template
    message = req.session.pop('chrome.%s.%d' % (type_, i))
  File "build/bdist.freebsd-8.1-RELEASE-amd64/egg/trac/web/api.py", line 216, in __getattr__
    value = self.callbacks[name](self)
  File "build/bdist.freebsd-8.1-RELEASE-amd64/egg/trac/web/main.py", line 300, in _get_session
    return Session(self.env, req)
  File "build/bdist.freebsd-8.1-RELEASE-amd64/egg/trac/web/session.py", line 203, in __init__
    self.get_session(req.authname, authenticated=True)
  File "build/bdist.freebsd-8.1-RELEASE-amd64/egg/trac/web/session.py", line 219, in get_session
    super(Session, self).get_session(sid, authenticated)
  File "build/bdist.freebsd-8.1-RELEASE-amd64/egg/trac/web/session.py", line 61, in get_session
    db = self.env.get_db_cnx()
  File "build/bdist.freebsd-8.1-RELEASE-amd64/egg/trac/env.py", line 328, in get_db_cnx
    return get_read_db(self)
  File "build/bdist.freebsd-8.1-RELEASE-amd64/egg/trac/db/api.py", line 90, in get_read_db
    return _transaction_local.db or DatabaseManager(env).get_connection()
  File "build/bdist.freebsd-8.1-RELEASE-amd64/egg/trac/db/api.py", line 152, in get_connection
    return self._cnx_pool.get_cnx(self.timeout or None)
  File "build/bdist.freebsd-8.1-RELEASE-amd64/egg/trac/db/pool.py", line 226, in get_cnx
    return _backend.get_cnx(self._connector, self._kwargs, timeout)
  File "build/bdist.freebsd-8.1-RELEASE-amd64/egg/trac/db/pool.py", line 146, in get_cnx
    raise TimeoutError(errmsg)
TimeoutError: Unable to get database connection within 0 seconds. (TracError(,))

根据上述错误信息,你可能会去检查数据库连接问题,并且百查不得头绪。其实是因为 trac 无法读取 conf/trac.ini 所致。

执行 trac-admin upgrade 之前,trac.ini的权限及属主信息如下:

-rw-------  1 www  www  4998 10 31 23:20 conf/trac.ini

而执行完成后,其权限变成了:

-rw-------  1 root  www  4998 10 31 23:20 conf/trac.ini

因为属主变成了 root,且 Apache 工作进程所在的用户组 www 的权限为g:---,所以 Apache 无法读取 trac.ini,从而导致了上述错误的产生。

解决方法:

chown www conf/trac.ini

如果错误依旧,尝试重启动 Apache 试试:

apachectl restart
Advertisements

#apache-http-server, #postgresql, #trac

把 Trac Spam Filtering 添加到 ports 里了

告诉各位 FreeBSD 上的 Trac 用户一个好消息,昨天我提交了 port: trac-spam-filter现在 ports 里已经有了 (和上次的 subcommander 相比,这次的效率好高,5分钟之内就被处理好了),让我们一起来抵制那讨厌的 spams 吧。这里我稍微讲一下它的安装方法和配置使用方法。
首先安装:
$ cd /usr/ports/www/trac-spam-filter/ && make install clean
或者通过添加 package 的方式安装也可以:
$ pkg_add -r trac-TracSpamFilter

安装完成后,在你的 trac.ini 里面添加下面的配置(可参考:http://trac.edgewall.org/wiki/SpamFilter ):
[components]
tracspamfilter.* = enabled

这样 trac spam filter 就被启用了,然后你可以选择使用下列过滤策略来进行垃圾信息的过滤,在这个版本(目前 trac spam filter 是 0.2.1 版本)中允许设置“karma”来组织多个不同的策略(这个配置可以在 WebAdmin (port: trac-webadmin ) 的 Spam Filter -> Configuration 中进行):

  1. 正则表达式过滤
  2. IP 黑名单
  3. 针对 IP 节流(IP Throttling,这个词有点难翻译,大概的意思是针对 IP 设置最大允许的 POST)
  4. 使用 Akismet 过滤(这个方式采用 Akismet 提供的一个 web 服务来检查内容是否存在可能的垃圾信息)
  5. Bayes(目前版本尚未支持,还在规划中)

下面我只着重介绍正则表达式过滤,因为这个比较常用(其它的过滤策略请自行研究):
新建一个 wiki 页面,名字为 BadContent,在里面增加一个代码块(就是用{{{}}}这个符号括起来的,可以参考 trac 网站上的例子: http://trac.edgewall.org/wiki/BadContent ),在代码块中增加 Python 语法的正则表达式 ,一行一个,如果匿名用户发送的内容中匹配了任意一行,就会被当做垃圾信息被拒绝。被拒绝的信息还可以通过 WebAdmin (port: trac-webadmin )中的 Spam Filter -> Monitoring 进行管理。

注意:

  1. 配置改好后,如果你是使用的 apache,要重启一下 apache。
  2. BadContent 这个页面最好设置成只读,别让别人乱改。
  3. 另外 spam filter 只会读取第一个代码块,其它的代码块是视而不见的。

另外,如果你遇到类似如下错误,可以参考: http://trac.edgewall.org/wiki/TracPlugins#SettingupthePluginCache
Traceback (most recent call last):
File "/usr/local/lib/python2.5/site-packages/trac/web/main.py", line 406, in dispatch_request
dispatcher.dispatch(req)
File "/usr/local/lib/python2.5/site-packages/trac/web/main.py", line 206, in dispatch
req.hdf = HDFWrapper(loadpaths=chrome.get_all_templates_dirs())
File "/usr/local/lib/python2.5/site-packages/trac/web/chrome.py", line 263, in get_all_templates_dirs
dirs += provider.get_templates_dirs()
File "build/bdist.freebsd-6.2-STABLE-i386/egg/tracspamfilter/admin.py", line 115, in get_templates_dirs
return [resource_filename(__name__, ‘templates’)]
File "build/bdist.freebsd-6.2-STABLE-i386/egg/pkg_resources.py", line 841, in resource_filename
self, resource_name
File "build/bdist.freebsd-6.2-STABLE-i386/egg/pkg_resources.py", line 1311, in get_resource_filename
return self._extract_resource(manager, zip_path)
File "build/bdist.freebsd-6.2-STABLE-i386/egg/pkg_resources.py", line 1318, in _extract_resource
manager, os.path.join(zip_path, name)
File "build/bdist.freebsd-6.2-STABLE-i386/egg/pkg_resources.py", line 1332, in _extract_resource
self.egg_name, self._parts(zip_path)
File "build/bdist.freebsd-6.2-STABLE-i386/egg/pkg_resources.py", line 921, in get_cache_path
self.extraction_error()
File "build/bdist.freebsd-6.2-STABLE-i386/egg/pkg_resources.py", line 887, in extraction_error
raise err
ExtractionError: Can’t extract file(s) to egg cache

The following error occurred while trying to extract file(s) to the Python egg
cache:

[Errno 13] Permission denied: ‘/root/.python-eggs’

The Python egg cache directory is currently set to:

/root/.python-eggs

Perhaps your account does not have write access to this directory? You can
change the cache directory by setting the PYTHON_EGG_CACHE environment
variable to point to an accessible directory.

#freebsd, #trac