Add prerun checks for postgres & solr database
This commit is contained in:
parent
9504ee4b27
commit
2feab6e9c6
|
@ -1,10 +1,57 @@
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import subprocess
|
import subprocess
|
||||||
|
import psycopg2
|
||||||
|
import urllib2
|
||||||
|
|
||||||
|
|
||||||
ckan_ini = os.environ.get('CKAN_INI', '/srv/app/production.ini')
|
ckan_ini = os.environ.get('CKAN_INI', '/srv/app/production.ini')
|
||||||
|
|
||||||
|
RETRY = 5
|
||||||
|
|
||||||
|
def check_db_connection(retry=None):
|
||||||
|
|
||||||
|
if retry is None:
|
||||||
|
retry = RETRY
|
||||||
|
elif retry == 0:
|
||||||
|
print '[prerun] Giving up after 5 tries...'
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
conn_str = os.environ.get('CKAN_SQLALCHEMY_URL', '')
|
||||||
|
try:
|
||||||
|
connection = psycopg2.connect(conn_str)
|
||||||
|
|
||||||
|
except psycopg2.Error as e:
|
||||||
|
print str(e)
|
||||||
|
print '[prerun] Unable to connect to the database...try again in a while.'
|
||||||
|
import time
|
||||||
|
time.sleep(10)
|
||||||
|
check_db_connection(retry = retry - 1)
|
||||||
|
else:
|
||||||
|
connection.close()
|
||||||
|
|
||||||
|
def check_solr_connection(retry=None):
|
||||||
|
|
||||||
|
if retry is None:
|
||||||
|
retry = RETRY
|
||||||
|
elif retry == 0:
|
||||||
|
print '[prerun] Giving up after 5 tries...'
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
url = os.environ.get('CKAN_SOLR_URL', '')
|
||||||
|
search_url = '{url}/select/?q=*&wt=json'.format(url=url)
|
||||||
|
|
||||||
|
try:
|
||||||
|
connection = urllib2.urlopen(search_url)
|
||||||
|
except urllib2.URLError as e:
|
||||||
|
print str(e)
|
||||||
|
print '[prerun] Unable to connect to solr...try again in a while.'
|
||||||
|
import time
|
||||||
|
time.sleep(10)
|
||||||
|
check_solr_connection(retry = retry - 1)
|
||||||
|
else:
|
||||||
|
eval(connection.read())
|
||||||
|
|
||||||
|
|
||||||
def init_db():
|
def init_db():
|
||||||
|
|
||||||
|
@ -68,5 +115,7 @@ if __name__ == '__main__':
|
||||||
if maintenance:
|
if maintenance:
|
||||||
print '[prerun] Maintenance mode, skipping setup...'
|
print '[prerun] Maintenance mode, skipping setup...'
|
||||||
else:
|
else:
|
||||||
|
check_db_connection()
|
||||||
|
check_solr_connection()
|
||||||
init_db()
|
init_db()
|
||||||
create_sysadmin()
|
create_sysadmin()
|
||||||
|
|
|
@ -1,3 +1,8 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
python prerun.py
|
python prerun.py
|
||||||
gunicorn --log-file=- --paste production.ini
|
if [ $? -eq 0 ]
|
||||||
|
then
|
||||||
|
gunicorn --log-file=- --paste production.ini
|
||||||
|
else
|
||||||
|
echo "[prerun] failed...not starting CKAN."
|
||||||
|
fi
|
||||||
|
|
Loading…
Reference in New Issue