Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TFJob client should not block forever trying to get the namespace object #607

Merged
merged 2 commits into from
May 25, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 29 additions & 13 deletions py/tf_job_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import datetime
import json
import logging
import multiprocessing
import time

from kubernetes import client as k8s_client
Expand All @@ -15,6 +16,8 @@
TF_JOB_PLURAL = "tfjobs"
TF_JOB_KIND = "TFJob"

# How long to wait in seconds for requests to the ApiServer
TIMEOUT = 120

def create_tf_job(client, spec):
"""Create a TFJob.
Expand All @@ -27,8 +30,9 @@ def create_tf_job(client, spec):
try:
# Create a Resource
namespace = spec["metadata"].get("namespace", "default")
api_response = crd_api.create_namespaced_custom_object(
TF_JOB_GROUP, TF_JOB_VERSION, namespace, TF_JOB_PLURAL, spec)
thread = crd_api.create_namespaced_custom_object(
TF_JOB_GROUP, TF_JOB_VERSION, namespace, TF_JOB_PLURAL, spec, async=True)
api_response = thread.get(TIMEOUT)
logging.info("Created job %s", api_response["metadata"]["name"])
return api_response
except ApiException as e:
Expand Down Expand Up @@ -61,8 +65,10 @@ def delete_tf_job(client, namespace, name):
"propagationPolicy": "Foreground",
}
logging.info("Deleting job %s.%s", namespace, name)
api_response = crd_api.delete_namespaced_custom_object(
TF_JOB_GROUP, TF_JOB_VERSION, namespace, TF_JOB_PLURAL, name, body)
thread = crd_api.delete_namespaced_custom_object(
TF_JOB_GROUP, TF_JOB_VERSION, namespace, TF_JOB_PLURAL, name, body,
async=True)
api_response = thread.get(TIMEOUT)
logging.info("Deleting job %s.%s returned: %s", namespace, name, api_response)
return api_response
except ApiException as e:
Expand Down Expand Up @@ -117,15 +123,25 @@ def wait_for_job(client,
crd_api = k8s_client.CustomObjectsApi(client)
end_time = datetime.datetime.now() + timeout
while True:
results = crd_api.get_namespaced_custom_object(
TF_JOB_GROUP, TF_JOB_VERSION, namespace, TF_JOB_PLURAL, name)

if status_callback:
status_callback(results)

# If we poll the CRD quick enough status won't have been set yet.
if results.get("status", {}).get("phase", {}) == "Done":
return results
# By setting async=True ApiClient returns multiprocessing.pool.AsyncResult
# If we don't set async=True then it could potentially block forever.
thread = crd_api.get_namespaced_custom_object(
TF_JOB_GROUP, TF_JOB_VERSION, namespace, TF_JOB_PLURAL, name, async=True)

# Try to get the result but timeout.
results = None
try:
results = thread.get(TIMEOUT)
except multiprocessing.TimeoutError:
logging.error("Timeout trying to get TFJob.")

if results:
if status_callback:
status_callback(results)

# If we poll the CRD quick enough status won't have been set yet.
if results.get("status", {}).get("phase", {}) == "Done":
return results

if datetime.datetime.now() + polling_interval > end_time:
raise util.TimeoutError(
Expand Down