Skip to content

Commit 41931f6

Browse files
authored
MRG: Merge pull request #524 from octue/feature/add-dynamic-children-selection
Add dynamic children override
2 parents 2952d33 + ac4d5d9 commit 41931f6

17 files changed

+497
-381
lines changed

docs/source/asking_questions.rst

Lines changed: 81 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Asking services questions
55
=========================
66

77
Octue services
8-
--------------
8+
==============
99

1010
There's a growing range of live :ref:`services <service_definition>` in the Octue ecosystem that you can ask questions
1111
to and get answers from. Currently, all of them are related to wind energy. Here's a quick glossary of terms before we
@@ -38,7 +38,7 @@ tell you more:
3838

3939

4040
How to ask a question
41-
---------------------
41+
=====================
4242
You can ask any service a question if you have its service ID, project name, and permissions. The question is formed of
4343
input values and/or an input manifest.
4444

@@ -51,7 +51,10 @@ input values and/or an input manifest.
5151
backend={"name": "GCPPubSubBackend", "project_name": "my-project"},
5252
)
5353
54-
answer = child.ask(input_values={"height": 32, "width": 3}, input_manifest=manifest)
54+
answer = child.ask(
55+
input_values={"height": 32, "width": 3},
56+
input_manifest=manifest,
57+
)
5558
5659
answer["output_values"]
5760
>>> {"some": "data"}
@@ -62,9 +65,12 @@ input values and/or an input manifest.
6265
6366
You can also set the following options when you call :mod:`Child.ask <octue.resources.child.Child.ask>`:
6467

68+
- ``children`` - If the child has children of its own (i.e. grandchildren of the parent), this optional argument can be used to override the child's "default" children. This allows you to specify particular versions of grandchildren to use (see :ref:`this subsection below <overriding_children>`).
6569
- ``subscribe_to_logs`` - if true, the child will forward its logs to you
6670
- ``allow_local_files`` - if true, local files/datasets are allowed in any input manifest you supply
6771
- ``handle_monitor_message`` - if provided a function, it will be called on any monitor messages from the child
72+
- ``record_messages_to`` – if given a path to a JSON file, messages received from the parent while it processes the question are saved to it
73+
- ``allow_save_diagnostics_data_on_crash`` – if true, the input values and input manifest (including its datasets) will be saved by the child for future crash diagnostics if it fails while processing them
6874
- ``question_uuid`` - if provided, the question will use this UUID instead of a generated one
6975
- ``timeout`` - how long in seconds to wait for an answer (``None`` by default - i.e. don't time out)
7076

@@ -78,7 +84,7 @@ sent back to the parent. If you're not sure how long a particular analysis might
7884

7985

8086
Asking multiple questions in parallel
81-
-------------------------------------
87+
=====================================
8288
You can also ask multiple questions to a service in parallel.
8389

8490
.. code-block:: python
@@ -98,7 +104,7 @@ This method uses threads, allowing all the questions to be asked at once instead
98104

99105

100106
Asking a question within a service
101-
----------------------------------
107+
==================================
102108
If you have :doc:`created your own Octue service <creating_services>` and want to ask children questions, you can do
103109
this more easily than above. Children are accessible from the ``analysis`` object by the keys you give them in the
104110
:ref:`app configuration <app_configuration>` file. For example, you can ask an ``elevation`` service a question like
@@ -154,3 +160,73 @@ See the parent service's `app configuration <https://github.com/octue/octue-sdk-
154160
and `app.py file <https://github.com/octue/octue-sdk-python/blob/main/octue/templates/template-child-services/parent_service/app.py>`_
155161
in the `child-services app template <https://github.com/octue/octue-sdk-python/tree/main/octue/templates/template-child-services>`_
156162
to see this in action.
163+
164+
.. _overriding_children:
165+
166+
Overriding a child's children
167+
=============================
168+
If the child you're asking a question to has its own children (static children), you can override these by providing the
169+
IDs of the children you want it to use (dynamic children) to the :mod:`Child.ask <octue.resources.child.Child.ask>`
170+
method. Questions that would have gone to the static children will instead go to the dynamic children. Note that:
171+
172+
- You must provide the children in the same format as they're provided in the :ref:`app configuration <app_configuration>`
173+
- If you override one static child, you must override others, too
174+
- The dynamic children must have the same keys as the static children (so the child knows which service to ask which
175+
questions)
176+
- You should ensure the dynamic children you provide are compatible with and appropriate for questions from the child
177+
service
178+
179+
For example, if the child requires these children in its app configuration:
180+
181+
.. code-block:: json
182+
183+
[
184+
{
185+
"key": "wind_speed",
186+
"id": "template-child-services/wind-speed-service",
187+
"backend": {
188+
"name": "GCPPubSubBackend",
189+
"project_name": "octue-amy"
190+
},
191+
},
192+
{
193+
"key": "elevation",
194+
"id": "template-child-services/elevation-service",
195+
"backend": {
196+
"name": "GCPPubSubBackend",
197+
"project_name": "octue-amy"
198+
},
199+
}
200+
]
201+
202+
then you can override them like this:
203+
204+
.. code-block:: python
205+
206+
answer = child.ask(
207+
input_values={"height": 32, "width": 3},
208+
children=[
209+
{
210+
"key": "wind_speed",
211+
"id": "my/own-service",
212+
"backend": {
213+
"name": "GCPPubSubBackend",
214+
"project_name": "octue-amy"
215+
},
216+
},
217+
{
218+
"key": "elevation",
219+
"id": "organisation/another-service",
220+
"backend": {
221+
"name": "GCPPubSubBackend",
222+
"project_name": "octue-amy"
223+
},
224+
},
225+
],
226+
)
227+
228+
Overriding beyond the first generation
229+
--------------------------------------
230+
It's an intentional choice to only go one generation deep with overriding children. If you need to be able to specify a
231+
whole tree of children, grandchildren, and so on, please `upvote this issue.
232+
<https://github.com/octue/octue-sdk-python/issues/528>`_

0 commit comments

Comments
 (0)