Skip to content

rs_dpr_service/main.md

<< Back to index

rs dpr service main module.

DatabaseJobFormatError

Bases: Exception

Exception raised when an error occurred during the init of a provider.

Source code in docs/rs-dpr-service/rs_dpr_service/main.py
96
97
class DatabaseJobFormatError(Exception):
    """Exception raised when an error occurred during the init of a provider."""

JobsFormatError

Bases: Exception

Exception raised when an error occurred during the init of a provider.

Source code in docs/rs-dpr-service/rs_dpr_service/main.py
100
101
class JobsFormatError(Exception):
    """Exception raised when an error occurred during the init of a provider."""

app_lifespan(fastapi_app) async

Lifespann app to be implemented with start up / stop logic

Source code in docs/rs-dpr-service/rs_dpr_service/main.py
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
@asynccontextmanager
async def app_lifespan(fastapi_app: FastAPI):
    """Lifespann app to be implemented with start up / stop logic"""
    logger.info("Starting up the application...")
    # Create jobs table
    process_manager = init_db()

    fastapi_app.extra["process_manager"] = process_manager
    # fastapi_app.extra["db_table"] = db.table("jobs")
    # fastapi_app.extra["dask_cluster"] = cluster

    # Yield control back to the application (this is where the app will run)
    yield

    # Shutdown logic (cleanup)
    logger.info("Shutting down the application...")
    logger.info("Application gracefully stopped...")

build_cluster_info(data)

This function handles missing parameters from request, hence properly creates a ClusterInfo object.

Source code in docs/rs-dpr-service/rs_dpr_service/main.py
219
220
221
222
223
224
225
226
227
228
229
230
231
232
def build_cluster_info(data: dict) -> ClusterInfo:
    """This function handles missing parameters from request, hence properly creates a ClusterInfo object."""
    jupyter_token = data.get("jupyter_token")
    cluster_label = data.get("cluster_label")
    cluster_instance = data.get("cluster_instance", "")

    if jupyter_token is None or cluster_label is None:
        raise HTTPException(status_code=400, detail="Missing required fields: jupyter_token or cluster_label")

    return ClusterInfo(
        jupyter_token=jupyter_token,
        cluster_label=cluster_label,
        cluster_instance=cluster_instance,
    )

delete_job_endpoint(request, job_id=Annotated[str, Path(..., title='The ID of the job to delete')]) async

Deletes a specific job from the database.

Source code in docs/rs-dpr-service/rs_dpr_service/main.py
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
@router.delete("/dpr/jobs/{job_id}")
async def delete_job_endpoint(request: Request, job_id=Annotated[str, Path(..., title="The ID of the job to delete")]):
    """Deletes a specific job from the database."""

    # Send a dask distributed event for the cancellation of the job
    cancel_event = distributed.Event(CANCEL_JOB.format(job_id=job_id))
    if cancel_event.client:
        cancel_event.set()

    try:
        job = app.extra["process_manager"].get_job(job_id)
    # Handle case when job_id is not found
    except JobNotFoundError:  # pylint: disable=W0718
        return JSONResponse(status_code=HTTP_404_NOT_FOUND, content=f"Job with ID {job_id} not found")

    app.extra["process_manager"].delete_job(job_id)

    # Create job response with a status message to confirm the job deletion
    job["message"] = f"Job {job_id} deleted successfully"
    formatted_job_data = format_job_data(job)
    validate_response(request, formatted_job_data)
    return JSONResponse(status_code=HTTP_200_OK, content=formatted_job_data)

execute_process(request, resource) async

Used to execute processing jobs.

Source code in docs/rs-dpr-service/rs_dpr_service/main.py
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
@router.post("/dpr/processes/{resource}/execution")
async def execute_process(request: Request, resource: str):  # pylint: disable=unused-argument
    """Used to execute processing jobs."""

    # Validate request payload. This will create the 'http receive' fastapi span
    valid_body = await validate_request(request)

    # Run business logic in our own span
    with start_span(__name__, f"execute_processor_{resource}") as span:
        try:
            # Check that the input resource exists
            if resource not in api.config["resources"]:
                err_msg = f"Process {resource!r} not found"
                span.set_status(StatusCode.ERROR, err_msg)
                return JSONResponse(status_code=HTTP_404_NOT_FOUND, content=err_msg)

            cluster_info = build_cluster_info(valid_body)

            processor_name = api.config["resources"][resource]["processor"]["name"]
            if processor_name in processor_types:
                processor_type = processor_types[processor_name]
                # Asynchronously execute the dpr process in the dask cluster
                _, dpr_status = await processor_type(app.extra["process_manager"], cluster_info).execute(
                    valid_body,
                )  # type: ignore

                # Get identifier of the current job
                status_dict = {
                    "accepted": HTTP_201_CREATED,
                    "running": HTTP_201_CREATED,
                    "successful": HTTP_201_CREATED,
                    "failed": HTTP_500_INTERNAL_SERVER_ERROR,
                    "dismissed": HTTP_500_INTERNAL_SERVER_ERROR,
                }
                id_key = [status for status in status_dict if status in dpr_status][0]
                formatted_job_data = format_job_data(app.extra["process_manager"].get_job(dpr_status[id_key]))
                validate_response(request, formatted_job_data, HTTP_201_CREATED)
                span.set_status(StatusCode.OK, str(formatted_job_data))
                return JSONResponse(status_code=HTTP_201_CREATED, content=formatted_job_data)

            err_msg = f"Processor {processor_name!r} not found"
            span.set_status(StatusCode.ERROR, err_msg)
            return JSONResponse(status_code=HTTP_404_NOT_FOUND, content=err_msg)

        except Exception as e:
            record_error(span, e)
            raise

format_job_data(job)

Method to apply reformatting on job data to make it compliant with OGC (process) standards Args: job: information on a specific job to fromat: the job must have the same attributes than the columns from the PostgreSql database Result: reformatted and validated job_data variable to put in the response

Source code in docs/rs-dpr-service/rs_dpr_service/main.py
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
def format_job_data(job: dict):
    """
    Method to apply reformatting on job data to make it compliant with OGC (process) standards
    Args:
        job: information on a specific job to fromat: the job must have the same attributes
        than the columns from the PostgreSql database
    Result:
        reformatted and validated job_data variable to put in the response
    """
    # Check that the input job have the same struture as the jobs contained in the PostgreSQL database
    if "identifier" not in job:
        raise DatabaseJobFormatError(
            """Input job must have the same structure than the jobs stored in the """
            """PostgreSql database: attribute 'identifier' is missing""",
        )
    job_data = copy.deepcopy(job)
    # Rename attribute "identifier" to be compliant with OGC standards
    job_data[JOB_ATTRS_MAPPING["identifier"]] = job_data.pop("identifier")
    # Remove attributes which should not be part of the response
    for attr in OGC_UNCOMPLIANT_JOB_ATTRS:
        if attr in job_data:
            job_data.pop(attr)
    for key, value in job_data.items():
        # Reformat datetime object to string
        if isinstance(value, datetime):
            job_data[key] = value.strftime("%Y-%m-%dT%H:%M:%SZ")
    # Remove "finished" attribute if its value is None
    if "finished" in job_data and job_data.get("finished") is None:
        job_data.pop("finished")
    return job_data

format_jobs_data(jobs)

Method validate information on all existing jobs

Parameters:

Name Type Description Default
jobs dict

information on all existing jobs

required

Result: reformatted and validated jobs_data variable to provide to the response

Source code in docs/rs-dpr-service/rs_dpr_service/main.py
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
def format_jobs_data(jobs: dict):
    """
    Method validate information on all existing jobs

    Args:
        jobs: information on all existing jobs
    Result:
        reformatted and validated jobs_data variable to provide to the response
    """
    if not isinstance(jobs, dict):
        raise JobsFormatError("Expected a dictionary as input")
    if "jobs" not in jobs:
        raise JobsFormatError("Invalid format for input jobs: missing 'jobs' key")
    jobs_data = copy.deepcopy(jobs)
    # Add "links" mandatory field to the response
    jobs_data.update(
        {
            "links": [
                {
                    "href": "string",
                    "rel": "service",
                    "type": "application/json",
                    "hreflang": "en",
                    "title": "List of jobs",
                },
            ],
        },
    )
    # Remove SQLAlchemy _sa_instance_state objects and convert datetime
    for i, job_data in enumerate(jobs_data["jobs"]):
        jobs_data["jobs"][i] = format_job_data(job_data)
    return jobs_data

get_config_contents()

Return the pygeoapi configuration yaml file contents.

Source code in docs/rs-dpr-service/rs_dpr_service/main.py
124
125
126
127
128
129
130
131
132
133
134
def get_config_contents() -> dict:
    """Return the pygeoapi configuration yaml file contents."""
    # Open the configuration file
    with open(get_config_path(), encoding="utf8") as opened:
        contents = opened.read()

        # Replace env vars by their value
        contents = Template(contents).substitute(os.environ)

        # Parse contents as yaml
        return yaml.safe_load(contents)

get_config_path()

Return the pygeoapi configuration path and set the PYGEOAPI_CONFIG env var accordingly.

Source code in docs/rs-dpr-service/rs_dpr_service/main.py
117
118
119
120
121
def get_config_path() -> pathlib.Path:
    """Return the pygeoapi configuration path and set the PYGEOAPI_CONFIG env var accordingly."""
    path = pathlib.Path(__file__).parent.parent / "config" / "geoapi.yaml"
    os.environ["PYGEOAPI_CONFIG"] = str(path)
    return path

get_job_status_endpoint(request, job_id=Annotated[str, Path(..., title='The ID of the job')]) async

Used to get status of processing job.

Source code in docs/rs-dpr-service/rs_dpr_service/main.py
391
392
393
394
395
396
397
398
399
400
401
402
@router.get("/dpr/jobs/{job_id}")
async def get_job_status_endpoint(request: Request, job_id=Annotated[str, Path(..., title="The ID of the job")]):
    """Used to get status of processing job."""
    try:
        job = app.extra["process_manager"].get_job(job_id)
    except JobNotFoundError:  # pylint: disable=W0718
        # Handle case when job_id is not found
        return JSONResponse(status_code=HTTP_404_NOT_FOUND, content=f"Job with ID {job_id} not found")

    formatted_job_data = format_job_data(job)
    validate_response(request, formatted_job_data)
    return JSONResponse(status_code=HTTP_200_OK, content=formatted_job_data)

get_jobs_endpoint(request) async

Returns the status of all jobs from database.

Source code in docs/rs-dpr-service/rs_dpr_service/main.py
406
407
408
409
410
411
412
413
414
415
416
@router.get("/dpr/jobs")
async def get_jobs_endpoint(request: Request):
    """Returns the status of all jobs from database."""
    try:
        # Generate an output conform to OGC process specifications
        formatted_jobs_data = format_jobs_data(app.extra["process_manager"].get_jobs())
        validate_response(request, formatted_jobs_data)
        return JSONResponse(status_code=HTTP_200_OK, content=formatted_jobs_data)
    except Exception as e:  # pylint: disable=W0718
        # Handle exceptions and return an appropriate error message
        return JSONResponse(status_code=HTTP_404_NOT_FOUND, content=str(e))

get_processes(request) async

Returns list of all available processes from config.

Source code in docs/rs-dpr-service/rs_dpr_service/main.py
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
@router.get("/dpr/processes")
async def get_processes(request: Request):
    """Returns list of all available processes from config."""
    processes = {
        "processes": [],
        "links": [
            {"href": str(request.url), "rel": "self", "type": "application/json", "title": "List of processes"},
        ],
    }
    for resource in api.config["resources"]:
        processes["processes"].append(
            {
                "id": api.config["resources"][resource]["processor"]["name"],
                "version": "1.0.0",
            },
        )
    validate_response(request, processes)
    return JSONResponse(status_code=HTTP_200_OK, content=processes)

get_resource(request, resource) async

Should return info about a specific resource.

Source code in docs/rs-dpr-service/rs_dpr_service/main.py
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
@router.get("/dpr/processes/{resource}")
async def get_resource(request: Request, resource: str):
    """Should return info about a specific resource."""
    with start_span(__name__, "tasktable"):

        # Check that the input resource exists
        if resource not in api.config["resources"]:
            return JSONResponse(status_code=HTTP_404_NOT_FOUND, content=f"Process {resource!r} not found")

        cluster_info = build_cluster_info(dict(request.query_params))

        processor_name = api.config["resources"][resource]["processor"]["name"]
        if processor_name in processor_types:
            processor_type = processor_types[processor_name]
            task_table = await processor_type(app.extra["process_manager"], cluster_info).get_tasktable()
            return JSONResponse(status_code=HTTP_200_OK, content=task_table)

init_db(pause=3, timeout=None)

Initialize the PostgreSQL database connection and sets up required table and ENUM type.

This function constructs the database URL using environment variables for PostgreSQL credentials, host, port, and database name. It then creates an SQLAlchemy engine and registers the ENUM type JobStatus and the 'job' tables if they don't already exist.

Environment Variables
  • POSTGRES_USER: Username for database authentication.
  • POSTGRES_PASSWORD: Password for the database.
  • POSTGRES_HOST: Hostname of the PostgreSQL server.
  • POSTGRES_PORT: Port number of the PostgreSQL server.
  • POSTGRES_DB: Database name.

Parameters:

Name Type Description Default
pause int

pause in seconds to wait for the database connection.

3
timeout int | None

timeout in seconds to wait for the database connection.

None

Returns:

Type Description
PostgreSQLManager

PostgreSQLManager instance

Source code in docs/rs-dpr-service/rs_dpr_service/main.py
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
def init_db(pause: int = 3, timeout: int | None = None) -> PostgreSQLManager:
    """Initialize the PostgreSQL database connection and sets up required table and ENUM type.

    This function constructs the database URL using environment variables for PostgreSQL
    credentials, host, port, and database name. It then creates an SQLAlchemy engine and
    registers the ENUM type JobStatus and the 'job' tables if they don't already exist.

    Environment Variables:
        - POSTGRES_USER: Username for database authentication.
        - POSTGRES_PASSWORD: Password for the database.
        - POSTGRES_HOST: Hostname of the PostgreSQL server.
        - POSTGRES_PORT: Port number of the PostgreSQL server.
        - POSTGRES_DB: Database name.

    Args:
        pause: pause in seconds to wait for the database connection.
        timeout: timeout in seconds to wait for the database connection.

    Returns:
        PostgreSQLManager instance
    """
    manager_def = api.config["manager"]
    if not manager_def or not isinstance(manager_def, dict) or not isinstance(manager_def["connection"], dict):
        message = "Error reading the manager definition for pygeoapi PostgreSQL Manager"
        logger.error(message)
        raise RuntimeError(message)
    connection = manager_def["connection"]

    # Create SQL Alchemy engine
    engine = get_engine(driver_name="postgresql+psycopg2", **connection)

    while True:
        try:
            # This registers the ENUM type and creates the jobs table if they do not exist
            Base.metadata.create_all(bind=engine)
            logger.info(f"Reached {engine.url!r}")
            logger.info("Database table and ENUM type created successfully.")
            break

        # It fails if the database is unreachable. Wait a few seconds and try again.
        except SQLAlchemyError:
            logger.warning(f"Trying to reach {engine.url!r}")

            # Sleep for n seconds and raise exception if timeout is reached.
            if timeout is not None:
                timeout -= pause
                if timeout < 0:
                    raise
            sleep(pause)

    # Initialize PostgreSQLManager with the manager configuration
    return PostgreSQLManager(manager_def)

init_pygeoapi()

Init pygeoapi

Source code in docs/rs-dpr-service/rs_dpr_service/main.py
137
138
139
def init_pygeoapi() -> API:
    """Init pygeoapi"""
    return API(get_config_contents(), "")