-
Notifications
You must be signed in to change notification settings - Fork 29
Connecting to Cloud SQL #9
Description
I wanted to leave a note here to update setup.py (or possibly provide more examples of setup.py?) with instructions on connecting to Cloud SQL. I hope this will save someone a few hours (days? :))
Connecting to Cloud SQL requires use of the Cloud SQL Proxy which needs to be installed via setup.py. I thought this would be as straight forward as:
CUSTOM_COMMANDS = [
['echo', 'Custom command worked!'],
['wget', 'https://dl.google.com/cloudsql/cloud_sql_proxy.linux.amd64'],
['mv', 'cloud_sql_proxy.linux.amd64', 'cloud_sql_proxy'],
['chmod', '+x', 'cloud_sql_proxy'],
['cloud_sql_proxy', '-dir=/cloudsql -instances=my-project:us-central1:mysql-instance=tcp:3307', '&']
]
But it turns out the ampersand & required to run the proxy in the background will cause distutils to freeze and thus cause the entire job to freeze. In this state, the job needs to be manually cancelled or it will just sit idle indefinitely. This is due to the underlying subprocess.Popen(..., shell=False).
To fix this I added another parameter to RunCustomCommand so that shell=True could be passed in. And then called RunCustomCommand separately as follows:
self.RunCustomCommand('./cloud_sql_proxy -dir=/cloudsql -instances=my-project:us-central1:mysql-instance=tcp:3307 &', True)
Update:
Seems I jumped the gun on coming up with a solution to this problem. The core of the issue was more so related to the line p.communicate() and p.returncode in setup.py which will block and wait for the sql proxy to return.
I've managed to get this all working in a local container by having a separate subprocess command in python:
subprocess.Popen(./cloud_sql_proxy -dir=/cloudsql -instances=my-project:us-central1:mysql-instance=tcp:3307, shell=True, preexec_fn=os.setpgrp)
However running this on dataflow causes easy_install to stall and puts the rest of the dataflow job in a hung state