Skip to content
This repository was archived by the owner on Jun 30, 2022. It is now read-only.
This repository was archived by the owner on Jun 30, 2022. It is now read-only.

Connecting to Cloud SQL #9

@sthomp

Description

@sthomp

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions