Service#

The Service module contains the Service class, which can be included into pipeline as object or a dictionary. Pipeline consists of services and service groups. Service group can be synchronous or asynchronous. Service is an atomic part of a pipeline. Service can be asynchronous only if its handler is a coroutine. Actor wrapping service is asynchronous.

class Service(handler, before_handler=None, after_handler=None, timeout=None, asynchronous=None, start_condition=None, name=None)[source]#

Bases: PipelineComponent

This class represents a service. Service can be included into pipeline as object or a dictionary. Service group can be synchronous or asynchronous. Service can be asynchronous only if its handler is a coroutine.

Parameters:
  • handler (ServiceBuilder) – A service function or an actor.

  • wrappers – List of Wrappers to add to the service.

  • timeout (Optional[float]) – Timeout to add to the group.

  • asynchronous (Optional[bool]) – Requested asynchronous property.

  • start_condition (Optional[StartConditionCheckerFunction]) – StartConditionCheckerFunction that is invoked before each service execution; service is executed only if it returns True.

  • name (Optional[str]) – Requested service name.

async _run_handler(ctx, pipeline)[source]#

Method for service handler execution. Handler has three possible signatures, so this method picks the right one to invoke. These possible signatures are:

  • (ctx: Context) - accepts current dialog context only.

  • (ctx: Context, pipeline: Pipeline) - accepts context and current pipeline.

  • (ctx: Context, pipeline: Pipeline, info: ServiceRuntimeInfo) - accepts context, pipeline and service runtime info dictionary.
Parameters:
  • ctx (Context) – Current dialog context.

  • pipeline (Pipeline) – The current pipeline.

Return type:

None

Returns:

None

async _run_as_actor(ctx, pipeline)[source]#

Method for running this service if its handler is an Actor. Catches runtime exceptions.

Parameters:

ctx (Context) – Current dialog context.

Return type:

None

async _run_as_service(ctx, pipeline)[source]#

Method for running this service if its handler is not an Actor. Checks start condition and catches runtime exceptions.

Parameters:
  • ctx (Context) – Current dialog context.

  • pipeline (Pipeline) – Current pipeline.

Return type:

None

async _run(ctx, pipeline)[source]#

Method for handling this service execution. Executes before and after execution wrappers, launches _run_as_actor or _run_as_service method.

Parameters:
  • ctx (Context) – (required) Current dialog context.

  • pipeline (Pipeline) – the current pipeline.

Return type:

None

property info_dict: dict#

See Component.info_dict property. Adds handler key to base info dictionary.

to_service(before_handler=None, after_handler=None, timeout=None, asynchronous=None, start_condition=None, name=None)[source]#

Function for decorating a function as a Service. Returns a Service, constructed from this function (taken as a handler). All arguments are passed directly to Service constructor.