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.1.3.tar.gz (8.4 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.1.3-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for requence-0.1.3.tar.gz
Algorithm Hash digest
SHA256 b47eb67b903fad102b85321c10d4f2fbb45be2d418cf4b22088fe5ca30add081
MD5 9194b0a792653154f2f73aa979463373
BLAKE2b-256 cfea4d50aa5632022c8ed0c8429a668d2a53c0206c67fc30b91bee5b3441dd8e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: requence-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 7.8 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.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 91e0b0b216df2e955a8fb0f08ccb9b36199a7bc0d59f8e3b1435195b91f05511
MD5 23acf38bf5656d7da64971b96197072f
BLAKE2b-256 e300740a160bd40feb1f7538ef343f5a3b80426a9c972e696a9156363a568c12

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