Skip to main content

service object flow orchestrator

Project description

service-flow

A small, simple, and elegant component/function orchestration framework for Python, service-flow enables separation of a functionality into single-responsibility and bite-size functions/service objects, which are testable and reuseable. It further helps with readability and maintainability of the embedding application.

Implementation

service-flow has two parts -- a flow and multiple services (Note: the word service here refers to a function, not RESTful services):

flow

a flow is the definition of processing procedure. It is defined as the following:

basic example (>>)

flow = Service1() >> \
        Service2(*args) >> \
        ...
        ServiceN(**kwargs)

output = flow(input) ## input is a dictionary with all the input parameters as attributes

Python >> operator is overloaded to define the sequence of the processing. Service1(), ..., ServiceN() are instances of the services, or functions.

for each processing of the input, service-flow creates a context, a dictionary-like object that serves as input and gather outputs from all the services.

fork example (<)

flow = Service1() >> \
        Service2(*args) < \
        ('context_var_name', {
            'var_value1': (Service3() >> Service4(**kwargs)),
            'var_value2': (Service5() >> Service4(**kwargs)),
          }
        )

output = flow(input) ## input is a dictionary with all the input parameters as attributes

Python < operator is overloaded to define a fork in processing. In this example, context_var_name is the name of context key, and var_value1, var_value2 are potential values for forking.

A fork can not be the first service in a flow definition.

services

A service is the equivalent of a Python function.

example

basic service

A element in a list of services that composes the flow. it inherits from service_flow.middleware.Middleware.

class InplaceModification(Middleware):
    def __init__(self, increment): # initialization
        self.increment = 1

    def __call__(self, bar: list): # service call
        return {'bar': [i + self.increment for i in bar]}
decorator service

As the name implies, decorator service works similiarly to a python decorator that is nested on top of the subsequent flow.

A decorator service inherits from service_flow.middleware.DecoratorMiddleware and has one additional instance variable next, which is the reference to the subsequent flow.

class ExceptionHandler(DecoratorMiddleware):
    def __call__(self, context):
        try:
            self.next(context)
        except ZeroDivisionError:
            return {'error': 'decided by zero'}

conventions

It needs to follow the convention below:

initialization parameters

parameters that initializes the service and is shared for all the calls to the service.

function parameters

These parameters are used as inputs to a service. They normally have to be existing attributes in the context object.

The only exception is if there is only one parameter and its name is "context", in which case the entire context dictionary will be passed in as the value of the context parameter.

return value

the return value of a service is optional. If a service does return values:

  1. if it is a dictionary, it will add/update the aforementioned processing context.
  2. otherwise, the return value is ignored and a warning message is logged.

exceptions

service-flow raises a few types of exceptions:

  1. StopFlowException: raised inside a service to signal stop processing. typical use cases include when incoming request is invalid.
  2. RetryException: raised inside a service to signal re-processing of the same request. typical use cases include when a http request issued from the service times out or database transaction level violation.
  3. FatalException: raised inside a service to restart the processor. typical use cases include when database connection is broken or other infrastructure related errors.

inspiration

service-flow draws inspiration from the following Ruby projects:

  1. Rack
  2. Light Service
  3. Ruby Middleware

TODOs

install

pip install service-flow

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

service_flow-0.2.0.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

service_flow-0.2.0-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: service_flow-0.2.0.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.0

File hashes

Hashes for service_flow-0.2.0.tar.gz
Algorithm Hash digest
SHA256 5bba3a07886449fc05f5ade1fe4b85ae125f64e7807bfba0aa52ff61550a5233
MD5 2d5df524ffa0dc7768fbbbcb76ae7ca8
BLAKE2b-256 5783beb9067c3c2be1b59c1114186fca0f5d0866446135796391d5f842787d89

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for service_flow-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5464507fb830d0652a0c0cdaa26e4f494bef1c182ca0757efcc4c25160398e85
MD5 cb0935d24606ebe67bd44e6f6a90190c
BLAKE2b-256 848b3e2f840084b1a1f9f0e93ceedbc2e3a8a7af16d0a465826739172c43a435

See more details on using hashes here.

Supported by

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