sql: declare application_name to postgresql connections (#75795) #516
|
@ -556,14 +556,22 @@ def test_configure_postgresql(setuptest, alt_tempdir, deploy_setup):
|
|||
with mock.patch('psycopg2.connect') as connect:
|
||||
hobo_cmd.configure_sql(service, pub)
|
||||
assert connect.call_args_list[0][1] == {'user': 'test', 'dbname': 'postgres'}
|
||||
assert connect.call_args_list[1][1] == {'user': 'fred', 'dbname': 'tests_wcs_wcs_example_net'}
|
||||
assert connect.call_args_list[1][1] == {
|
||||
'user': 'fred',
|
||||
'dbname': 'tests_wcs_wcs_example_net',
|
||||
'application_name': 'wcs',
|
||||
}
|
||||
assert pub.initialize_sql.call_count == 1
|
||||
|
||||
pub.reload_cfg()
|
||||
assert 'createdb-connection-params' in pub.cfg['postgresql']
|
||||
with mock.patch('psycopg2.connect') as connect:
|
||||
sql.get_connection(new=True)
|
||||
assert connect.call_args_list[0][1] == {'user': 'fred', 'dbname': 'tests_wcs_wcs_example_net'}
|
||||
assert connect.call_args_list[0][1] == {
|
||||
'user': 'fred',
|
||||
'dbname': 'tests_wcs_wcs_example_net',
|
||||
'application_name': 'wcs',
|
||||
}
|
||||
|
||||
pub.cfg['postgresql']['database-template-name'] = 'very_long_' * 10 + '%s'
|
||||
with mock.patch('psycopg2.connect') as connect:
|
||||
|
@ -572,6 +580,7 @@ def test_configure_postgresql(setuptest, alt_tempdir, deploy_setup):
|
|||
assert connect.call_args_list[1][1] == {
|
||||
'user': 'fred',
|
||||
'dbname': 'very_long_very_long_very_long_c426_ng_very_long_wcs_example_net',
|
||||
'application_name': 'wcs',
|
||||
}
|
||||
assert len(connect.call_args_list[1][1]['dbname']) == 63
|
||||
assert pub.initialize_sql.call_count == 2
|
||||
|
@ -583,5 +592,6 @@ def test_configure_postgresql(setuptest, alt_tempdir, deploy_setup):
|
|||
assert connect.call_args_list[1][1] == {
|
||||
'user': 'fred',
|
||||
'dbname': 'test_2_wcs_example_net',
|
||||
'application_name': 'wcs',
|
||||
}
|
||||
assert pub.initialize_sql.call_count == 3
|
||||
|
|
|
@ -124,6 +124,8 @@ class WcsPublisher(QommonPublisher):
|
|||
backoffice_directory_class = BackofficeRootDirectory
|
||||
admin_directory_class = AdminRootDirectory
|
||||
|
||||
sql_application_name = 'wcs'
|
||||
|
||||
session_manager_class = None
|
||||
user_class = User
|
||||
tracking_code_class = TrackingCode
|
||||
|
|
|
@ -114,6 +114,7 @@ def cron_worker(publisher, since, job_name=None):
|
|||
jobs = get_jobs_since(publisher, since)
|
||||
|
||||
# reindex user and formdata if needed
|
||||
publisher.set_sql_application_name('wcs-cron-reindex')
|
||||
publisher.reindex_sql()
|
||||
|
||||
if jobs:
|
||||
|
@ -121,6 +122,7 @@ def cron_worker(publisher, since, job_name=None):
|
|||
for job in jobs:
|
||||
publisher.install_lang()
|
||||
publisher.reset_formdata_state()
|
||||
publisher.set_sql_application_name(f'wcs-cron-{job.name}')
|
||||
try:
|
||||
with job.log_long_job():
|
||||
job.function(publisher, job=job)
|
||||
|
|
|
@ -55,6 +55,7 @@ class Command(BaseCommand):
|
|||
publisher_class = get_publisher_class()
|
||||
publisher_class.register_cronjobs()
|
||||
publisher = publisher_class.create_publisher()
|
||||
publisher.set_sql_application_name('wcs-cron')
|
||||
offset = ord(settings.SECRET_KEY[-1]) % 60
|
||||
if not job_name:
|
||||
CronJob.log('starting cron (minutes offset is %s)' % offset, in_tenant=False)
|
||||
|
|
|
@ -35,6 +35,7 @@ class Command(BaseCommand):
|
|||
if pub.has_postgresql_config():
|
||||
if verbosity:
|
||||
print('Running migrations for', tenant.hostname, '(%d/%d)' % (n, nb_tenants), flush=True)
|
||||
pub.set_sql_application_name('wcs-migrate')
|
||||
pub.migrate_sql()
|
||||
pub.cleanup()
|
||||
quixote.cleanup()
|
||||
|
|
|
@ -128,6 +128,8 @@ class QommonPublisher(Publisher):
|
|||
app_dir = None
|
||||
_i18n_catalog = None
|
||||
|
||||
sql_application_name = None
|
||||
|
||||
def get_root_url(self):
|
||||
if self.get_request():
|
||||
return self.get_request().environ['SCRIPT_NAME'] + '/'
|
||||
|
@ -727,6 +729,13 @@ class QommonPublisher(Publisher):
|
|||
self._app_logger = None
|
||||
self.init_publisher_substitutions(self.get_request())
|
||||
|
||||
def set_sql_application_name(self, name):
|
||||
if name != self.sql_application_name:
|
||||
from wcs.sql import get_connection
|
||||
|
||||
self.sql_application_name = name
|
||||
get_connection(new=True)
|
||||
|
||||
def reset_formdata_state(self):
|
||||
# reset parameters that may have been altered by running a workflow on
|
||||
# a formdata. required be run before performing actions on another formdata.
|
||||
|
|
|
@ -228,7 +228,8 @@ def get_connection(new=False):
|
|||
if new:
|
||||
cleanup_connection()
|
||||
|
||||
if not getattr(get_publisher(), 'pgconn', None):
|
||||
publisher = get_publisher()
|
||||
if not getattr(publisher, 'pgconn', None):
|
||||
postgresql_cfg = {}
|
||||
for param in ('database', 'user', 'password', 'host', 'port'):
|
||||
value = get_cfg('postgresql', {}).get(param)
|
||||
|
@ -236,6 +237,7 @@ def get_connection(new=False):
|
|||
postgresql_cfg[param] = value
|
||||
if 'database' in postgresql_cfg:
|
||||
postgresql_cfg['dbname'] = postgresql_cfg.pop('database')
|
||||
postgresql_cfg['application_name'] = getattr(publisher, 'sql_application_name', None)
|
||||
try:
|
||||
pgconn = psycopg2.connect(**postgresql_cfg)
|
||||
except psycopg2.Error:
|
||||
|
@ -243,9 +245,9 @@ def get_connection(new=False):
|
|||
raise
|
||||
pgconn = None
|
||||
|
||||
get_publisher().pgconn = pgconn
|
||||
publisher.pgconn = pgconn
|
||||
|
||||
return get_publisher().pgconn
|
||||
return publisher.pgconn
|
||||
|
||||
|
||||
def cleanup_connection():
|
||||
|
|
Loading…
Reference in New Issue