Skip to content

Allow intermediate services to continue running if child error #371

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

Closed
Show file tree
Hide file tree
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
40 changes: 36 additions & 4 deletions octue/cloud/pub_sub/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import functools
import json
import logging
import math
import time
import uuid

Expand Down Expand Up @@ -96,10 +97,32 @@ def serve(self, timeout=None, delete_topic_and_subscription_on_exit=False):
future = subscriber.subscribe(subscription=subscription.path, callback=self.answer)
logger.debug("%r is waiting for questions.", self)

try:
future.result(timeout=timeout)
except (TimeoutError, concurrent.futures.TimeoutError, KeyboardInterrupt):
future.cancel()
start_time = time.perf_counter()
run_time = 0

if timeout is None:
timeout = math.inf

while run_time < timeout:

logger.info("%r is waiting for new questions...", self)
future = subscriber.subscribe(subscription=subscription.path, callback=self.answer)

try:
if timeout == math.inf:
future.result(timeout=None)
else:
future.result(timeout=timeout - run_time)

# Abandon analysis for current question but continue listening for new ones.
except octue.exceptions.AbandonAnalysis:
run_time = time.perf_counter() - start_time
continue

# Stop serving.
except (TimeoutError, concurrent.futures.TimeoutError, KeyboardInterrupt):
future.cancel()
break

finally:
if delete_topic_and_subscription_on_exit:
Expand Down Expand Up @@ -276,6 +299,7 @@ def wait_for_answer(
handle_monitor_message=None,
service_name="REMOTE",
timeout=60,
just_log_errors=False,
delivery_acknowledgement_timeout=30,
retry_interval=5,
):
Expand Down Expand Up @@ -326,6 +350,14 @@ def wait_for_answer(
time.sleep(retry_interval)
self.ask(**self._current_question)

except Exception as e:

if just_log_errors:
logger.exception(e)
raise octue.exceptions.AbandonAnalysis()

raise e

finally:
subscription.delete()
subscriber.close()
Expand Down
4 changes: 4 additions & 0 deletions octue/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ class CloudStorageBucketNotFound(OctueSDKException):
"""Raise if attempting to access a cloud storage bucket that cannot be found."""


class AbandonAnalysis(OctueSDKException):
"""Raise to abandon the current analysis but remain running for new analyses."""


class PushSubscriptionCannotBePulled(OctueSDKException):
"""Raise if attempting to pull a push subscription."""

Expand Down
2 changes: 2 additions & 0 deletions octue/resources/child.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def ask(
input_values=None,
input_manifest=None,
subscribe_to_logs=True,
just_log_errors=False,
allow_local_files=False,
handle_monitor_message=None,
question_uuid=None,
Expand Down Expand Up @@ -63,5 +64,6 @@ def ask(
subscription=subscription,
handle_monitor_message=handle_monitor_message,
service_name=self.name,
just_log_errors=just_log_errors,
timeout=timeout,
)