Skip to content

Commit 5019a67

Browse files
committed
[batch] Re-create Batch Api client on every request
kubernetes-client/python#741
1 parent 296f55d commit 5019a67

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

batch/entrypoint.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,26 @@ def render_job(name: str, spec: str, job_input: str) -> V1Job:
2323
)
2424

2525

26-
def callback(batch_v1: BatchV1Api, namespace: str, spec: str) -> Callable[[Message], None]:
26+
def callback(namespace: str, spec: str) -> Callable[[Message], None]:
2727
def cb(m: Message):
2828
try:
2929
job_name = md5(m.message_id.encode('utf-8')).hexdigest()
3030
job_input = m.data.decode('utf-8')
3131
log.info(f'Submitting job {job_name} with input "{job_input}"')
3232

3333
job = render_job(job_name, spec, job_input)
34-
batch_v1.create_namespaced_job(namespace, job)
34+
get_batch_v1().create_namespaced_job(namespace, job)
3535
log.info(f'Submitted job {job_name}')
3636
except Exception:
3737
log.exception('PubSub subscriber callback')
3838
m.ack()
3939
return cb
4040

4141

42-
def listen(subscription: str, batch_v1: BatchV1Api, namespace: str, spec: str) -> None:
42+
def listen(subscription: str, namespace: str, spec: str) -> None:
4343
subscriber = pubsub_v1.SubscriberClient()
4444
with subscriber:
45-
cb = callback(batch_v1, namespace, spec)
45+
cb = callback(namespace, spec)
4646
streaming_pull = subscriber.subscribe(subscription, cb)
4747
log.info(f'Listening to subscription {subscription}')
4848
try:
@@ -57,7 +57,7 @@ def load_job_spec(spec_path: str) -> str:
5757
return f.read()
5858

5959

60-
def get_batch_v1():
60+
def get_batch_v1() -> BatchV1Api:
6161
try:
6262
load_kube_config()
6363
except:
@@ -74,9 +74,7 @@ def main():
7474
log.basicConfig(level=log_level)
7575

7676
spec = load_job_spec(spec_path)
77-
batch_v1 = get_batch_v1()
78-
79-
listen(subscription, batch_v1, namespace, spec)
77+
listen(subscription, namespace, spec)
8078

8179

8280
if __name__ == '__main__':

0 commit comments

Comments
 (0)