@@ -36,15 +36,27 @@ In order to work with pygeoapi-prefect, you need to set up:
3636
3737- a Prefect server;
3838- pygeoapi;
39- - at least one Prefect worker.
39+ - (if you want to support async execution) at least one Prefect worker.
4040
4141
4242### Prefect server
4343
4444After installation, you need to have a Prefect server running and to set some environment variables
4545so that the Prefect Manager is able to communicate with it.
4646
47- As the most basic setup, just start a local Prefect server
47+ As the most basic setup:
48+
49+ generate an env file for configuring Prefect, ensuring that you
50+ enable [ persistence of results] ( https://docs.prefect.io/v3/advanced/results ) .
51+
52+ ``` shell
53+ cat << EOF > prefect-env.env
54+ PREFECT_API_URL=http://127.0.0.1:4200/api
55+ PREFECT_RESULTS_PERSIST_BY_DEFAULT=true
56+ EOF
57+ ```
58+
59+ Then start a local Prefect server
4860
4961=== "uv"
5062
@@ -79,6 +91,51 @@ server:
7991The Prefect Manager accepts the following configuration parameters, all of which are optional.
8092These should be specified as properties of the ` server.manager` object:
8193
94+ - `enable_sync_job_execution : bool = False` - Whether to accept execution requests that run synchronously.
95+
96+ !!! NOTE
97+
98+ This setting is disabled by default and we recommend keeping it disabled - Executing syncronously risks
99+ overloading the server in case a large number of requestes is received.
100+
101+ If needed, the metadata of
102+ each processor is allowed to override this setting, making it possible to selectively enable sync
103+ execution for those processors in which it is a useful execution mode.
104+
105+ # ### Controlling processor execution modes via pygeoapi config
106+
107+ By default, pygeoapi does not allow modying a processor's metadata in its configuration and assumes that
108+ all parameters are to be set together with the processor code. This has the disadvantage of making it
109+ impossible to toggle a processor's support for sync execution on and off without modifying the source code.
110+ In order to ease configuration, `PrefectManager` will look for a `job_control_options` configuration key on
111+ its own configuration file, which has the same meaning as the `jobControlOptions` metadata key, _i.e._ it
112+ should be a list of strings with at least one entry and can contain `sync-execute` and `async-execute` entries.
113+
114+ In the following example we disable sync mode in the manager but re-enable it in the `hello-world`
115+ processor configuration :
116+
117+ ` ` ` yaml
118+ # pygeoapi configuration file
119+ server:
120+ manager:
121+ name: pygeoapi_prefect.PrefectManager
122+ enable_sync_job_execution: false
123+ resources:
124+ hello-world:
125+ type: process
126+ processor:
127+ name: HelloWorld
128+ job_control_options:
129+ - sync-execute
130+ ` ` `
131+
132+
133+ - `enable_async_job_execution : bool = True` - Whether to accept execution requests that run asynchronously.
134+ This is enabled by default and is the recommended way to operate. Async execution defers the scheduling of
135+ processor execution to the Prefect scheduler, allowing for more stable operation, as the scheduler can control
136+ when to execute jobs depending on the load. Similarly to the previous parameter, this can also be
137+ overridden on a per-processor basis.
138+
82139- `use_deployment_for_sync_requests : bool = False` - Whether to call native pygeoapi processors via the
83140 Prefect deployment interface or not. If this is enabled :
84141
@@ -88,7 +145,11 @@ These should be specified as properties of the `server.manager` object:
88145 - This means that job execution is slower, as there is a temporal overhead that is introduced by
89146 the coordination that happens between the Prefect scheduler service and the Prefect worker
90147
91- If this is disabled, then jobs are executed immediately and run in the same process as pygeoapi
148+ If this is disabled (the default), then jobs are executed immediately and run in the same process as
149+ pygeoapi.
150+
151+ Note that regardless of this being enabled or not, all pygeoapi processes execution requests are tracked by
152+ Prefect and can be monitored using the Prefect UI.
92153
93154- `sync_job_execution_timeout_seconds : int = 60` - How much time to give a sync job to finish its
94155 processing before declaring it as failed
@@ -115,10 +176,62 @@ resources:
115176 name: HelloWorld
116177` ` `
117178
179+ The `PrefectManager` wraps these processors inside a Prefect [flow](https://docs.prefect.io/v3/concepts/flows) and
180+ is able to execute them by generating Prefect flow runs whenever an execution request is received. Execution can be
181+ either sync or async, as both types are supported by the Prefect manager.
182+
183+
184+ # ##### Sync execution
185+
186+ Whenever pygeoapi receives a sync processor execution request, such as :
187+
188+ ` ` ` shell
189+ curl \
190+ -X POST \
191+ "http://localhost:5000/processes/hello-world/execution" \
192+ -H "Content-Type: application/json" \
193+ -d '{"inputs": {"name": "Joe"}}'
194+ ` ` `
195+
196+ This is turned into a Prefect flow run and is executed locally, in the same process as pygeoapi. In other words,
197+ the pygeoapi processor is wrapped as a Prefect flow and is run by the Prefect manager, using regular function calls.
198+
199+ Being managed by Prefect, this means.
200+
201+
202+ # ##### Async execution
203+
204+ When pygeoapi receives an async processor execution request, such as :
205+
206+ ` ` ` shell
207+ curl \
208+ -X POST \
209+ "http://localhost:5000/processes/hello-world/execution" \
210+ -H "Content-Type: application/json" \
211+ -H "Prefer: respond-async" \
212+ -d '{"inputs": {"name": "Joe"}}'
213+ ` ` `
214+
215+ This is turned into a Prefect flow and is executed via the respective processor deployment
216+
118217
119218# #### custom prefect-aware processes
120219
121- TBD
220+ If you prefer, you can write Prefect flows, deploy them using any of the multiple techniques supported by Prefect
221+ and then adapt them to run as pygeoapi processors.
222+
223+ In order to do so, your Prefect flows need to implement the
224+ [protocol](https://typing.python.org/en/latest/spec/protocol.html)
225+ defined in `pygeoapi_prefect.PygeoapiPrefectFlowProtocol`
226+
227+ ` ` ` python
228+ from prefect import flow
229+
230+ @flow()
231+ def my_custom_flow()
232+
233+ ` ` `
234+
122235
123236# ### Launching pygeoapi
124237
0 commit comments