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.4.tar.gz (8.5 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.4-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: requence-0.1.4.tar.gz
  • Upload date:
  • Size: 8.5 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.4.tar.gz
Algorithm Hash digest
SHA256 ad56241a0245c52cd1cda5c615e0b649b3dc4e17d9c8cc1fefe6f74685917702
MD5 2a8065e038eeda4bee5d75354405664c
BLAKE2b-256 ce41ede715415b539e2392820dc513461212ff625df09acbe5d1c14c8e803e5f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: requence-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 7.9 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.4-py3-none-any.whl
Algorithm Hash digest
SHA256 3a0d19421a9884cc0febc90328bcb356b1a4fd1c343cb0bf1f040c57ada3beac
MD5 f7690f4000ff81278d5b28f9716005ae
BLAKE2b-256 e1da1c41dc4945416b8bf881f23084f55c23d921424923361220eb0cc4353b7e

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