Skip to main content

Consumer implementation

Project description

requence

This package connects python to the operator bus. It manages the retrieval and responses of messages.

Usage

from requence.consumer import ContextHelper, Consumer

def handleMessage(context: ContextHelper):
    return "this is my response"

Consumer({}, handleMessage)

Every consumer instance needs a url parameter to connect to the operator and a version. In the basic example, those parameters get automatically retrieved from environment variables REQUENCE_URL and VERSION. To be more explicit about those configurations, you can pass an object as the first parameter:

Consumer(
  {
    'url': 'your operator connection url string',
    'version': 'your version in format major.minor.patch',
  },
  handleMessage
)

Additional configuration

By default, the consumer retrieves one message from the operator, processes it and passes the response back to the operator. If your service is capable of processing multiple messages in parallel, you can define a higher prefetch.

Consumer(
  {
    'prefetch': 10, # this will process max. 10 messages at once when available
  },
  handleMessage
)

Unsubscribing service

Should you ever need to unsubscribe your service from the operator programmatically, Consumer has a close method.

consumer = Consumer(...)

# later
consumer.close()

To resubscribe, you have to instantiate a new Consumer

Processing messages

The message handler callback provides one argument: the message context. The context provides helper methods to access previous operator results and also methods to abort the processing early.

context api data retrieval

ctx.get_input(): Optional[Any]

The input that was defined when the task started

ctx.get_meta(): Optional[Any]

The additional meta information added to the task

ctx.get_configuration(): Optional[Any]

The optional configuration of the current service

ctx.get_service_meta(serviceIdentifier: str): TypedDict{
    'executionDate': Optional[datetime], # null when the service was not yet executed
  'id': str, // service id used for internal routing
  'alias': Optional[str], # service alias (see service Identifier)
  'name': str,
  'configuration': Optional[Any],
  'version': str
}

The meta parameters of a service, usually not needed

ctx.get_service_data(serviceIdentifier: str): Any

The response of a previously executed service

ctx.get_last_service_data(serviceIdentifier: str): Any

Same as ctx.get_service_data but only returns the last data when a service is used multiple times

ctx.get_service_error(serviceIdentifier: str): str | None

The error message of a previously executed service or null when said service was not yet executed or did process without error.

ctx.get_last_service_error(serviceIdentifier: str): str | None

Same as ctx.get_service_error but only returns the last error when a service is used multiple times

ctx.get_results(): Array[TypedDict{
  'executionDate': Optional[datetime], # null when the service was not yet executed
  'id': str, # service id used for internal routing
  'alias': Optional[str], # service alias (see service Identifier)
  'name': str,
  'configuration': Optional[Any],
  'version': str
  'error': Optional[str]
  'data': Optional[Any]
}]

get results of all configured services in this task. When a service did run prior to the current service, executionDate and error or data will be available.

ctx.get_tenant_name(): str

The name of the tenant that initiated the task

context api processing control

ctx.retry(delay: Optional[int]): None

Instructs the operator to retry the service after an optional delay in milliseconds (minimum is 100ms). No code gets executed after this line. Currently, it is your responsibility to prevent infinite loops.

ctx.abort(reason: Optional[str]): None

Instructs the operator to abort the processing of this service immediately. If the service is not configured with a fail over, the complete task will fail.

service identifier

Most context methods require a service identifier as parameter. This identifier can either be a service name or a service alias. The latter is useful for situations where a service is used multiple times in a task and needs the data from one specific execution.

Full example

Pseudo implementation of a database service

from requence.consumer import ContextHelper, Consumer
from someDb import db

def handleMessage(context: ContextHelper):
    ocrData = ctx.get_service_data("ocr")

    if (not ocrData):
      ctx.abort("Ocr service is mandatory for this AI service")

    if (not db.isConnected):
        # lets wait 2s for the db to recover
        ctx.retry(2000)

    response = db.getDataBasedOnOcr(ocrData)

    return response

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

requence-0.2.0.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

requence-0.2.0-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file requence-0.2.0.tar.gz.

File metadata

  • Download URL: requence-0.2.0.tar.gz
  • Upload date:
  • Size: 8.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.9

File hashes

Hashes for requence-0.2.0.tar.gz
Algorithm Hash digest
SHA256 f3063810ca4c888c1b3b7b26289552444b5a8461af09ecdd998b737b8b65640c
MD5 0c7719a8bb79e57013bceb2b07a8943a
BLAKE2b-256 7840f2c353622ddeaec641742934000e7d33cbaaf619a912e49f1cafe362f4c5

See more details on using hashes here.

File details

Details for the file requence-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: requence-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 8.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.9

File hashes

Hashes for requence-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f6f8d7b94473ac1b2974f9f5d413affe0e90067a22d5d905cb008559ea1d6eeb
MD5 8010a08dee12f8b080ebe6569113f503
BLAKE2b-256 9980f28fdf7df37ef2123605af7571268c04d09fb97f0980632aa95ce837e81f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page