This repository provides a Python library for Composer. For convenience, the Composer documentation is repeated below using Python bindings instead of JavaScript.
Composer is a new programming model for composing cloud functions built on Apache OpenWhisk. With Composer, developers can build even more serverless applications including using it for IoT, with workflow orchestration, conversation services, and devops automation, to name a few examples.
This repository includes:
- the composer Python library for authoring compositions using Python,
- the pycompose and pydeploy commands for compiling and deploying compositions,
- documentation, examples, and tests.
You need Python 3 installed on your system.
$ git clone https://github.com/ibm-functions/composer-python.git
$ cd composer-python
$ pip3 install -e .
$ pycompose -h
usage: pycompose composition.py command [flags]
$ pydeploy -h
usage: pydeploy composition composition.json [flags]
$ pip3 install git+https://github.com/ibm-functions/composer-python.git
A composition is typically defined by means of a Python expression as illustrated in samples/demo.py:
import composer
def authenticate(args):
return {'value': args['password'] == 'abc123'}
def success(args):
return {'message': 'success'}
def failure(args):
return {'message': 'failure'}
def main():
return composer.when(
composer.action('authenticate', { 'action': authenticate }),
composer.action('success', { 'action': success }),
composer.action('failure', { 'action': failure }))
Compositions compose actions using combinator methods. These methods
implement the typical control-flow constructs of a sequential imperative
programming language. This example composition composes three actions named
authenticate
, success
, and failure
using the composer.when
combinator,
which implements the usual conditional construct. It takes three actions (or
compositions) as parameters. It invokes the first one and, depending on the
result of this invocation, invokes either the second or third action.
One way to deploy a composition is to use the pycompose
and pydeploy
commands:
$ pycompose demo.py > demo.json
$ pydeploy demo demo.json -w
ok: created /_/authenticate,/_/success,/_/failure,/_/demo
The pycompose
command compiles the composition code to a portable JSON format.
The pydeploy
command deploys the JSON-encoded composition creating an action
with the given name. It also deploys the composed actions if definitions are
provided for them. The -w
option authorizes the deploy
command to overwrite
existing definitions.
The demo
composition may be invoked like any action, for instance using the
IBM Cloud CLI:
$ ibmcloud fn action invoke demo -p password passw0rd
ok: invoked /_/demo with id 09ca3c7f8b68489c8a3c7f8b68b89cdc
The result of this invocation is the result of the last action in the
composition, in this case the failure
action since the password in incorrect:
$ ibmcloud fn activation result 09ca3c7f8b68489c8a3c7f8b68b89cdc
{
"message": "failure"
}
This invocation creates a trace, i.e., a series of activation records:
$ ibmcloud fn activation list
Datetime Activation ID Kind Start Duration Status Entity
2019-03-15 16:43:22 e6bea73bf75f4eb7bea73bf75fdeb703 nodejs:6 warm 1ms success guest/demo:0.0.1
2019-03-15 16:43:21 7efb6b7354c3472cbb6b7354c3272c98 nodejs:6 cold 31ms success guest/failure:0.0.1
2019-03-15 16:43:21 377cd080f0674e9cbcd080f0679e9c1d nodejs:6 warm 2ms success guest/demo:0.0.1
2019-03-15 16:43:20 5dceeccbdc7a4caf8eeccbdc7a9caf18 nodejs:6 cold 29ms success guest/authenticate:0.0.1
2019-03-15 16:43:19 66355a1f012d4ea2b55a1f012dcea264 nodejs:6 cold 104ms success guest/demo:0.0.1
2019-03-15 16:43:19 09ca3c7f8b68489c8a3c7f8b68b89cdc sequence warm 3.144s success guest/demo:0.0.1
The entry with the earliest start time (09ca3c7f8b68489c8a3c7f8b68b89cdc
)
summarizes the invocation of the composition while other entries record later
activations caused by the composition invocation. There is one entry for each
invocation of a composed action (5dceeccbdc7a4caf8eeccbdc7a9caf18
and
7efb6b7354c3472cbb6b7354c3272c98
). The remaining entries record the beginning
and end of the composition as well as the transitions between the composed
actions.
Compositions are implemented by means of OpenWhisk conductor actions. The documentation of conductor actions explains execution traces in greater details.