OpenWhisk Actions on Knative — Asynchronous Actions
This is a follow up blog post of How to run OpenWhisk actions on Knative?
In this blog post, we are going to experiment with Asynchronous NodeJS action.
The main
function in this action returns a Promise, which indicates that the activation hasn't completed yet, but is expected to in the future. The setTimeout()
JavaScript function in this case waits for two seconds before calling the callback function. This represents the asynchronous code and goes inside the Promise's callback function. The Promise’s callback takes two arguments, resolve and reject, which are both functions. The call to resolve()
fulfills the Promise and indicates that the activation has completed normally. A call to reject()
can be used to reject the Promise and signal that the activation has completed abnormally.
Pre-requisites:
- Secret exists on Knative, check with:
$ kubectl get secret dockerhub-user-pass
- Service Account exists on Knative, check with:
$ kubectl get serviceaccount openwhisk-runtime-builder
- Build Template exists on Knative, check with:
$ kubectl get buildtemplate openwhisk-nodejs-runtime
If any of the secret/service account/build template does not exist on your Knative installation, please go back to the How to run OpenWhisk actions on Knative? blog post and run Step 1 through Step 3 depending on what’s missing.
Deploy NodeJS Runtime with Async Action Code
Configure the build file to point to your Docker Hub repo by replacing DOCKER_USERNAME
with your username.
Deploy NodeJS runtime with action code:
$ kubectl apply -f tests/extra/build.yaml
build.build.knative.dev/nodejs-10-async created
Verify the build pod exists:
$ kubectl get build.build.knative.dev/nodejs-10-async
NAME SUCCEEDED REASON STARTTIME COMPLETIONTIME
nodejs-10-async True 1m
Serve NodeJS Runtime as a Knative Service
Configure the service template to point to the Docker Hub repo where the OpenWhisk runtime (built in previous step) will be pulled from. Replace ${DOCKER_USERNAME}
and create service.yaml
:
Run Async Action
Depending on your Knative installation, determine how to access your knative gateway and run sequence of curl
:
$ curl -H "Host: nodejs-async.default.example.com" -X POST http://<IP_ADDRESS>:<PORT>
{"OK":true}$ time curl -H "Host: nodejs-async.default.example.com" -X POST http://<IP_ADDRESS>:<PORT>
{"done":true}
real 0m2.027s
user 0m0.006s
sys 0m0.008s
Note that how long the invocation took to complete, just a little more than 2 seconds of which 2 seconds are coming from the timeout function.
Enjoy! 👍