def run_server(dbhostport=None, dbpath=None, logfile=DATABASE['LOG'], loglevel='WARN'): """ Run the DbServer on the given database file and port. If not given, use the settings in openquake.cfg. """ if dbhostport: # assume a string of the form "dbhost:port" dbhost, port = dbhostport.split(':') addr = (dbhost, int(port)) DATABASE['PORT'] = int(port) else: addr = config.DBS_ADDRESS if dbpath: DATABASE['NAME'] = dbpath # create the db directory if needed dirname = os.path.dirname(DATABASE['NAME']) if not os.path.exists(dirname): os.makedirs(dirname) # create and upgrade the db if needed db = dbapi.Db(sqlite3.connect, DATABASE['NAME'], isolation_level=None, detect_types=sqlite3.PARSE_DECLTYPES) db('PRAGMA foreign_keys = ON') # honor ON DELETE CASCADE actions.upgrade_db(db) db.conn.close() # configure logging and start the server logging.basicConfig(level=getattr(logging, loglevel), filename=logfile) DbServer(db, addr, config.DBS_AUTHKEY).loop()
import sqlite3 import logging import threading import subprocess from openquake.baselib import config, sap, zeromq as z, workerpool as w from openquake.baselib.general import socket_ready, detach_process from openquake.baselib.parallel import safely_call from openquake.commonlib import logs from openquake.server.db import actions from openquake.server import dbapi from openquake.server import __file__ as server_path db = dbapi.Db(sqlite3.connect, os.path.expanduser(config.dbserver.file), isolation_level=None, detect_types=sqlite3.PARSE_DECLTYPES, timeout=20) db.cmd = lambda action, *args: getattr(actions, action)(db, *args) # NB: I am increasing the timeout from 5 to 20 seconds to see if the random # OperationalError: "database is locked" disappear in the WebUI tests ZMQ = os.environ.get('OQ_DISTRIBUTE', config.distribution.oq_distribute) == 'zmq' DBSERVER_PORT = int(os.environ.get('OQ_DBSERVER_PORT') or config.dbserver.port) class DbServer(object): """ A server collecting the received commands into a queue